Solved

Graphics2D ....

Posted on 2004-09-26
4
186 Views
Last Modified: 2010-03-31
Hi Experts,
 
   I tried to make a moving circle, but I got  mistakes at line 00 & line 01 ? Does anyone know what I did wrong ? thanks.
 
//------------------------------------------
import java.lang.* ;
import javax.swing.*;
import javax.swing.event.* ;
import java.awt.* ;
import java.awt.event.* ;
import java.awt.image.BufferedImage;


public class MovingFigs extends JFrame{
    private MovingItem movingCircle, movingRec ;
    /** Creates a new instance of Main */
    public MovingFigs() {
       
       
         javax.swing.Timer circleTimer = new Timer(1000, movingCircle);
         circleTimer.start();
   
      setSize(400,400) ;
      show();
    }
   
    class MovingItem implements ActionListener {
       
          Graphics2D myCircle ;
          int x = 30, y = 30;
          final int DELTA_X =10;
       
          myCircle.setColor(Color.red) ;             // line 00
          myCircle.fillOval(30,30,60,60) ;           //  line 01
          public void actionPerformed(ActionEvent e) {
               x += DELTA_X;
               myCircle.fillOval(x, y, x+60, y);
          }
     }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
    }
   
}
0
Comment
Question by:meow00
  • 3
4 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12154204
If you want to paint on your frame, you'll have to override its paint method:

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class MovingFigs extends JFrame {
      private MovingItem movingCircle, movingRec;
      int x = 30, y = 30;
      final int DELTA_X = 10;

      public MovingFigs() {
            javax.swing.Timer circleTimer = new Timer(500, new MovingItem());
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            circleTimer.start();
            setSize(400, 400);
            show();
      }

      class MovingItem implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                  // Cause the ball to move
                  repaint();
            }
      }


      public void paint(Graphics g) {
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.clearRect(0, 0, getWidth(), getHeight());
            g2d.setPaint(Color.red);
            x += DELTA_X;
            g2d.fillOval(x, y, y, y);
            g2d.drawOval(x, y, y, y);
            g2d.dispose();
      }

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

}
0
 
LVL 1

Author Comment

by:meow00
ID: 12159406
err ..... in the :
-------------------
class MovingItem implements ActionListener {
          public void actionPerformed(ActionEvent e) {
               // Cause the ball to move
               repaint();
          }
     }
-------------------------
why does it call repaint() rather than paint ? thanks !
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 400 total points
ID: 12159448
Repaint makes the component paint itself again without worrying about passing the parameter (Graphics g)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12161999
8-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Eclipse Neon and jdk 1.8.0 11 122
solarwind tftp server 2 44
recursion example 16 111
Java Loop 6 48
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now