How to draw lines (segments) using the arrow keys? - java

I'm trying to resolve an excercise about drawing lines using the arrow keys. The line starts from the center and draws toward east, west, north or south when one of the arrow keys is pressed. The code works only in east or west direction and not in a north or south and that is my problem!!
Could someone give me an idea about this matter? Thanks.
Here's the code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DrawingLinesUsingTheArrowKeys extends JFrame {
// Create a panel
private LinesUsingTheArrowKeys LinesUsingTheArrowKeys = new LinesUsingTheArrowKeys();
public DrawingLinesUsingTheArrowKeys() {
add(LinesUsingTheArrowKeys);
/*
* A component (keyboard) must be focused for its can receive the
* KeyEvent To make a component focusable , set its focusable property
* to true
*/
LinesUsingTheArrowKeys.setFocusable(true);
}
public static void main(String[] args) {
JFrame frame = new DrawingLinesUsingTheArrowKeys();
frame.setTitle("Drawing Lines Using The Arrow Keys");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
frame.setVisible(true);
}
// Inner class: LinesUsingTheArrowKeys (keyboardPanel) for receiving key
// input
static class LinesUsingTheArrowKeys extends JPanel {
private int x = 200;
private int y = 100;
private int x1 = x + 10;
private int y1 = y;
// register listener
public LinesUsingTheArrowKeys() {
addKeyListener(new KeyAdapter() {
#Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
// x1 += y1;
y1 += 10;
repaint();
} else if (e.getKeyCode() == KeyEvent.VK_UP) {
y1 -= 10;
repaint();
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
x1 += 10;
repaint();
} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
x1 -= 10;
repaint();
}
}
});
}
// Draw the line(s)
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(x, y, x1, y1);
}
}
}

Your first mistake is using a KeyListener. KeyListener will only respond to key events when the component is registered to is focusable AND has focus.
Your second mistake is not providing size hints for your LinesUsingTheArrowKeys class, so the layout manager has some idea of how big your component should be.
You third mistake is assuming that painting in Swing is accumative, which it is not. Painting in Swing is destructive. That is, each time paintComponent is called, the expectation is that the Graphics context will be cleared and what ever needs to be painted will be completely regenerated.
Take a look at:
How to use Key Bindings
Performing Custom Painting
2D Graphics
Painting in AWT and Swing...because every body that wants to do painting in Swing should know how this works
Basically, a better solution would be to have a List of Point, which the paintComponent would simply either generate a Line between them or even maybe some kind of Polygon or Shape. You would then simply add a new Point to this List as you require and then repaint the component

Related

Transparent Panels

I am making a game where a user has to draw lines so as to make a ball bounce into a target. I'm having trouble getting both the ball and the line to show up concurrently, and I can get only one or the other to appear. It seems to me that the panels block each other out, even though I made them transparent. I would like for them both to appear on the same frame. As of this post, the line panel covers the ball panel.
import javax.swing.Timer;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JPanel;
import javax.swing.JFrame;
public class Game
{
public static void main(String args[]) throws Exception
{
JFrame f = new JFrame("Let's Play");
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
f.setSize(1280, 720);
f.setLocation(300, 300);
f.setResizable(false);
//this part draws a ball that bounces around the screen
BallPanel ballPanel = new BallPanel()
{
// draw rectangles and arcs
public void paintComponent(Graphics g)
{
super.paintComponent(g); // call superclass's paintComponent
g.setColor(Color.red);
// check for boundaries
if (x < radius) dx = Math.abs(dx);
if (x > getWidth() - radius) dx = -Math.abs(dx);
if (y < radius) dy = Math.abs(dy);
if (y > getHeight() - radius) dy = -Math.abs(dy);
// adjust ball position
x += dx;
y += dy;
g.fillOval(x - radius, y - radius, radius*2, radius*2);
}
};
ballPanel.setOpaque(false);
f.add(ballPanel);
//this part allows you to draw lines on the frame with your mouse
JPanel lineP = new JPanel()
{
Point pointStart = null;
Point pointEnd = null;
{
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent me)
{
pointStart = me.getPoint();
}
public void mouseReleased(MouseEvent me)
{
pointStart = null;
}
});
addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseMoved(MouseEvent me)
{
pointEnd = me.getPoint();
}
public void mouseDragged(MouseEvent me)
{
pointEnd = me.getPoint();
repaint();
}
});
}
public void paint(Graphics dline)
{
super.paint(dline);
if (pointStart != null)
{
dline.setColor(Color.RED);
dline.drawLine(pointStart.x, pointStart.y, pointEnd.x, pointEnd.y);
}
}
};
lineP.setOpaque(false); //attempted to enable to see ball panel here
f.add(lineP);
f.setVisible(true);
}
}
class BallPanel extends JPanel implements ActionListener
{
private int delay = 10;
protected Timer timer;
public int x = 30; // x position
public int y = 30; // y position
public int radius = 15; // ball radius
public int dx = 10; // increment amount (x coord)
public int dy = 10; // increment amount (y coord)
public BallPanel()
{
timer = new Timer(delay, this);
timer.start(); // start the timer
}
public void actionPerformed(ActionEvent e)
// will run when the timer fires
{
repaint();
}
}
You've got several issues, but the main one is that you're over-using GUI components. You should have just one single component JPanel that does the drawing, a DrawingPanel, and not a ball panel and a line panel. Rather Ball and Line should be logical classes, not GUI classes, and their display should be in the same single DrawingPanel.
Other issues include:
A main method that has way too much code. Most of that code should be off-loaded into the OOP world where it belongs.
GUI component classes that also implement listener interfaces. This is giving the class too much responsibility making debugging and upgrading difficult. Separate these concerns.
One of your classes overrides the paint method, and this should be avoided. Override paintComponent.
The other class that overrides paintComponent has program logic within paintComponent, and this should be avoided since you have limited control over when or if this method gets called. Get the logic out of that class and into either the mouse listener code or the game loop code (Swing Timer).

How do I create wall barriers or impassable walls in Java?

I'm trying to make impassable walls on the edge of my screen (JFrame). So when I move my image to the left and it touches the left side of the frame, it forces the image to not move. I tried various things, but I just can't seem to find the right code for it, so I'm wondering how to do it based around my code.
import javax.swing.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.net.URL;
public class MovingImages extends JPanel implements KeyListener, ActionListener
{
Timer t = new Timer(5, this);
int x = 0, y = 0; //coordinates for the image
int imageScaleX = 100, imageScaleY = 100; //scale the size of the image
int velX = 0, velY = 0;
//--------------------------------------------------------------------------------------- DISPLAYING IMAGE
public MovingImages()
{
t.start();
addKeyListener(this); //enables the KeyListener so keys can be pressed
setFocusable(true);
}
/** This code is only used for importing the image and runs the program even when there is no image
* #param path is a String that is used to represent the the name or where your file is
* #return is the tempImage which is the image that the program found
*/
public Image getImage(String path)
{
Image tempImage = null;
try
{
URL imageURL = MovingImages.class.getResource(path); //finds where the image is
tempImage = Toolkit.getDefaultToolkit().getImage(imageURL); //loads image from file
}
catch (Exception e)
{
}
return tempImage;
}
/** This code is used to display the image in specified coordinates
* #param g is a variable that uses the Graphics method
*/
public void paint(Graphics g)
{
Image image = getImage("sprite.png"); //choose the file for your image
super.paintComponent(g); //everytime the image moves, it clears the previous image
Graphics2D g2 = (Graphics2D) g; //converts graphics into 2D
g2.drawImage(image, x, y, imageScaleX, imageScaleY, this); //draws image in specific coordinates
}
//--------------------------------------------------------------------------------------- KEYBOARD FUNCTIONS
public void actionPerformed(ActionEvent e)
{
x += velX;
y += velY;
repaint();
}
public void up()
{
velY = -2;
}
public void down()
{
velY = 2;
}
public void left()
{
velX = -2;
}
public void right()
{
velX = 2;
}
public void keyPressed(KeyEvent e)
{
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_UP)
{
up();
}
if (keyCode == KeyEvent.VK_DOWN)
{
down();
}
if (keyCode == KeyEvent.VK_LEFT)
{
left();
}
if (keyCode == KeyEvent.VK_RIGHT)
{
right();
}
}
public void keyTyped(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_UP)
{
velY = 0;
}
if (keyCode == KeyEvent.VK_DOWN)
{
velY = 0;
}
if (keyCode == KeyEvent.VK_LEFT)
{
velX = 0;
}
if (keyCode == KeyEvent.VK_RIGHT)
{
velX = 0;
}
}
//--------------------------------------------------------------------------------------- MAIN
public static void main(String args[])
{
MovingImages s = new MovingImages();
JFrame f = new JFrame();
f.add(s);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(1280, 720);
}
}
The following answer explains the general principle behind forcing objects to stay within their container. Within your program's event loop you are updating an object's x and y coordinates, either directly in response to keyboard input or in a timer loop based on a saved velocity. In either case the basic principle is the same:
Detect when an object's edge bumps up against the container's boundary and don't apply any changes to the object coordinate that would move it so it is partially or completely outside the container.
The following pseudocode describes what has to happen every time you go to update an object's position. I show only the code for horizontal movement, vertical movement is left as an exercise. I assume the object's "position" is the coordinate of the lower left corner of its bounding box.
int left_edge = pos_x;
int right_edge = pox_x + width;
if (velocity_x < 0)
pos_x += left_edge > 0 ? velocity_x : 0;
else if (velocity_x > 0)
pos_x += right_edge < container_width ? velocity_x : 0;
Some questions I have not addressed which are left as an exercise:
Vertical movement
What happens to velocity when the object bumps against a wall. Does (a) the object continue to "try" to move or (b) does the velocity in that direction drop to zero? The first option (a) might apply for instance if there's a barrier of some kind in the middle of the container. The object could bump up against it and stop horizontal movement while still having vertical movement and when eventually clearing the barrier vertically, then continue also moving horizontally.
If velocity > 1 the above code can result in ending up partially outside the container (i.e. you start at x==1 with velocity==-2). You will need to enhance the code for this case, keeping in mind your answer to item 2 above.

