java/Swing issue with paintComponent

Posted by user310254 on Stack Overflow See other posts from Stack Overflow or by user310254
Published on 2010-04-06T17:38:51Z Indexed on 2010/04/06 19:13 UTC
Read the original article Hit count: 259

Filed under:
|

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. 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; 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.

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);
    }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about swing