mouseMoved not called while mousePressed has been called - java

My mouseMoved simply doesn't get called when mousePressed has been called, but it gets called normally when mousePressed didn't. If I move my mouse while I press a mouse button mouseMoved doesn't get called.
package src.game.main.gui_hud;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;
import javax.swing.SwingUtilities;
import src.game.main.Game;
public class Slider {
private Color lc,hc,fc;
private int x,y;
private int w,h;
private Runnable change;
private int lineY;
private double value = 100;
private volatile boolean canMove;
public Slider(Color bgColor,Color filledColor,Color handlerColor,Runnable onValueChange,int x,int y,int w,int h,int lineY) {
setLc(bgColor);
setHc(handlerColor);
setFc(filledColor);
change = onValueChange;
this.x = x;
this.y = y;
this.w = w;
this.h = w;
this.lineY = lineY;
}
public void render(Graphics gt) {
Graphics2D g = (Graphics2D) gt.create(x, y, w, h);
g.setColor(getLc());
g.fillRoundRect(10, y/2-lineY, w-10, lineY, 10, 10);
g.setColor(getFc());
g.fillRoundRect(10, y/2-lineY, (int) ((value*w)/100)-10, lineY, 10, 10);
g.setColor(getHc());
g.fillRoundRect((int)((value*w)/100)-6, y/2-20, 5, 30, 10, 10);
}
public void tick() {
value = Game.clamp(value, 0, 100);
System.out.println(canMove);
}
public void mousePressed(MouseEvent e) {
Point p = e.getPoint();
if (new RoundRectangle2D.Double(x+ ((int)((value*w)/100)-6), y + (y/2-20), 5, 30, 10, 10).contains(p)) {
canMove = SwingUtilities.isLeftMouseButton(e);
}
}
public void mouseReleased(MouseEvent e) {
Point p = e.getPoint();
canMove = false;
}
public void mouseMoved(MouseEvent e) {
System.out.println(e.getX());
Point p = e.getPoint();
if(canMove) System.out.println("LOL");
}
public Color getHc() {
return hc;
}
public Slider setHc(Color hc) {
this.hc = hc;
return this;
}
public Color getLc() {
return lc;
}
public Slider setLc(Color lc) {
this.lc = lc;
return this;
}
public int getX() {
return x;
}
public Slider setX(int x) {
this.x = x;
return this;
}
public int getY() {
return y;
}
public Slider setY(int y) {
this.y = y;
return this;
}
public int getW() {
return w;
}
public Slider setW(int w) {
this.w = w;
return this;
}
public int getH() {
return h;
}
public Slider setH(int h) {
this.h = h;
return this;
}
public double getValue() {
return value;
}
public Slider setValue(double v) {
this.value = v;
return this;
}
public Color getFc() {
return fc;
}
public Slider setFc(Color fc) {
this.fc = fc;
return this;
}

Based on the code in the mousePressed handler, you're trying to do some sort of drag-rectangle, although I can't be certain.
The mouseMoved messages will keep happening until you get a mousePressed event. Once you've received the mousePressed event you will then start to receive mouseDragged events until the receipt of a mouseReleased event. After that point you will start to receive mouseMoved events again.
This is intended to allow differentiating between just moving the mouse and dragging with one of the buttons pressed.

Related

Trying to create Circles using MouseListener and MouseMotionListener - what I am doing wrong?

I'm trying to create Circles in JFrame using JComponent. Here's what I'm trying to achieve:
And here's what's happening with my code:
I have no idea what's causing this problem. Here's my code:
CircleViewer.java
import javax.swing.JFrame;
import java.awt.event.*;
public class CircleViewer
{
public static void main(String[] args)
{
final CirclePanel panel = new CirclePanel();
class MousePressListener implements MouseListener, MouseMotionListener
{
public void mouseClicked(MouseEvent event) { }
public void mouseEntered(MouseEvent event) { }
public void mouseExited(MouseEvent event) { }
public void mouseWheelMoved(MouseWheelEvent event) { }
public void mouseMoved(MouseEvent event) { }
public void mousePressed(MouseEvent event)
{
var x = event.getX();
var y = event.getY();
panel.addCircle(x, y);
}
public void mouseDragged(MouseEvent event)
{
var x = event.getX();
var y = event.getY();
panel.moveTo(x, y);
}
public void mouseReleased(MouseEvent event)
{
panel.finalMove();
}
}
MousePressListener listener = new MousePressListener();
panel.addMouseListener(listener);
panel.addMouseMotionListener(listener);
JFrame frame = new JFrame("Circle Shapes");
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.setVisible(true);
}
private static final int FRAME_WIDTH = 700;
private static final int FRAME_HEIGHT = 500;
}
CirclePanel.java
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import java.util.ArrayList;
import java.awt.Color;
import java.lang.Math;
import java.awt.BasicStroke;
import java.awt.Stroke;
public class CirclePanel extends JComponent
{
private int lineX;
private int lineY;
private boolean isDraged;
private ArrayList<Circle> circleList;
private BasicStroke dashLine;
public CirclePanel()
{
this.circleList = new ArrayList<Circle>();
this.isDraged = false;
this.lineX = 0;
this.lineY = 0;
this.dashLine = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{6}, 0);
}
public void addCircle(int x, int y)
{
lineX = x;
lineY = y;
isDraged = true;
circleList.add(new Circle(x, y, 0, Color.RED));
repaint();
}
public void moveTo(int x, int y)
{
var circleTemp = circleList.get(circleList.size() - 1);
isDraged = true;
var tempR = (int)Math.sqrt(Math.pow(x - circleTemp.get(0), 2) + Math.pow(y - circleTemp.get(1), 2));
System.out.println(tempR);
var tempX = circleTemp.get(0) - (tempR / 2);
var tempY = circleTemp.get(1) - (tempR / 2);
circleList.get(circleList.size() - 1).setCords(tempX, tempY, tempR);
lineX = x;
lineY = y;
repaint();
}
public void finalMove()
{
isDraged = false;
circleList.get(circleList.size() - 1).setColor(Color.BLUE);
repaint();
}
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
Stroke defaultStroke;
defaultStroke = g2.getStroke();
if (!circleList.isEmpty())
{
if (isDraged)
{
g2.setColor(Color.RED);
g2.setStroke(dashLine);
g2.drawLine(circleList.get(circleList.size() - 1).get(0), circleList.get(circleList.size() - 1).get(1), lineX, lineY);
}
for (Circle circle : circleList)
{
g2.setStroke(defaultStroke);
circle.draw(g2);
isDraged = false; //this is prob reduntant
}
}
}
}
And finally, Circle.java
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
public class Circle
{
private int x;
private int y;
private int radius;
private Color color;
public Circle(int x, int y, int radius, Color color)
{
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
}
public int get(int option)
{
switch (option)
{
case 0:
return this.x;
case 1:
return this.y;
case 2:
return this.radius;
}
return 0;
}
public Color get()
{
return this.color;
}
public void setCords(int x, int y, int r)
{
this.x = x;
this.y = y;
this.radius = r;
}
public void set(int option, int value)
{
switch (option)
{
case 0: //set x
this.x = value;
break;
case 1:
this.y = value;
break;
case 2:
radius = value;
break;
}
}
public void setColor(Color color)
{
this.color = color;
}
public void draw(Graphics2D g2)
{
g2.setColor(color);
g2.draw(new Ellipse2D.Double(x, y, radius, radius));
}
}
Rather than finding the specific problem in you code, I just decided to make a cleaner implementation. Using this circle class
class Circle {
final int x;
final int y;
int radius;
public Circle(int x, int y, int radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
}
You can implement CirclePanel like this:
public class CirclePanel extends JPanel {
private static final Stroke DASHED = new BasicStroke(1,
BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL,
0, new float[]{6}, 0);
public Color oldCircleColor = Color.BLUE;
public Color newCircleColor = Color.RED;
public Color backgroundColor = Color.LIGHT_GRAY;
private final List<Circle> oldCircles = new ArrayList<>();
private Circle newCircle = null;
private int mouseX = 0;
private int mouseY = 0;
private class MouseHelper implements MouseListener, MouseMotionListener {
#Override public void mouseMoved(MouseEvent e) {}
#Override public void mouseClicked(MouseEvent e) {}
#Override public void mousePressed(MouseEvent e) {}
#Override public void mouseEntered(MouseEvent e) {}
#Override public void mouseExited(MouseEvent e) {}
#Override
public void mouseDragged(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
if (newCircle == null) {
newCircle = new Circle(mouseX, mouseY, 0);
} else {
int dX = newCircle.x - mouseX;
int dY = newCircle.y - mouseY;
newCircle.radius = (int) Math.sqrt(dX*dX + dY*dY);
}
repaint();
}
#Override
public void mouseReleased(MouseEvent e) {
if (newCircle != null) {
oldCircles.add(newCircle);
newCircle = null;
repaint();
}
}
}
public CirclePanel() {
MouseHelper helper = new MouseHelper();
addMouseListener(helper);
addMouseMotionListener(helper);
setPreferredSize(new Dimension(400, 400));
}
#Override
public void paint(Graphics g) {
g.setColor(backgroundColor);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(oldCircleColor);
for (Circle c : oldCircles) {
drawCircle(g, c);
}
Circle c = newCircle;
if (c != null) {
g.setColor(newCircleColor);
drawCircle(g, c);
Graphics2D g2 = (Graphics2D) g.create();
g2.setStroke(DASHED);
g2.drawLine(c.x, c.y, mouseX, mouseY);
g2.dispose();
}
}
private void drawCircle(Graphics g, Circle c) {
// note: drawOval takes top-left corner and diameter, NOT center and radius
g.drawOval(c.x - c.radius, c.y - c.radius, c.radius * 2, c.radius * 2);
}
}
And test to see that it works
public static void main(String[] args) {
JFrame frame = new JFrame();
CirclePanel panel = new CirclePanel();
frame.add(panel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
Like #SirLenz0rlot mentioned in the comments there is a bug in your moveTo method. The problem is the call circleList.get(circleList.size() - 1).setCords(tempX, tempY, tempR); which is setting the position of the circle to tempX, tempY, but you probably only want to set the radius of the circle, while the coords are not changed.
Changing the moveTo method to the code below should help:
public void moveTo(int x, int y)
{
var circleTemp = circleList.get(circleList.size() - 1);
isDraged = true;
var tempR = (int)Math.sqrt(Math.pow(x - circleTemp.get(0), 2) + Math.pow(y - circleTemp.get(1), 2));
System.out.println(tempR);
var tempX = circleTemp.get(0) - (tempR / 2);
var tempY = circleTemp.get(1) - (tempR / 2);
//EDITED HERE
circleList.get(circleList.size() - 1).setCords(circleTemp.get(0), circleTemp.get(1), tempR);//using getX() and getY() on your circle would be easier to understand here, but this should also work...
lineX = x;
lineY = y;
repaint();
}
For some reason your circleTemp.get(0) and (1) doesn't give you the (x,y) coordinates
circleTemp.get(0)
What you should do is save the circle's (x,y) coordinates, and use them in the moveTo method.
Correct Change
And in the end for the draw line, you should again, use the saved (x,y) coordinates and the new coordinated of the mouse. (not the circleList.size() -1).get(0) - Line 67)
(Ex. I added the drawLineX and drawLineY)

Detecting Collision between two filled Rectangles

I'm trying to make it print out "Game over" when the Green Square(Cuboid) runs over/into the blue(CuboidKiller) one.
GAME class:
package plugin.dev.wristz;
import java.awt.Graphics;
import java.awt.Image;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
public class Game extends JFrame {
private static final long serialVersionUID = 294623570092988970L;
public static ArrayList<CuboidKiller> killers;
public static int h = 1024, w = 768;
public static Game game;
public static Graphics graphics, g2;
public static Image image;
public static Cuboid cuboid;
public Game(String title) {
setTitle(title);
setSize(1024, 768);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(true);
setVisible(true);
setLocationRelativeTo(null);
addKeyListener(new KeyHandler(cuboid));
g2 = getGraphics();
paint(g2);
}
public static void main(String[] args) {
cuboid = new Cuboid();
Thread cubi = new Thread(cuboid);
cubi.start();
killers = new ArrayList<CuboidKiller>();
CuboidKiller a = new CuboidKiller(new Random().nextInt(h), new Random().nextInt(w), new Random().nextInt(50) + 20);
killers.add(a);
game = new Game("Killer Cuboids");
}
#Override
public void paint(Graphics g) {
image = createImage(getWidth(), getHeight());
graphics = image.getGraphics();
paintComponent(graphics);
g.drawImage(image, 0, 0, this);
}
public void paintComponent(Graphics g) {
checkGameOver();
cuboid.draw(g);
for (CuboidKiller killer : killers)
killer.draw(g);
repaint();
}
public void checkGameOver() {
for (CuboidKiller killer : killers)
if (killer.isTouching(cuboid))
System.out.println("Game over!");
}
public int getH() {
return h;
}
public void setH(int wh) {
h = wh;
}
public int getW() {
return w;
}
public void setW(int ww) {
w = ww;
}
}
Cuboid class:
package plugin.dev.wristz;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
#SuppressWarnings("static-access")
public class Cuboid implements Runnable {
private int x, y, xDirection, zDirection;
public Cuboid() {
this.x = 799;
this.y = 755;
}
public void draw(Graphics g) {
g.setColor(Color.GREEN);
g.fillRect(x, y, 25, 25);
}
public void move() {
x += xDirection;
y += zDirection;
if (x <= 10)
x = 0 + 10;
if (y <= 35)
y = 0 + 35;
if (x >= 1024 - 35)
x = 1024 - 35;
if (y >= 768 - 35)
y = 768 - 35;
}
public void keyPressed(KeyEvent ev) {
int keyCode = ev.getKeyCode();
if (keyCode == ev.VK_LEFT) {
setXDirection(-5);
}
if (keyCode == ev.VK_RIGHT) {
setXDirection(5);
}
if (keyCode == ev.VK_UP) {
setZDirection(-5);
}
if (keyCode == ev.VK_DOWN) {
setZDirection(5);
}
}
public void keyReleased(KeyEvent ev) {
int keyCode = ev.getKeyCode();
if (keyCode == ev.VK_LEFT) {
setXDirection(0);
}
if (keyCode == ev.VK_RIGHT) {
setXDirection(0);
}
if (keyCode == ev.VK_UP) {
setZDirection(0);
}
if (keyCode == ev.VK_DOWN) {
setZDirection(0);
}
}
#Override
public void run() {
try {
while (true) {
move();
Thread.sleep(5);
}
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getXDirection() {
return xDirection;
}
public void setXDirection(int xDirection) {
this.xDirection = xDirection;
}
public int getZ() {
return y;
}
public void setZ(int z) {
this.y = z;
}
public int getZDirection() {
return zDirection;
}
public void setZDirection(int zDirection) {
this.zDirection = zDirection;
}
}
Cuboid Killer:
package plugin.dev.wristz;
import java.awt.Color;
import java.awt.Graphics;
import java.util.HashMap;
public class CuboidKiller {
private int x, y, radius;
private HashMap<Integer, Integer> points;
public CuboidKiller(int x, int y, int radius) {
this.points = new HashMap<Integer, Integer>();
setPoints();
this.x = x;
this.y = y;
this.radius = radius;
}
public void draw(Graphics g) {
g.setColor(Color.blue);
g.fillRect(x, y, radius, radius);
}
public void setPoints() {
this.points.put(x, y);
this.points.put(x + radius, y);
this.points.put(x + radius, y - radius);
this.points.put(x, y - radius);
}
public boolean isTouching(Cuboid cuboid) {
boolean result = true;
//int a = cuboid.getX(), b = cuboid.getZ();
result = true;
return result;
}
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public HashMap<Integer, Integer> getPoints() {
return points;
}
public void setPoints(HashMap<Integer, Integer> points) {
this.points = points;
}
}
Well, there are two approaches. Either you write it yourself, or you just use what Java 8 provides.
This guy has a very nice explanation on how to detect collision between two rectangles: Java check if two rectangles overlap at any point
But if I were the one writing it, I would just have both classes contain a Rectangle object (http://docs.oracle.com/javase/8/docs/api/java/awt/Rectangle.html), and just call the intersects() function provided by Rectangle. :-)

Why won't my KeyPressed method run

I am trying to write a game in which a character moves around and jumps from block to block in order to get to the end. But the problem I'm facing is that my KeyPressed method won't run. I've tried putting a System.out.println("Hi"); in the method to see if it even starts running. But the "Hi" never showed up. This is my code.
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Driver extends Applet implements KeyListener
{
private int X = 0;
private int Y = 250;
private int sizeX = 25;
private int sizeY = 25;
private boolean start=false;
public int getX()
{
return X;
}
public void setX(int x)
{
X = x;
}
public int getY()
{
return Y;
}
public void setY(int y)
{
Y = y;
}
public int getsizeY()
{
return sizeY;
}
public void setsizeY(int sizey)
{
sizeY = sizey;
}
public int getsizeX()
{
return sizeX;
}
public void setsizeX(int sizex)
{
sizeX = sizex;
}
public void init()
{
this.addKeyListener(this);
setSize(300, 300);
setFocusable(false);
requestFocus();
}
public void paint(Graphics g)
{
init();
map map1 = new map();
g.setColor(Color.WHITE);
g.fillRect(0, 0, 300, 300);
g.setColor(Color.black);
map1.paint(g);
g.fillRect(X, Y, sizeX, sizeY);
start=true;
}
public void keyTyped(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
}
public void keyPressed(KeyEvent e)
{
if(start)
{
System.out.println("Hi");
if(e.getKeyChar() == 'w')
{
System.out.println("Hi");
setY(getY()-1);
}
if(e.getKeyChar() == 'd')
{
setX(getX()+1);
}
if(e.getKeyChar() == 'a')
{
setX(getX()-1);
}
if(e.getKeyChar() == 's')
{
setY(getY()+1);
}
repaint();
}
}
}
I am still learning how to code so please don't be mean, but if you could help that would be great!

Tic Tac Toe game in Java using MouseAdapter

I have a CLickableBox class that creates boxes for me and now I need to make it so that when clicked, either an X or an O will be displayed in place. Here is the ClickableBox class.
import java.awt.event.MouseAdapter;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.Container;
public class ClickableBox extends MouseAdapter {
private int x, y, width, height;
private Color borderColor, backColor, oldColor;
private boolean drawBorder, clicked;
private Container parent;
public ClickableBox(int x, int y, int width, int height, Color borderColor,
Color backColor, boolean drawBorder, Container parent) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.borderColor = borderColor;
this.backColor = backColor;
this.drawBorder = drawBorder;
this.parent = parent;
}
public void draw(Graphics g) {
oldColor = g.getColor();
g.setColor(backColor);
g.fillRect(x, y, width, height);
if(drawBorder) {
g.setColor(borderColor);
g.drawRect(x, y, width, height);
}
g.setColor(oldColor);
}
public void mouseReleased(MouseEvent e) {
if(x < e.getX() && e.getX() < x + width &&
y < e.getY() && e.getY() < y + height) {
clicked = true;
parent.repaint();
}
}
public boolean isClicked() {
return clicked;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public Color getBorderColor() {
return borderColor;
}
public void setBorderColor(Color borderColor) {
this.borderColor = borderColor;
}
public Color getBackColor() {
return backColor;
}
public void setBackColor(Color backColor) {
this.backColor = backColor;
}
public Color getOldColor() {
return oldColor;
}
public void setOldColor(Color oldColor) {
this.oldColor = oldColor;
}
public boolean isDrawBorder() {
return drawBorder;
}
public void setDrawBorder(boolean drawBorder) {
this.drawBorder = drawBorder;
}
public void setClicked(boolean clicked) {
this.clicked = clicked;
}
}
The TicTacToeBox class should extend ClickableBox, so that each box will be a listener. It needs to be designed so that each Box object will take care of itself- it knows if it's been clicked or not, and if so, whether it's going to be showing an x or an o.
The TicTacToeBox class is what I am having trouble with. This is what I will need for my game board. Any suggestions on how to implement this, simply? Below is my TicTacToeBox so far (not much):
Some direction and/or assistance would be greatly appreciated! Thanks.
import java.awt.Color;
import java.awt.Container;
public class TicTacToeBox extends ClickableBox {
public TicTacToeBox(int x, int y, int width, int height, Color borderColor,
Color backColor, boolean drawBorder, boolean mask, Container parent)
{
super(x, y, width, height, borderColor, backColor, drawBorder, parent);
}
}
Perhaps you need to override mouseReleased() - something like this:
public void mouseReleased(MouseEvent e) {
if ( this.value == NONE ) {
if ( currentTurn == Turn.X ) {
this.value = X;
}
else {
this.value = O;
}
}
super.mouseReleased();
}
With some global currentTurn variable to keep track of whose turn it is, and a value field to represent what the current value of this box is. You'd also probably want to override draw() to make it render the "X" or "O"...

Drawing a Rectangle using MouseListener & MouseMotionListener

I've created two packages, Display and paint_core, which contain five Java files: Displayable.java, Drawing.java, NewCordinates.java, OldCordinates.java and Main.java.
I am facing some coordinates issues and the output is not coming as desired. What is wrong with my program?
Main.java
package paint_core;
import Display.Displayable;
public class Main {
public Main() {
}
public static void main(String[] args) {
new Displayable();
}
}
Displayable.java
package Display;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
public class Displayable extends JFrame implements MouseListener,MouseMotionListener{
Drawing dr;
int x,y;
OldCordinates op;
public Displayable()
{
setVisible(true);
dr = new Drawing();
add(dr);
dr.addMouseListener(this);
dr.addMouseMotionListener(this);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void mouseDragged(MouseEvent e)
{
NewCordinates np = new NewCordinates();
np.setX(x);
np.setY(y);
np.setW(e.getX()-x);
np.setH(e.getY()-y);
op= dr.setXY(np.getX(),np.getY(),np.getW(),np.getH(),Color.RED,op);
}
public void mouseMoved(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
}
public void mousePressed(MouseEvent e) {
x=e.getX();
y=e.getY();
op = new OldCordinates();
op.setX(x);
op.setY(y);
op.setW(x);
op.setH(y);
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
Drawing.java
package Display;
import java.awt.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Drawing extends JPanel
{
private int x,y,w,h;
Color r;
OldCordinates op;
public OldCordinates setXY(int x,int y,int w,int h,Color r,OldCordinates op)
{
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.r = r;
this.op = op;
//System.out.println("Old Cordinates" + op.getW()+" "+op.getY());
repaint();
op.setH(h);
op.setW(w);
//System.out.println("New Cordinates" + w+" "+h);
return op;
}
#Override
public void paint(Graphics g) {
g.drawRect(op.getX(),op.getY(),op.getW(),op.getH());
g.setColor(r);
g.drawRect(x,y,w,h);
}
}
OldCordinates.java
package Display;
import java.awt.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Drawing extends JPanel
{
private int x,y,w,h;
Color r;
OldCordinates op;
public OldCordinates setXY(int x,int y,int w,int h,Color r,OldCordinates op)
{
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.r = r;
this.op = op;
//System.out.println("Old Cordinates" + op.getW()+" "+op.getY());
repaint();
op.setH(h);
op.setW(w);
//System.out.println("New Cordinates" + w+" "+h);
return op;
}
#Override
public void paint(Graphics g) {
//g.drawRect(op.getX(),op.getY(),op.getW(),op.getH());
g.setColor(r);
g.drawRect(x,y,w,h);
}
}
NewCordinates.java
package Display;
public class NewCordinates {
private int x;
private int y;
private int w;
private int h;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getW() {
return w;
}
public void setW(int w) {
this.w = w;
}
public int getH() {
return h;
}
public void setH(int h) {
this.h = h;
}
}
You should override paintComponent instead of paint. If this is not your problem, please give more details

Categories

Resources