Jay Gorio
Jay Gorio

Reputation: 335

Cannot see oval shape moving down the panel

I have this program that when a user click on panel, the oval shape will move downward the panel. I printed the yLocation (location of the oval on y coordinate) using System.out.print and I can see the values changing. But the problem is that I cannot view the oval shape moving.

public class OvalWithThreading {

public OvalWithThreading(){
    SwingUtilities.invokeLater(new Runnable(){

        @Override
        public void run() {
            JFrame frame = new JFrame("Click On The Canvas");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new OvalPane());
            frame.pack() ;
            frame.setVisible(true);
        }

    });
}
public static void main(String[] args) {
    new OvalWithThreading();
}

//Panel that will hold the oval shape
public class OvalPane extends JPanel{

    private int xPosition = 50;
    private int yPosition = 50;
    private int xSize = 50;
    private int ySize = 50;
    boolean clicked = true;
    boolean horizontalBoundary = true;
    boolean verticalBoundary = true;

    private List<Ellipse2D> shapes;

    public OvalPane(){
        shapes = new ArrayList<>();
        setBackground(Color.CYAN);
        addMouseListener(new MouseAdapter(){
            @Override
            public void mouseClicked(MouseEvent e){
                if(clicked) clicked = false;
                else if(!clicked) yPosition = 50; 

                shapes.add(new Ellipse2D.Double(xPosition, yPosition , 50, 50)); 
                System.out.print("Clicked");
                repaint();
            }

       });
   public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();
        for (Ellipse2D shape : shapes) {
            g2d.fill(shape);
            Rectangle2D bounds = shape.getBounds2D();
            double yPos = bounds.getY();
            double xPos = bounds.getX();
            shape.setFrame(xPos, yPos, bounds.getWidth(), bounds.getHeight());
        }
    }

Upvotes: 0

Views: 66

Answers (3)

Jay Gorio
Jay Gorio

Reputation: 335

for(;;){   
if(horizontalBoundary){
    if((yPosition += 30) >= getHeight()-50){
        horizontalBoundary = false;
    }

}else if(!horizontalBoundary){
    if((yPosition -= 30)<= 0){
        horizontalBoundary = true;
    }
}
if(verticalBoundary){
    if((xPosition += 30) >= getWidth()-50){
        verticalBoundary = false;
    }
}else if(!verticalBoundary){
    if((xPosition -= 30) <= 0){
        verticalBoundary = true;
    }
}
System.out.println(yPosition + " " + xPosition);
  repaint();

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347204

The problem is, yPosition has no relationship to the shape once the shape is made (it has to do with the way that Java passes parameters and is a good thing), so no matter how much you change it, it will have no effect on your shapes.

You also have multiple shapes, so you don't want them all to have the same value anyway.

As was outlined in your previous question, you need to iterate over each shape and update it's position, for example...

for (Ellipse2D shape : shapes) {
    Rectangle2D bounds = shape.getBounds2D();
    double yPos = bounds.getY();
    yPos += 30;
    shape.setFrame(bounds.getX(), yPos, bounds.getWidth(), bounds.getHeight());
}

Upvotes: 1

StanislavL
StanislavL

Reputation: 57381

You block Event Dispather Thread by your sleep(). Use javax.swing.Timer instead. In the actionPerformed() change y and call repaint()

UPDATE:

Place the repaint() in SwingUtilities.invokeAndWait() to let EDT perform repaint

Upvotes: 1

Related Questions