Drawing lines with arrow keys and adding each new line in repaint

I'm teaching myself Java programming with a textbook. An exercise asks you to:
Write a program that draws line segments using the arrow keys. The line starts from the center of the frame and draws toward east, north, west, or south when the right-arrow key, up-arrow key, left-arrow key, or down-arrow key is pressed, as shown in Figure 16.22c.
Figure 16.22c shows a frame with one continuous line flowing in the direction of whichever arrow key the user presses. With each press of an arrow key, the line extends in the direction of the arrow key pressed.
I have gotten as far as drawing a single iteration of the a line, but when I press an arrow key, the original line disappears and a new line is drawn. I know why it does this. And I think I know how to fix it. I was thinking of adding each iteration of a lint into an array (with its corresponding points). I haven't done it yet because it would require rewriting so far.
I figured there might be something I missed in my learning about graphics that could help me perform with task without an array. If there is an easier way, can someone explain it to me.
Here is the code I have so far:
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class DrawLinesWithArrowKeys extends JFrame {
DrawLinesPanel panel = new DrawLinesPanel();
/** Constructor */
public DrawLinesWithArrowKeys() {
add(panel);
panel.setFocusable(true);
}
/** Main Method */
public static void main(String[] args) {
JFrame frame = new DrawLinesWithArrowKeys();
frame.setTitle("Draw Lines With Arrow Keys");
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
/** Inner class Draw Lines Panel */
private class DrawLinesPanel extends JPanel {
private int x1Offset = 0;
private int x2Offset = 0;
private int y1Offset = 0;
private int y2Offset = 0;
/* Constructor */
public DrawLinesPanel () {
addKeyListener(new KeyAdapter() {
#Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP) {
y1Offset = y2Offset;
x1Offset = x2Offset;
y2Offset -= 10;
repaint();
}
else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
y1Offset = y2Offset;
x1Offset = x2Offset;
y2Offset += 10;
repaint();
}
else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
x1Offset = x2Offset;
y1Offset = y2Offset;
x2Offset -= 10;
repaint();
}
else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
x1Offset = x2Offset;
y1Offset = y2Offset;
x2Offset += 10;
repaint();
}
}
});
}
/* Paint line */
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(computeXOne(), computeYOne(), computeXTwo(), computeYTwo());
}
private int computeXOne() {
return (getWidth() / 2) + x1Offset;
}
private int computeXTwo() {
return (getWidth() / 2) + x2Offset;
}
private int computeYOne() {
return (getHeight() / 2) + y1Offset;
}
private int computeYTwo() {
return (getHeight() / 2) + y2Offset;
}
}
}
Accumulate your points in a Shape, such as a Polygon or GeneralPath, seen here. You can draw() the current shape in your implementation of paintComponent(). As an alternative to KeyListener, use key bindings, shown here.

