我正在创建一个程序,您可以使用JPanel和MouseListener和MouseMotionListener绘制矩形,但是,当我运行该程序时,会得到一个nullPointer异常,它是由于不输入任何参数来绘制矩形而导致的。问题在于程序如何不允许用户在屏幕上绘制矩形以提供矩形的参数。
到目前为止,我只想画一个矩形,但在最后,程序将需要绘制多个矩形,所以在这方面的任何帮助将是伟大的。
以下是代码:
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());
}
}发布于 2014-02-28 22:55:09
您需要在mousePressed上创建一个新的矩形对象,然后将它分配给矩形变量。然后可以在mouseDragged方法中更改其状态。
或者更好的是,使用设置在鼠标事件上的Point对象:
即
// 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();
}同样在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);
}
}至于,
到目前为止,我只想画一个矩形,但在最后,程序将需要绘制多个矩形,所以在这方面的任何帮助将是伟大的。
这很简单。创建一个ArrayList<Rectangle>,并在mouseReleased上将创建的矩形对象放入列表中。在paintComponent方法中,使用for循环遍历列表,绘制其中包含的每个矩形。
发布于 2014-03-01 03:34:37
查看定制绘画方法,获得两种允许您绘制矩形的解决方案:
https://stackoverflow.com/questions/22107266
复制相似问题