0

I have the following code that draws an invisible window on the entire screen:

Window w=new Window(null)
    {
      private int x = 200; private int y=200;
      private int dx = 2; private int dy = 2;
      private final int CIRCLE_DIAMETER = 400;
      @Override
      public void paint(Graphics g)
      {  
          g.setColor(Color.ORANGE);
          g.fillOval(x, y, CIRCLE_DIAMETER, CIRCLE_DIAMETER);

      }
      @Override
      public void update(Graphics g)
      {
            if(x<=0)
                dx*=-1;
            if(y<=0)
                dy*=-1;
            if(x+CIRCLE_DIAMETER>=this.getWidth())
                dx*=-1;
            if(y+CIRCLE_DIAMETER>=this.getHeight())
                dy*=-1;

            x+=dx;
            y+=dy;

            this.paint(g);
        }
    };
    w.setAlwaysOnTop(true);
    w.setBounds(w.getGraphicsConfiguration().getBounds());
    w.setBackground(new Color(0, true));
    w.setVisible(true);
            //Lazy way of making calls to paint for testing
    while(true){
        w.repaint();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

This draws an orange circle on the screen at the coordinates x and y. When I call repaint in my infinite loop, paint gets called and the x and y get updated, but the circle never gets drawn in another position. If I print the values of x and y to every call of paint they are getting updated properly, so I do not know why it isn't getting drawn. Does anyone know what I am doing wrong here?

Thanks for any suggestions!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Zach
  • 1,311
  • 3
  • 16
  • 36
  • Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. Instead of calling `Thread.sleep(n)` implement a Swing `Timer` for repeating tasks . See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more details. – Andrew Thompson Mar 29 '14 at 06:31
  • Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. – Andrew Thompson Mar 29 '14 at 06:33

1 Answers1

2

I am new here so I may be wrong.

  • I think that your problem is something to do with how you are using a Window object and not a JPanel. So change your Window object to a JPanel. You should probably close that up with a JFrame in order to complete the final window. You should be using a JPanel I think so that the methods that you can use to perform the drawing of the ball to move are implemented properly.

  • Instead of overriding the paint() method you need to override the paintComponent() method. Following the cycle of drawing your objects.

Like so...

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.ORANGE);
                g.fillOval(x, y, CIRCLE_DIAMETER, CIRCLE_DIAMETER);
            }

The super.paintComponent() should empty the original image out of the JPanel and you should then be able to draw your updated image.

These may help you as well(I haven't really looked at them properly):

Java ball moving

http://docs.oracle.com/javase/tutorial/uiswing/painting/

Java Bouncing Ball

Sorry if I missed something. (I haven't tested your code)

Community
  • 1
  • 1
BoydyBoydy
  • 159
  • 1
  • 9