tags:

views:

38

answers:

2

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

I would go with the ifs, but the question is where.

  1. you can add them to paintPanel.paintComponent()
  2. you can add them to movedCircle.paint() and do not draw anything if its coordinates are dummy (for instance <0). The same with 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.)

pajton
For the last solution you provided:That's no different than movedCircle myCircle; even if it was set to movedCircle myCircle = new nullMovedCircle(); The issue is paintComponent will always want do draw what isn't there because of this line "myCircle.paintCircle(g);" this causes an error that oddly enough allows me to run the program but does not draw anything until I preform the required mouse events to draw the desired circle. This is actually where I was planning on placing the bool value check and creating a getDraw() method that returns a bool value from the nested movedCircle class.
pcmike
Well, I was wrong... and glad too. I didn't want to liter my paintComponent with if statements... and they really just didn't seem to belong there imo.Here's the fix: class nullMovedCircle extends movedCircle{ public nullMovedCircle(int x1, int y1, int x2, int y2) { super(x1, y1, x2, y2); } public void paintCircle(Graphics g){} }In short:Thanks!!!
pcmike
You could do that, but as pajton implies it is overkill, an extra class to develop, test and maintain. Best practice should strive to keep code readable, I'd argue that an if !null conveys the intent much more.
vickirk
A: 

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? e.g.

super.paintComponent(g);
myText.paintText(g);
if (myCircle != null) {
    myCircle.paintCircle(g);
}
vickirk
This solution was given when the question was asked. I was merely looking for an alternative or to see if someone knew what was considered "best practice".
pcmike