meow00
asked on
Graphics2D ....
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.BufferedIma ge;
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.re d) ; // line 00
myCircle.fillOval(30,30,60 ,60) ; // line 01
public void actionPerformed(ActionEven t 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
}
}
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.BufferedIma
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.re
myCircle.fillOval(30,30,60
public void actionPerformed(ActionEven
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
}
}
ASKER
err ..... in the :
-------------------
class MovingItem implements ActionListener {
public void actionPerformed(ActionEven t e) {
// Cause the ball to move
repaint();
}
}
-------------------------
why does it call repaint() rather than paint ? thanks !
-------------------
class MovingItem implements ActionListener {
public void actionPerformed(ActionEven
// Cause the ball to move
repaint();
}
}
-------------------------
why does it call repaint() rather than paint ? thanks !
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
8-)
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(J
circleTimer.start();
setSize(400, 400);
show();
}
class MovingItem implements ActionListener {
public void actionPerformed(ActionEven
// 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();
}
}