Issues with adding images to JFrames

I have been trying to load imageicons into a jframe. I have no idea why this code doesn't work. I have tried using jpanels and jlabels, and also other ways of loading images, but those don't work either. I think because of this it has something to do with my JFrame that I set up . I would like to stay away from jpanels and jlabels, because at least as far as my knowledge goes, they cannot be scaled. If anyone has a solution to adding the images, please tell me. here is the code:
import java.awt.Canvas;
import javax.swing.JFrame;
import java.awt.*;
import javax.swing.ImageIcon;
import java.awt.event.KeyEvent;
import java.awt.Graphics;
public class Base extends Canvas implements Runnable {
private static final long serialVersionUID = 001;
private Image rock1;
private Image rock2;
private Image wood1;
private Thread thread;
private boolean running = (false);
private boolean paused = (false);
int x = 0;
int y = 0;
int z = 512;
private void start() {
if (running)
return;
running = true;
thread = new Thread(this);
}
public void run(){}
public Base(){}
public static void main(String[] Args) {
Base game = new Base();
JFrame frame = new JFrame();
frame.add(game);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
frame.setTitle("Game");
System.out.println("Running...");
game.start();
game.loadPics();
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_A) {
x = x - 5;
}
if (e.getKeyCode() == KeyEvent.VK_D) {
x = x + 5;
}
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
y = y - 5;
}
if (e.getKeyCode() == KeyEvent.VK_SHIFT) {
y = y + 5;
}
if (e.getKeyCode() == KeyEvent.VK_W) {
z = z + 5;
}
if (e.getKeyCode() == KeyEvent.VK_S) {
z = z - 5;
}
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
paused = (true);
}
}
public void loadPics(){
rock1 = new ImageIcon("C\\...\\rock1.png").getImage();
rock2 = new ImageIcon("C\\...\\rock2.png").getImage();
wood1 = new ImageIcon("C\\....\\wood1.png").getImage();
repaint();
}
public void paint(Graphics g){
while(paused = false){
g.drawImage(rock1, x, y, z, z, this);
g.drawImage(rock2, x + 512, y, z, z, this);
g.drawImage(wood1, x, y + 512, z, z, this);
try{
Thread.sleep(16);
}catch(Exception e){}
g.dispose();
}
}
}
Again, I think the problem lies with my JFrame, but I can't be too sure since I am not the most experienced java programmer. Anyway, if you know a solution or what to change, please help.
This statement
while (paused = false){
will always evaluate to false as you're using an assignment expression, so the subsequent calls to drawImage won't occur. You probably meant to use the == operator to compare the primitive boolean value:
while (paused == false){
Don't use paint, Use paintComponent from a subclassed JComponent
Don't call Thread.sleep in any paint method. Swing has its own concurrency mechanisms. Instead of calling Thread.sleep here, you could use a Swing Timer to periodically perform graphical updates.
Aside from that, AWT are heavyweight components are don't render well with lightweight Swing components. Use Key Bindings rather than Key Listeners for Swing applications.
I have had many recurring problems like this and have developed a way to solve this.
First of all, your public class Base needs to be double buffered. Change method paint(Graphics g) to paintComponent(Graphics g). Add a new paint(Graphics g) method and add this code:
public void paint(Graphics g) {
// TODO Auto-generated method stub (IGNORE!!!)
dbi = createImage (getWidth(),getHeight());
dbg = dbi.getGraphics();
paintComponent(dbg);
g.drawImage(dbi , 0 , 0 , this);
}
And at the top of public class Base, add these variables:
private Image dbi;
private Graphics dbg;
This automatically calls paintComponent(Graphics g) and has completed the first step.
Next, remove the try statement in paintComponent() and write this in run()
#Override
public void run() {
try {
while (true) {
Thread.sleep(5); // we want this so the computer doesn't go too fast
}
} catch (Exception e) { // Never have an empty catch statement
System.out.println ("Error! stacktrace: ");
e.printStackTrace();
}
}
The #Override annotation has to be there or it will not repaint.
The next part is crititcal:
before anything else, put super.paint(g); at the top of paintComponent(Graphics g)
and repaint(); at the bottom.
This should have solved it, but there are some potential problems I found;
JFrame initialization
public Base(){
add(game);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 600);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
setTitle("Game");
start();
loadPics();
}
public static void main(String[] Args) { // Makes game smoother
Base base = new Base ();
Thread t = new Thread (base);
t.start();
}
Remove private void start() as this does not affect game play.
Check the file directories, as they may be incorrect. Hint: C\ doesn't cut it. It's C:\
Hope it works out and happy coding!

how to draw rectangle on java applet using mouse drag event

i am using java.
i want to draw rectangle based on mousedrag event. if user dragging the mouse, then the rectangle on the applet should increase or decrease basing on current mouse coordinates.
i have the following code.
in the following code i am using [b]SelectionArea[/b] class which extends a canvas on which i am performing drawing operation. i am using [b]image[/b] variable in this class for double buffering to reduce flickering and to save the applet's previous state(i.e drawing content of applet)
but the code is working fine if i draw first rectangle. if i start to draw second rectangle the previously drawn rectangle is disappearing. i want the previously drawn rectangle to be on the screen
can any one tell me how to solve this.
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
/*
* This displays a framed area. When the user drags within
* the area, this program displays a rectangle extending from
* where the user first pressed the mouse button to the current
* cursor location.
*/
public class RectangleDemo extends Applet {
SelectionArea drawingPanel;
Label label;
public void init() {
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
setLayout(gridBag);
drawingPanel = new SelectionArea(this);
c.fill = GridBagConstraints.BOTH;
c.weighty = 1.0;
c.gridwidth = GridBagConstraints.REMAINDER; //end row
gridBag.setConstraints(drawingPanel, c);
add(drawingPanel);
label = new Label("Drag within the framed area.");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
c.weighty = 0.0;
gridBag.setConstraints(label, c);
add(label);
drawingPanel.setVisible(true);
validate();
}
public void paint(Graphics g){
drawingPanel.repaint();
}
public void update(Graphics g){
paint(g);
}
}
class SelectionArea extends Canvas implements ActionListener, MouseListener, MouseMotionListener{
Rectangle currentRect;
RectangleDemo controller;
//for double buffering
Image image;
Graphics offscreen;
public SelectionArea(RectangleDemo controller) {
super();
this.controller = controller;
addMouseListener(this);
addMouseMotionListener(this);
}
public void actionPerformed(ActionEvent ae){
repaintoffscreen();
}
public void repaintoffscreen(){
image = createImage(this.getWidth(), this.getHeight());
offscreen = image.getGraphics();
Dimension d = getSize();
if(currentRect != null){
Rectangle box = getDrawableRect(currentRect, d);
//Draw the box outline.
offscreen.drawRect(box.x, box.y, box.width - 1, box.height - 1);
//repaint();
}
}
public void mouseEntered(MouseEvent me) {}
public void mouseExited(MouseEvent me){ }
public void mouseClicked(MouseEvent me){}
public void mouseMoved(MouseEvent me){}
public void mousePressed(MouseEvent me) {
currentRect = new Rectangle(me.getX(), me.getY(), 0, 0);
repaintoffscreen();
}
public void mouseDragged(MouseEvent me) {
System.out.println("here in dragged()");
currentRect.setSize(me.getX() - currentRect.x, me.getY() - currentRect.y);
repaintoffscreen();
repaint();
}
public void mouseReleased(MouseEvent me) {
currentRect.setSize(me.getX() - currentRect.x, me.getY() - currentRect.y);
repaintoffscreen();
repaint();
}
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g) {
g.drawImage(image, 0, 0, this);
}
Rectangle getDrawableRect(Rectangle originalRect, Dimension drawingArea) {
int x = originalRect.x;
int y = originalRect.y;
int width = originalRect.width;
int height = originalRect.height;
//Make sure rectangle width and height are positive.
if (width < 0) {
width = 0 - width;
x = x - width + 1;
if (x < 0) {
width += x;
x = 0;
}
}
if (height < 0) {
height = 0 - height;
y = y - height + 1;
if (y < 0) {
height += y;
y = 0;
}
}
//The rectangle shouldn't extend past the drawing area.
if ((x + width) > drawingArea.width) {
width = drawingArea.width - x;
}
if ((y + height) > drawingArea.height) {
height = drawingArea.height - y;
}
return new Rectangle(x, y, width, height);
}
}
also if i run this code on full screen mode then i am seeing that the rectangle is appering on screen only after i released the mouse. but i want the rectangle to be on the screen while dragging the mouse and it should change it's dimension according to the current mouse coordinates.
can any one help me pls.
homework?
basically what you need to do is:
on mouse down keep the mouse-down coordinates and repaint
on mouse move keep current mouse coordinates and repaint
on mouse up, nullify the mouse-down coordinates to indicate there is no rect, and repaint.
on paint, draw background and then rect between mousedown and cur-mouse coordinates.
if you don't want to keep a background image, you can do a trick with the Graphics xor function, drawing the same rect twice will erase the old rect, so you can use it to restore the old image straight on the graphics object.
Edit: code xor usage sample:
public void paint(Graphics g)
{
g.setXORMode(Color.black);
// draw old rect if there is one. this will erase it
// draw new rect, this will draw xored
g.setDrawMode(); // restore normal draw mode
}
Xor has the an interesting property:
xor(xor(x)) = x
so xoring the same pixel twice restores it's original color.
There are a couple issues that need to be addressed.
First, regarding only one rectangle can be drawn, this is due to the design of your program. In your code, whenever the repaintoffscreen method is called, the currectRect field is used to draw a rectangle. However, there is no provision to keep holding onto rectangles which were made in the past.
One way to keep a hold of past rectangles would be perhaps to make another field which is, for example, a List<Rectangle> which is used to store past rectangles. Then, when the mouse is released, add the current rectangle to that list.
Then, in order for all rectangles, currentRect and past rectangles to appear, repaintoffscreen will need to not only perform getDrawableRect and offscreen.drawRect using the currentRect but also with the past rectangles which are stored in the List<Rectangle>. (Hint, use a for loop to iterate through the list.)
Second, regarding the rectangle not appearing until after releasing the mouse button, rather than using the mouseDragged method, maybe using the mouseMoved method along with a check to see that the mouse button is depressed may be a workaround. (I think I've also had trouble dealing with the mouseDragged method in the past.)
The MouseEvent passed into the mouseMoved method can be used to check if a button is depressed by the getButton method:
public void mouseMoved(MouseEvent e)
{
// Check if button1 is pressed.
if (e.getButton() == MouseEvent.BUTTON1)
{
// Perform sizing of rectangle and off-screen drawing, and repaint.
}
}
My question was about create a select rectangle invert mouse click position, but, in the end I got make this with this method:
... //to set the selection area
private int iniSelX;
private int iniSelY;
private int endSelX;
private int endSelY;
private JPanel myJPanel = new JPanel() {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.WHITE);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.red);
g.drawLine(260, 5, 260, 260);
g.setColor(Color.BLUE);
//verify if go draw the rectangle
if (iniSelX != 0 || endSelX != 0) {
boolean revertX = iniSelX < endSelX;
boolean revertY = iniSelY < endSelY;
//Simple way
//g.drawRect(iniSelX, iniSelY, endSelX - iniSelX, endSelY - iniSelY);
//reverse way
g.drawRect(revertX ? iniSelX : endSelX, revertY ? iniSelY : endSelY,
revertX ? endSelX - iniSelX : iniSelX - endSelX, revertY ? endSelY - iniSelY : iniSelY - endSelY);
}
}
}; ...
addMouseMotionListener(new MouseMotionListener() {
#Override
public void mouseDragged(MouseEvent m) {
//update selection area
endSelX = m.getX();
endSelY = m.getY();
repaint();
}
#Override
public void mouseMoved(MouseEvent m) {
repaint();
}
});
addMouseListener(new MouseListener() {
...
#Override
public void mousePressed(MouseEvent e) {
//start drawing the selection
iniSelX = e.getX() - 15;
iniSelY = e.getY() - 20;
}
#Override
public void mouseReleased(MouseEvent e) {
//start drawing the selection
iniSelX = 0;
iniSelY = 0;
endSelX = 0;
endSelY = 0;
}
...
});
}
public void log() {
System.out.println("iniSelX" + iniSelX);
System.out.println("iniSelY" + iniSelY);
System.out.println("endSelX" + endSelX);
System.out.println("endSelY" + endSelY);
} ...
I hope this is useful.

Categories

Resources