• Post Reply Bookmark Topic Watch Topic
  • New Topic

animation  RSS feed

 
Carolin Sha
Ranch Hand
Posts: 68
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

Please see my code below:



I am getting the following errors when I compile:

\MovingCircleGUI.java:20: error: <identifier> expected
public int -velX;
^
\MovingCircleGUI.java:20: error: <identifier> expected
public int -velX;
^
2 errors

any help would appreciated with the errors.
Also my circle is not being animated. the circle is supposed to move across the draw panel until it touches the opposite side, then it moves back until it again touches the side it started at. the circle is supposed to keep going across and back again.

thanks
 
Liutauras Vilda
Marshal
Posts: 4513
308
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carolin Sha wrote:any help would appreciated with the errors.
Please read this short paragraph about variables naming, right at the bottom.
In short: you cannot start variable name with dash "-".
 
Carolin Sha
Ranch Hand
Posts: 68
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

Thanks I got it. Please see updated code.

[code=java]

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.*;
import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.Graphics;
import javax.swing.Timer;


public class MovingCircleGUI{
JFrame frame;
JButton button;
Timer timer;
public int x,y;
public int diameter=100;
public int xVel;
public int velX;
CircleDrawPanel drawPanel;
Color color = Color.orange;


public static void main (String[] args){
MovingCircleGUI gui = new MovingCircleGUI();
gui.go();
}

//this method sets up the JFrame and adds the drawpanel to the frame
public void go(){
frame = new JFrame();
JButton button = new JButton("Click me to start the animation");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

button.addActionListener(new AnimateCircleListener());

drawPanel = new CircleDrawPanel();
frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
frame.getContentPane().add(BorderLayout.SOUTH, button);
frame.setSize(500,500);
frame.setVisible(true);


}
public class AnimateCircleListener implements ActionListener {


Timer timer = new Timer (5,this);
int x = 0, velX = 2;


} //close inner class

class CircleDrawPanel extends JPanel{
public void paintComponent (Graphics g){

super.paintComponent(g);

Graphics2D g2=(Graphics2D)g;
g2.setColor(Color.white);
g2.fillRect(0,0,this.getWidth(), this.getHeight());
g2.setColor(color);
g2.fillOval(x,50,100,100);
timer.start();
}
public void actionPerformed(ActionEvent event) {
if ( x< 0 || x > 400)
velX = -velX;

x = x + velX;
repaint();

}
}
}

when I compile the above code I am getting this error and my animation does not work.

\MovingCircleGUI.java:48: error: MovingCircleGUI.AnimateCircleListener is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener
public class AnimateCircleListener implements ActionListener {
^
1 error
 
Carolin Sha
Ranch Hand
Posts: 68
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry uploading code again.



getting error code and my animation is not working.

\MovingCircleGUI.java:48: error: MovingCircleGUI.AnimateCircleListener is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener
public class AnimateCircleListener implements ActionListener {
^
1 error
 
Swastik Dey
Rancher
Posts: 1805
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Error itself is giving you the description. Look at the code



you are implementing ActionListener but not implementing it's method actionPerformed.
 
Carolin Sha
Ranch Hand
Posts: 68
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

I made some changes to the code again. no compile errors, however when i hit on "click me to start animation" getting these errors and circle does not move.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at MovingCircleGUI$AnimateCircleListener.actionPerformed(MovingCircleGUI.java:50)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3322)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:751)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:702)
at java.awt.EventQueue$3.run(EventQueue.java:696)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:724)
at java.awt.EventQueue$4.run(EventQueue.java:722)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:721)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)



 
Fred Kleinschmidt
Bartender
Posts: 556
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have made "button: a local variable in method "go". This hides the class instance variable, which remains null.
 
Fred Kleinschmidt
Bartender
Posts: 556
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You also have declared extraneous variables x, velX, and timer in your AnimateCircleListener class.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!