Reputation: 39
How do I fix this error?
error: expected timer.start()
import java.awt.event.ActionEvent ;
import java.awt.event.ActionListener ;
import javax.swing.JFrame ;
import javax.swing.Timer ;
/**
This class illustrates using a Timer with a listener
The listener illustrates using an interface.
The program also illustrates that the inner class has access
to constants in the local environment, but not variables.
This is an example from the book, so the answer is there.
Your job is
1. write the line that makes the Rectangle component,
remembering that it will have to be a constant.
2. write the listener so that every time the timer ticks, it
moves the rectangle in the component by (dx, dy) = (DELTA, DELTA).
*/
public class RectangleComponentViewerWithTimer
{
private static final int FRAME_WIDTH = 300 ;
private static final int FRAME_HEIGHT = 400 ;
private static final int DELTA = 2 ;
private static final int LIMIT = 30 ;
private static final int INTERVAL = 400 ;
public static void main(String[] args)
{
//-----------Start below here. To do: approximate lines of code = 12
// 1. create the RectangleComponent1 object called component.
//Hint: component will be used in the inner class
RectangleComponent1 component = new RectangleComponent1();
//1. define class TimerListener, an ActionListener
class TimerListener implements ActionListener
{//
//2. instance variable count starting at zero ;
int count = 0;
//3. actionPerformed method signature ;
public void actionPerformed(ActionEvent event)
{//
//4. increment count ;
count++;
//5. tell component to moveBoxBy DELTA and DELTA ;
component.moveBoxBy(DELTA, DELTA);
//6. when count reaches the LIMIT
if (count == LIMIT)
//7. print component.toString() ;
System.out.println(component.toString());
//8. stop the application
System.exit(0);
}//
}//
}//
//9. make the listener
ActionListener listener = new TimerListener();
//10. make the timer based on INTERVAL and listener
Timer timer = new Timer(INTERVAL, listener);
//11. start the timer
timer.start() ;
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
JFrame frame = new JFrame() ;
frame.add(component) ;
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT) ;
frame.setTitle("Animated rectangle.") ;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
frame.setVisible(true) ;
}
}
RectangleComponentViewerWithTimer.java:58: error: <identifier> expected
timer.start() ;
^
RectangleComponentViewerWithTimer.java:61: error: <identifier> expected
frame.add(component) ;
^
RectangleComponentViewerWithTimer.java:61: error: <identifier> expected
frame.add(component) ;
^
RectangleComponentViewerWithTimer.java:62: error: <identifier> expected
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT) ;
^
RectangleComponentViewerWithTimer.java:62: error: <identifier> expected
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT) ;
^
RectangleComponentViewerWithTimer.java:62: error: <identifier> expected
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT) ;
^
RectangleComponentViewerWithTimer.java:63: error: <identifier> expected
frame.setTitle("Animated rectangle.") ;
^
RectangleComponentViewerWithTimer.java:63: error: illegal start of type
frame.setTitle("Animated rectangle.") ;
^
RectangleComponentViewerWithTimer.java:64: error: <identifier> expected
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
^
RectangleComponentViewerWithTimer.java:64: error: <identifier> expected
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
^
RectangleComponentViewerWithTimer.java:65: error: <identifier> expected
frame.setVisible(true) ;
Upvotes: 2
Views: 1222
Reputation: 285405
You look to be messing yourself with poor code formatting. Match your curly braces and you'll see that you have some code outside of a method.
Your formatting is causing you to think you have a curly brace where you don't:
if (count == LIMIT) // ***** no curly brace here *****
//7. print component.toString() ;
System.out.println(component.toString());
//8. stop the application
System.exit(0);
} // **** this does not belong to the if block!
Because of this, all code from ActionListener listener = new TimerListener();
on down is sitting naked in the class, outside of any method or constructor.
Key point: code formatting isn't about pretty code, it's about debuggable code.
Other recs:
Upvotes: 6