Reputation: 1
basically this code needs to be able to draw a rectangle with 2-point clicks from one corner to the other which then draws the rectangle according to the distance of the 2 click. Right now it works but its dragging system. Can anyone help?
package javaio;
import java.awt.*;
import java.awt.event.;
import javax.swing.*;
public class DrawingProgram extends JPanel implements ActionListener
{
MouseHandler mouseHandler = new MouseHandler();
Point p1 = new Point(0, 0);
Point p2 = new Point(0, 0);
boolean drawing;
public DrawingProgram(){
this.setPreferredSize(new Dimension(1000, 1000));
this.addMouseListener(mouseHandler);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawRect(p1.x, p1.y, p2.x, p2.y);
repaint();
}
private class MouseHandler extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
drawing = true;
p1 = e.getPoint();
p2 = p1;
repaint();
}
public void mouseReleased(MouseEvent e) {
drawing = false;
p2 = e.getPoint();
repaint();
}
}
public void actionPerformed(ActionEvent e) {
JFrame f = new JFrame("Draw Rectangle On Mouse Click");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new DrawingProgram());
f.pack();
f.setVisible(true);
}
}
For it to be able to draw a rectangle with 2-point clicks.
Upvotes: 0
Views: 313
Reputation: 347184
The basic idea is pretty simple. You have to keep track of the first click and the second click. When both are not null
, you need to create a rectangle from them.
This example demonstrates not only that, but also how you might provide a visual feedback of the shape creation through the use of a MouseMotionListener
, if you don't want it, it's not hard to get rid of
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private Point startPoint;
private Rectangle2D rectangle;
private Point currentPoint;
public TestPane() {
MouseAdapter mouseAdapter = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (startPoint != null && rectangle != null) {
startPoint = e.getPoint();
rectangle = null;
} else if (startPoint == null) {
startPoint = e.getPoint();
} else {
// You could use a List of some kind to
// keep track of all the shapes you've created
Point endPoint = e.getPoint();
rectangle = makeRectangle(startPoint, endPoint);
}
repaint();
}
@Override
public void mouseMoved(MouseEvent e) {
currentPoint = e.getPoint();
repaint();
}
};
addMouseListener(mouseAdapter);
addMouseMotionListener(mouseAdapter);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
protected Rectangle2D makeRectangle(Point startPoint, Point endPoint) {
int minX = Math.min(startPoint.x, endPoint.x);
int minY = Math.min(startPoint.y, endPoint.y);
int maxX = Math.max(startPoint.x, endPoint.x);
int maxY = Math.max(startPoint.y, endPoint.y);
return new Rectangle2D.Double(minX, minY, maxX - minX, maxY - minY);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
if (rectangle != null) {
g2d.draw(rectangle);
} else if (startPoint != null && currentPoint != null) {
// These are guide lines, you can get rid of them if you prefer
g2d.setColor(Color.LIGHT_GRAY);
g2d.drawLine(startPoint.x, startPoint.y, currentPoint.x, currentPoint.y);
g2d.draw(makeRectangle(startPoint, currentPoint));
}
g2d.dispose();
}
}
}
I would consider taking a look at:
Upvotes: 1