[英]How to draw with MouseListener and MouseMotionListener
I am creating a program that you can draw rectangles within a JPanel using MouseListener and MouseMotionListener however when I run the program I get a nullPointer exception that is caused by not inputing any parameters to draw the rectangle. 我正在创建一个程序,您可以使用MouseListener和MouseMotionListener在JPanel中绘制矩形,但是当我运行该程序时,出现了nullPointer异常,该异常是由于未输入任何参数来绘制矩形而引起的。 The problem lies with how the program does not allow the user to draw the rectangle on the screen to give the parameters for the rectangle. 问题在于程序如何不允许用户在屏幕上绘制矩形以提供矩形的参数。
As of right now I am only trying to draw 1 rectangle but in the end the program will need to draw multiple rectangles so any help in that regard would be great. 截至目前,我仅尝试绘制1个矩形,但最后程序将需要绘制多个矩形,因此在这方面的任何帮助都将非常有用。
Here is the code: 这是代码:
public class RectangleFrame extends JFrame implements ActionListener {
JPanel buttonPanel;
JButton saveImage;
JButton clearImage;
JCheckBox intersections;
JCheckBox union;
JPanel drawingArea;
public RectangleFrame()
{
super();
setTitle("Rectangles");
setSize(600,600);
setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buttonPanel = new JPanel();
buttonPanel.setBorder(BorderFactory.createLineBorder(Color.black));
this.add(buttonPanel, BorderLayout.SOUTH);
intersections = new JCheckBox("Draw Intersections");
buttonPanel.add(intersections);
union = new JCheckBox("Draw Union");
buttonPanel.add(union);
saveImage = new JButton("Save Image");
saveImage.setMargin(new Insets(0,0,0,0));
buttonPanel.add(saveImage);
clearImage = new JButton("Clear Image");
clearImage.setMargin(new Insets(0,0,0,0));
buttonPanel.add(clearImage);
drawingArea = new RectanglePanel();
drawingArea.setBorder(BorderFactory.createLineBorder(Color.blue));
this.add(drawingArea, BorderLayout.CENTER);
drawingArea.setVisible(true);
drawingArea.addMouseListener((MouseListener) drawingArea);
drawingArea.addMouseMotionListener((MouseMotionListener) drawingArea);
}
@Override
public void actionPerformed(ActionEvent arg0)
{
// TODO Auto-generated method stub
}
}
class RectanglePanel extends JPanel implements MouseListener, MouseMotionListener {
Rectangle rectangle;
int x2, y2;
public RectanglePanel()
{
super();
}
@Override
public void mouseDragged(MouseEvent arg0)
{
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent arg0)
{
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent arg0)
{
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0)
{
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0)
{
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0)
{
rectangle.setX(arg0.getX());
rectangle.setY(arg0.getY());
repaint();
}
@Override
public void mouseReleased(MouseEvent arg0)
{
x2 = arg0.getX();
y2 = arg0.getY();
rectangle.setWidth(Math.abs(rectangle.getX() - x2));
rectangle.setHeight(Math.abs(rectangle.getY() - y2));
repaint();
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillRect(rectangle.getX(), rectangle.getY(), rectangle.getWidth(), rectangle.getHeight());
}
}
You need to create a new Rectangle object on mousePressed and then assign it to the rectangle variable. 您需要在mousePressed上创建一个新的Rectangle对象,然后将其分配给矩形变量。 Then you can change its state in the mouseDragged method. 然后,您可以在mouseDragged方法中更改其状态。
Or better, use Point objects that are set on mouse events: 或更妙的是,使用在鼠标事件上设置的Point对象:
ie 即
// variable declarations
Point initialPoint = null;
Rectangle rectangle = null;
@Override
public void mousePressed(MouseEvent mEvt) {
initialPoint = mEvt.getPoint();
rectangle = null;
repaint();
}
mouseDragged(MouseEvent mEvt) {
// use initialPoint, mEvt.getPoint(),
// Math.abs(...), Math.min(...), and Math.max(...)
// to calculate x, y, w, and h
rectangle = new Rectangle(x, y, w, h);
repaint();
}
also in the paintComponent, only draw rectangle if it's not null. 同样在paintComponent中,如果不为null,则仅绘制矩形。
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (rectangle != null) {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLUE);
g2.fill(rectangle);
}
}
As for, 至于,
As of right now I am only trying to draw 1 rectangle but in the end the program will need to draw multiple rectangles so any help in that regard would be great. 截至目前,我仅尝试绘制1个矩形,但最后程序将需要绘制多个矩形,因此在这方面的任何帮助都将非常有用。
This is easy. 这很容易。 Create an ArrayList<Rectangle>
, and on mouseReleased place the created Rectangle object into the List. 创建一个ArrayList<Rectangle>
,然后在mouseReleased上将创建的Rectangle对象放入List中。 In the paintComponent method, iterate through the list with a for loop, drawing each Rectangle that it contains. 在paintComponent方法中,使用for循环遍历列表,绘制其中包含的每个Rectangle。
Check out Custom Painting Approaches for two solutions that allow you to draw Rectangles: 查看“ 自定义绘画方法”中的两个解决方案,这些解决方案使您可以绘制矩形:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.