1

I have error with timer, and I don't know where the error is situated in the code.

ERROR:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: The constructor Timer(int, Player) is undefined The method start() is undefined for the type Timer

at Player.(Player.java:12)

at Game.main(Game.java:11)

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.util.Timer;

import javax.swing.*;


public class Player extends JPanel implements ActionListener{
Timer t = new Timer(5, this); // Error (LINE 12)
double x = 0; double velX = 2;
double y = 0; double velY = 2;

public void paintComponent(Graphics g){
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    Ellipse2D circle = new Ellipse2D.Double(x,y,40,40);
    g2.fill(circle);
    t.start(); // error
}

public void actionPerformed(ActionEvent e){
    x += velX;
    y += velY;
    repaint();
  }
}
blackbishop
  • 30,945
  • 11
  • 55
  • 76
Nikola
  • 43
  • 8

3 Answers3

1

You've imported java.util.Timer. Maybe you meant javax.swing.Timer?

You can google for more info, but here is a pretty good explanation of the difference between the two.

Community
  • 1
  • 1
Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
0

Replace

import java.util.Timer;

with

import javax.swing.Timer;

Good luck.

Tanmay Patil
  • 6,882
  • 2
  • 25
  • 45
0

There is no such constructor for the java.util.Timer class, and no such method either.

Ensure that you are importing the correct class in the import declarations above the class header. You may mean java.swing.Timer.

If so, java.swing.Timer won't get imported by java.swing.*, since you have already imported a class of the same name (java.util.Timer). Remove the java.util.Timer import and it should all work.

Source: http://www.tutorialspoint.com/java/util/java_util_timer.htm

SamTebbs33
  • 5,507
  • 3
  • 22
  • 44