2

I am trying to learn 2D graphics. This code below draws a couple of spinning wheels. To get them to refresh, I finally inserted the repaint(1000) in the paint method, but I know that this paints at times when it does not have to.

    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;

        for(int angle=0; angle<360; angle+=90){
            g2.setColor(blue);
            g2.fillArc(100,100,200,200,theta1 + angle,45);
            g2.setColor(red);
            g2.fillArc(100,100,200,200,theta1 + angle + 45,45);
        }

        for(int angle=0; angle<360; angle+=30){
            g2.setColor(green);
            g2.fillArc(250,250,250,250,angle + theta2,15);
            g2.setColor(yellow);
            g2.fillArc(250,250,250,250,angle + theta2 + 15,15);
        }

//        repaint(1000);
   }

    public static void main(String s[]) {
        JFrame f = new JFrame("ShapesDemo2D");
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {System.exit(0);}
        });
        JApplet applet = new ShapesDemo2D();
        f.getContentPane().add("Center", applet);
        applet.init();
        f.pack();
        f.setSize(new Dimension(800,800));
        f.setVisible(true);

        while(true) {
            theta2 += 5;
            theta1 -= 2;

            f.repaint(1000);

            try {
                Thread.sleep(100);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    }

What I would really like to do is have it refresh after I have made a change. Main has a reference to paint since it created the applet, but the f.repaint() does not appear to do anything. (If I comment out the repaint() in paint, it does not update). What am I doing wrong?

mKorbel
  • 108,320
  • 17
  • 126
  • 296
mkstlwtz
  • 710
  • 2
  • 6
  • 18
  • 1
    Please have a read of [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html), [Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/) and [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html). I'd also have a look at [How to Use Swing Timers](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) – MadProgrammer Oct 24 '12 at 04:00
  • Applets are suppose to be display in browsers or the applet viewer...why are you trying to display it on a `JFrame` and/or why are you using an `JAppelt` if you already have a frame...?? – MadProgrammer Oct 24 '12 at 04:13

2 Answers2

3

It would be to your advantage to have a read through

I would also have a look at

Which all show animation principles and custom graphics in Swing

Community
  • 1
  • 1
MadProgrammer
  • 323,026
  • 21
  • 204
  • 329
  • I have implemented the Custom Painting demo [link]http://docs.oracle.com/javase/tutorial/uiswing/painting/step3.html[link] and added my graphic objects to paint. I have added the angle variables to the panel which will rotate the wheels. Should I create a thread inside of panel to advance the angles, or could I put it outside in which case, how would i reference the angles? Create a method to set them? – mkstlwtz Oct 24 '12 at 06:06
  • Well it all depends. I would be tempted to only ever have one "animation" thread, as two many will cross over each and you will get conflicts. The problem there is, when and how to update. You shouldn't update the UI from any thread other then the EDT. In that case I might use a detached model that the animation thread would give to the UI that contained the values to updated, but which the UI took a snapshot (so that if the animation thread was updating it, we wouldn't get dirty reads). However, in your case, you could simply get away with using something like a javax.swing.Timer – MadProgrammer Oct 24 '12 at 08:32
  • In the oracle painting demo, the MyPanel constructor sets up the mouse listeners for their square mouse cursor. Their moveSquare uses repaint(). My Runnabble animate is also instantiated by the constructor. It runs and updates the static angle variables in the JPanel, but repaint() is not accepted in my animate even though it is in moveSquare. Putting repaint(1000) in the paint method works, but that's the same problem I had with the applet in the original question. I have leaned a lot, but I can't see where I am making obvious progress. – mkstlwtz Oct 24 '12 at 18:47
  • Of course. How should I send it? Early in the AM down under? – mkstlwtz Oct 24 '12 at 19:03
  • You paste to pastebin or the like. Never early for a 6 month old – MadProgrammer Oct 24 '12 at 19:10
  • OK. Posted as Spinning JFrames on pastebin by mkstlwtz – mkstlwtz Oct 24 '12 at 19:22
  • I'm not able to find the example of pastebin :( – MadProgrammer Oct 25 '12 at 00:23
  • Back again, this time for 24 hours. – mkstlwtz Oct 25 '12 at 01:45
  • Still can't see it :( Even your profile page says you don't have any public pastes...do you have a direct link? – MadProgrammer Oct 25 '12 at 03:00
  • I have tried again. It says that it will be there for 29 more days. – mkstlwtz Oct 25 '12 at 05:10
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/18550/discussion-between-madprogrammer-and-mkstlwtz) – MadProgrammer Oct 25 '12 at 05:35
0

You coded for a JFrame, not a JApplet, although they both can implement WindowListener and they both access the paint(Graphics g) method.