paintComponent的java / Swing问题

时间:2010-04-06 17:38:51

标签: java swing

我遇到的问题是我试图让paintComponent仅在单击鼠标,拖动鼠标时绘制圆圈,然后放开。但是在我的paintPanel类中,我必须初始化我创建的对象(例如movingCircle myCircle = new movedCircle(0,0,0,0);)只是创建对象movedCircle myCircle;在我用值实际完全初始化对象之前给出错误。

我正在寻找的东西: 什么被认为是这个问题的最佳实践。我不想在需要之前画出任何不必要的内容。

我知道如何修复它的方式: paintComponent中的布尔值,这样它就不会在实际存在的情况下绘制。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class drawCircle extends JFrame{
    private JPanel myPanel = new paintPanel();

    public drawCircle(){
        add(myPanel);
    }

    private class paintPanel extends JPanel{
        private int x1, y1, x2, y2;
        movedText myText = new movedText(0,0,0,0);
        movedCircle myCircle = new movedCircle(0,0,0,0);

        public paintPanel(){
            addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent e){
                    x1 = e.getX();
                    y1 = e.getY();
                    myCircle = new movedCircle(x1, y1, 0, 0);
                    repaint();
                }
                public void mouseReleased(MouseEvent e){
                    x2 = e.getX();
                    y2 = e.getY();
                    myCircle = new movedCircle(x1, y1, x2, y2);
                    repaint();
                }
            });

            addMouseMotionListener(new MouseMotionAdapter(){
                public void mouseDragged(MouseEvent e){
                    x2 = e.getX();
                    y2 = e.getY();
                    myText = new movedText(x1, y1, x2, y2);
                    myCircle = new movedCircle(x1, y1, x2, y2);
                    repaint();
                }
                public void mouseMoved(MouseEvent e){
                    x1 = e.getX();
                    y1 = e.getY();
                    x2 = 0;
                    y2 = 0;

                    myText = new movedText(x1, y1, x2, y2);
                    repaint();
                }
            });
        }

        protected void paintComponent(Graphics g){
            super.paintComponent(g);
            //draw oval after mouse released
            myText.paintText(g);
            myCircle.paintCircle(g);
        }
    }

    class movedCircle{
        private int x1, y1, x2, y2;

        public movedCircle(int x1, int y1, int x2, int y2){
            this.x1 = x1;
            this.y1 = y1;
            this.x2 = x2;
            this.y2 = y2;
        }

        public void paintCircle(Graphics g){
            g.drawOval(x1, y1, x2 - x1, y2 - y1);
        }
    }
    class movedText{
        private int x1, y1, x2, y2;

        public movedText(int x1, int y1, int x2, int y2){
            this.x1 = x1;
            this.y1 = y1;
            this.x2 = x2;
            this.y2 = y2;
        }

        public void paintText(Graphics g){
            g.drawString("x1: "+x1+" y1: "+y1+" x2: "+x2+" y2: "+y2, x1, y1);
        }
    }

    class RedSquare{
        private int xPos = 50;
        private int yPos = 50;
        private int width = 20;
        private int height = 20;

        public void setX(int xPos){ 
            this.xPos = xPos;
        }

        public int getX(){
            return xPos;
        }

        public void setY(int yPos){
            this.yPos = yPos;
        }

        public int getY(){
            return yPos;
        }

        public int getWidth(){
            return width;
        } 

        public int getHeight(){
            return height;
        }

        public void paintSquare(Graphics g){
            g.setColor(Color.RED);
            g.fillRect(xPos,yPos,width,height);
            g.setColor(Color.BLACK);
            g.drawRect(xPos,yPos,width,height);  
        }
    }

    public static void main(String[] args){
        JFrame frame = new drawCircle();

        frame.setTitle("Is in ellipse? Demo");
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

2 个答案:

答案 0 :(得分:0)

我会选择if,但问题是在哪里。

  1. 您可以将它们添加到paintPanel.paintComponent()
  2. 您可以将它们添加到movedCircle.paint(),如果其坐标为虚拟(例如< 0),则不绘制任何内容。与movedText
  3. 相同

    或者,将您的数字放在合理的起点。

    (还有一个解决方案:将movedCirclenullMovedCircle子类化,但不会绘制任何内容,并在paintPanel中首先创建它。但是,为此类创建新类对我来说,行为的改变似乎有点过头了。)

答案 1 :(得分:0)

不确定我是否错过了这一点,但是如果你不想在初始化之前画画,那就不要了!您是否可以检查它是否已经创建,而不是在构造函数完成后创建一个不在可用状态的新实例? e.g。

super.paintComponent(g);
myText.paintText(g);
if (myCircle != null) {
    myCircle.paintCircle(g);
}