簡體   English   中英

我不知道為什么我的對象變成了null。 另外,為什么repaint()並不總是調用paintComponent()?

[英]I don't know why my object is becoming null. Also, why does repaint() not always call paintComponent()?

我試圖讓用戶用鼠標繪制一個矩形。

我有兩個問題,我認為可能與此有關。 首先,我在這里發布的第三個類Color.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());
  }
}
}

究其原因rectangle ,當你達到是空paintComponent是因為colorsMouse不一樣的colorsJavaSwing 在這兩個類中,您都可以使用Colors colors = new Colors() 這意味着你有兩個獨立的,無關的類實例。 這也是你在調用repaint()時沒有看到重繪的原因 - 你在一個不屬於顯示的實際組件層次結構的組件上調用它。

如果您更改了內容以便在兩種情況下都使用相同的Colors實例,它將正常工作。 因此,將Mouse代碼更改為:

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 mouse = new Mouse(colors);

另外,雖然我不認為這就是你所說的,但我想我應該提一下, repaint()並不總是會導致paintComponent被調用,即使你在正確的對象上進行調用也是如此。 它的作用是在下一次事件調度線程時發出重新繪制組件的請求。 通常它會導致paintComponent被調用,但並不總是(例如,如果組件不可見,或者如果在重新繪制它之前多次調用它,這將導致它只被繪制一次)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM