Java 我不知道';我不知道为什么我的对象变为空。另外,为什么repaint()并不总是调用paintComponent()?

Java 我不知道';我不知道为什么我的对象变为空。另外,为什么repaint()并不总是调用paintComponent()?,java,swing,Java,Swing,我试图让用户用鼠标画一个矩形 我有两个问题,我想可能是相关的。首先,我在这里发布的第三个类Colors.java需要使用鼠标侦听器中的同一个矩形对象,这是我提供的第二个类(mouse.java) 我尝试使用getter和setter,当我在调试模式下运行程序时,它在color()方法中有正确的矩形对象,但一旦它转到paintComponent(),我现在有一个空矩形对象。我不知道这是为什么 我遇到的另一个问题是repaint()方法并不总是调用paintComponent()方法。我想这可能是因

我试图让用户用鼠标画一个矩形

我有两个问题,我想可能是相关的。首先,我在这里发布的第三个类Colors.java需要使用鼠标侦听器中的同一个矩形对象,这是我提供的第二个类(mouse.java)

我尝试使用getter和setter,当我在调试模式下运行程序时,它在color()方法中有正确的矩形对象,但一旦它转到paintComponent(),我现在有一个空矩形对象。我不知道这是为什么

我遇到的另一个问题是repaint()方法并不总是调用paintComponent()方法。我想这可能是因为矩形对象为null,但我不确定

我试图缩短代码并用注释替换一些代码。我也不认为包含矩形类是必要的,因为类中的大部分都是与这个问题完全无关的其他函数

如果有人能帮我找到正确的方向,我将不胜感激,因为我现在被卡住了。谢谢

public class JavaSwing extends JFrame implements ItemListener {

//Checkbox objects here

Colors colors = new Colors();

public void createGui() {
    intersection = new JCheckBox("Draw Intersections");
    intersection.setMnemonic(KeyEvent.VK_C);
    intersection.setSelected(false);
    //checkbox objects assigned like above

    //checkbox listeners

    JFrame frame = new JFrame("Rectangles");
    frame.setSize(600, 600);
    frame.setVisible(true);

    Mouse mouse = new Mouse();


    colors.setLayout(new BoxLayout(colors, BoxLayout.PAGE_AXIS));

    colors.addMouseListener(mouse);
    colors.addMouseMotionListener(mouse);
    colors.add(Box.createRigidArea(new Dimension(0, 5)));
    colors.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));


    JButton button = new JButton("Clear Image");

    JPanel panel = new JPanel();

    panel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
    panel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
    panel.add(Box.createHorizontalGlue());
    panel.add(intersection);
    panel.add(Box.createRigidArea(new Dimension(10, 0)));
    panel.add(union);
    panel.add(Box.createRigidArea(new Dimension(10, 0)));
    panel.add(commonArea);
    panel.add(Box.createRigidArea(new Dimension(10, 0)));
    panel.add(button);


    frame.add(colors, BorderLayout.CENTER);
    frame.add(panel, BorderLayout.PAGE_END);

}

@Override
public void itemStateChanged(ItemEvent e) {
  //for checkboxes
}


public static void main(String args[]) {
    JavaSwing javaSwing = new JavaSwing();
    javaSwing.createGui();

}
}
二等舱:

public class Mouse extends MouseInputAdapter implements MouseListener{
Rectangle rectangleOne = new Rectangle(0, 0, 0, 0);

Colors colors = new Colors();

public void mousePressed(MouseEvent e) {
    rectangleOne.setX(e.getX());
    rectangleOne.setY(e.getY());
    rectangleToDraw = new Rectangle(rectangleOne.getX(), rectangleOne.getY(),
            rectangleOne.getWidth(), rectangleOne.getHeight());
    colors.setRectangle(rectangleToDraw);
    colors.color();
}

public void mouseReleased(MouseEvent e) {
    int x2 = e.getX();
    int y2 = e.getY();
    rectangleOne.setWidth(x2 - rectangleOne.getX());
    rectangleOne.setHeight(y2 - rectangleOne.getY());
    rectangleToDraw = new Rectangle(rectangleOne.getX(), rectangleOne.getY(),
            rectangleOne.getWidth(), rectangleOne.getHeight());
    colors.setRectangle(rectangleToDraw);
    colors.color();
}
三等舱:

public class Colors extends JPanel {
Rectangle rectangle;

public void setRectangle(Rectangle rectangle)
{
    this.rectangle = rectangle;
}

public Rectangle getRectangle() {
    return rectangle;
}

public void color() {
    repaint();
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);

    g.setColor(Color.GREEN);
   if (getRectangle() != null) {
       g.fillRect(getRectangle().getX(), getRectangle().getY(), getRectangle().getWidth(), getRectangle().getHeight());
  }
}
}

当您到达
paintComponent
时,
rectangle
的原因是
鼠标中的
颜色与
JavaSwing
中的
颜色不相同。在这两个类中,您都执行
Colors=new Colors()
。这意味着您有两个独立的、不相关的类实例。这也是在调用
repaint()
时,您没有看到重新绘制的原因-您是在一个不属于所显示的实际组件层次结构的组件上调用它

如果更改内容,以便在这两种情况下使用相同的
颜色
实例,它将正常工作。因此,将鼠标的
代码更改为:

public class Mouse extends MouseInputAdapter implements MouseListener{
    Rectangle rectangleOne = new Rectangle(0, 0, 0, 0);
    Colors colors;

    public Mouse(Colors colors){
        this.colors = colors;
    }
    /* The rest of your methods here */
}
然后像这样创建鼠标实例:

Mouse mouse = new Mouse(colors);

另外,虽然我不认为这是您所说的,但我想我应该指出,
repaint()
并不总是导致调用
paintComponent
,即使您在正确的对象上执行该操作。它所做的是请求在事件分派线程下次能够重新绘制组件时重新绘制组件。通常,它会导致调用paintComponent,但并不总是这样(例如,如果组件不可见,或者在重新绘制之前多次调用它,这将导致只绘制一次).

您的
Mouse
类的
Colors
对象实例与您的
JavaSwing
类不同。这可能就是您丢失参考信息的原因。注意:到目前为止,唯一的问题(或者至少是唯一一个在末尾包含问号的问题)是“。为什么repaint()不总是调用paintComponent()?”鉴于这是一个问答网站,如果线程仅限于一个问题,它最适合您、我们以及以后的所有人。请澄清您希望在此问答线程上解决的问题,然后将另一个问题移动到新的问答线程。
g.fillRect(getRectangle().getX(),getRectangle().getY(),getRectangle().getWidth(),getRectangle().getHeight())
图形
转换为
图形2d
,代码行可以简化为<代码>g2.fill(getRectangle())