我正在创建一个程序,您可以使用MouseListener和MouseMotionListener在JPanel中绘制矩形但是当我运行程序时,我得到一个nullPointer异常,这是由于没有输入任何参数来绘制矩形引起的。问题在于程序如何不允许用户在屏幕上绘制矩形以给出矩形的参数。
截至目前,我只想绘制1个矩形,但最后程序需要绘制多个矩形,因此在这方面的任何帮助都会很棒。
以下是代码:
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());
}
}
答案 0 :(得分:3)
您需要在mousePressed上创建一个新的Rectangle对象,然后将其分配给矩形变量。然后,您可以在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);
}
}
至于,
截至目前,我只想绘制1个矩形,但最后程序需要绘制多个矩形,因此在这方面的任何帮助都会很棒。
这很容易。创建一个ArrayList<Rectangle>
,并在mouseReleased上将创建的Rectangle对象放入List中。在paintComponent方法中,使用for循环遍历列表,绘制它包含的每个Rectangle。
答案 1 :(得分:2)
查看Custom Painting Approaches以获得两个允许您绘制矩形的解决方案: