Java drawing rectangle in frame

I am creating a rectangular drawing program. A square is drawn only if the program is dragged to the bottom. Even if dragging in the other direction, I want the squares to be drawn correctly. How can i fix this? Please help me.

**DrawRect.java** import javax.swing.*; import java.awt.*; import java.awt.event.*; public class DrawRect extends JPanel { int x, y, w, h; public static void main(String [] args) { JFrame f = new JFrame("Draw Box Mouse 2"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setContentPane(new DrawRect()); f.setSize(300, 300); f.setVisible(true); } DrawRect() { x = y = w = h = 0; // MyMouseListener listener = new MyMouseListener(); addMouseListener(listener); addMouseMotionListener(listener); } public void setStartPoint(int x, int y) { this.x = x; this.y = y; } public void setEndPoint(int x, int y) { w = Math.abs(this.x - x); h = Math.abs(this.y - y); } class MyMouseListener extends MouseAdapter { public void mousePressed(MouseEvent e) { setStartPoint(e.getX(), e.getY()); } public void mouseDragged(MouseEvent e) { setEndPoint(e.getX(), e.getY()); repaint(); } public void mouseReleased(MouseEvent e) { setEndPoint(e.getX(), e.getY()); repaint(); } } public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.RED); Please help me. g.drawRect(x, y, w, h); } } 
+5
source share
1 answer

Try something like this. you must pinpoint the starting point. The starting point is the minutes x and y of the points of the 1st and last mouse coordinates.

Here are the steps to solve this problem.

  • take the first coordinate when you click x, y
  • take the last coordinates when dragging with the mouse x2, y2
  • take min x and y coordinates as the starting point for drawRect Math.min(x,x2);
  • use the absolute value of the coordinate difference to calculate the height and width of the rectangle. Math.abs(x-x2);

code

 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class DrawRect extends JPanel { int x, y, x2, y2; public static void main(String[] args) { JFrame f = new JFrame("Draw Box Mouse 2"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setContentPane(new DrawRect()); f.setSize(300, 300); f.setVisible(true); } DrawRect() { x = y = x2 = y2 = 0; // MyMouseListener listener = new MyMouseListener(); addMouseListener(listener); addMouseMotionListener(listener); } public void setStartPoint(int x, int y) { this.x = x; this.y = y; } public void setEndPoint(int x, int y) { x2 = (x); y2 = (y); } public void drawPerfectRect(Graphics g, int x, int y, int x2, int y2) { int px = Math.min(x,x2); int py = Math.min(y,y2); int pw=Math.abs(x-x2); int ph=Math.abs(y-y2); g.drawRect(px, py, pw, ph); } class MyMouseListener extends MouseAdapter { public void mousePressed(MouseEvent e) { setStartPoint(e.getX(), e.getY()); } public void mouseDragged(MouseEvent e) { setEndPoint(e.getX(), e.getY()); repaint(); } public void mouseReleased(MouseEvent e) { setEndPoint(e.getX(), e.getY()); repaint(); } } public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.RED); drawPerfectRect(g, x, y, x2, y2); } } 
+4
source

Source: https://habr.com/ru/post/1260751/


All Articles