简体   繁体   English

java / Swing问题与paintComponent

[英]java/Swing issue with paintComponent

The issue I'm having is issue with is I'm trying to get the paintComponent to draw the circle only when the mouse is clicked, dragged, then let go. 我遇到的问题是我试图让paintComponent仅在单击,拖动并放开鼠标时绘制圆圈。 However inside my paintPanel class I have to initialize the object I've created (ex. movedCircle myCircle = new movedCircle(0,0,0,0);) just creating the object movedCircle myCircle; 但是在我的paintPanel类中,我必须初始化创建的对象(例如,moveCircle myCircle = new moveCircle(0,0,0,0);),只是创建对象movecircle myCircle;。 gives an error until I actually fully initialize the object with a value. 给出一个错误,直到我用值完全初始化对象为止。

What I'm looking for: What's considered the best practice for this issue. 我在寻找什么:什么是此问题的最佳实践。 I don't want to draw anything unnecessary before it is needed. 我不想在需要之前画任何不必要的东西。

The way I know how to fix it: boolean values inside of paintComponent so that way it doesn't draw until somethings actually there. 我知道如何解决此问题的方法: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);
    }
}

I would go with the if s, but the question is where. 我会使用if ,但是问题是在哪里。

  1. you can add them to paintPanel.paintComponent() 您可以将它们添加到paintPanel.paintComponent()
  2. you can add them to movedCircle.paint() and do not draw anything if its coordinates are dummy (for instance <0). 您可以将它们添加到movedCircle.paint()并且如果其坐标为伪(例如<0),则不绘制任何内容。 The same with movedText movedText相同

Alternatively, position your figures at sensible start place. 或者,将您的图形放置在合理的起点。

(There is one more solution: to subclass movedCircle with nullMovedCircle that doesn't draw anything and create that at first in your paintPanel . However, creating new class for such a little alteration of behaviour seems overkill for me.) (还有另一种解决方案:用nullMovedCircle子类来movedCircle nullMovedCircle子类,该子类不会绘制任何内容,并首先在paintPanel创建它。但是,对于行为的这种小改动创建新类对我来说似乎是过大了。)

Not sure if I missed the point, but if you don't want to draw something until it is initialized then don't! 不知道我是否错过了要点,但是如果您不希望在初始化之前画点东西,那就不要! Can you not just check if it has been created yet rather than creating a new instance that is not in a usable state after the constructor completes? 您不仅可以检查它是否已创建,还可以在构造函数完成后创建一个不处于可用状态的新实例吗? eg 例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM