11 Replies - 5155 Views - Last Post: 27 October 2010 - 09:53 PM Rate Topic: -----

#1 BlakeRey  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 23
  • Joined: 08-February 10

My 'Timer' constructor says it's undefined.

Posted 27 October 2010 - 08:32 PM

Hey guys,

Im testing out some Java game programming, and was doing the basics on moving an object. Unfortunately, I struck rock after coding it.

Here is the error code:

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

	at second.<init>(second.java:9)
	at CFrame.main(CFrame.java:8)


Image:
Posted Image

Okay, so I don't know what's happening.

Here is my 2 files, second.java and CFrame.java

second.java
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.util.Timer;
import javax.swing.JPanel;

public class second extends JPanel implements ActionListener, KeyListener {

	Timer t = new Timer(5, this);
	double x = 0, y = 0, velx = 0, vely = 0;

	public second() {
		t.start();
		addKeyListener(this);
		setFocusable(true);
		setFocusTraversalKeysEnabled(false);
	}

	public void paintComponenet(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		g2.fill(new Ellipse2D.Double(x, y, 40, 40));
	}

	public void actionPerformed(ActionEvent e) {
		repaint();
		x += velx;
		y += vely;
	}

	public void up() {
		vely = -1.5;
		velx = 0;
	}

	public void down() {
		vely = 1.5;
		velx = 0;
	}

	public void left() {
		velx = -1.5;
		vely = 0;
	}

	public void right() {
		velx = 1.5;
		vely = 0;
	}

	public void keyPressed(KeyEvent e) {
		int code = e.getKeyCode();
		if (code == KeyEvent.VK_UP) {
			up();
		}
		if (code == KeyEvent.VK_DOWN) {
			down();
		}
		if (code == KeyEvent.VK_LEFT) {
			left();
		}
		if (code == KeyEvent.VK_RIGHT) {
			right();
		}
	}

	public void keyTyped(KeyEvent e) {
	}

	public void keyReleased(KeyEvent e) {
	}

}


and CFrame.java
import javax.swing.JFrame;

public class CFrame {

	public static void main(String args[]) {

		JFrame f = new JFrame();
		second s = new second();
		f.add(s);
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setSize(800, 600);

	}

}




Thank's for your help.

This post has been edited by BlakeRey: 27 October 2010 - 08:41 PM


Is This A Good Question/Topic? 0
  • +

Replies To: My 'Timer' constructor says it's undefined.

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: My 'Timer' constructor says it's undefined.

Posted 27 October 2010 - 08:44 PM

java.util.Timer


That constructor isn't there.
Was This Post Helpful? 0
  • +
  • -

#3 aonbyte  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 26
  • Joined: 08-October 10

Re: My 'Timer' constructor says it's undefined.

Posted 27 October 2010 - 08:56 PM

I'm still learning to program so I would not rely on my information too much. However, I think I'm pretty sure the problem is with how you are creating the object "t" from class Timer.


http://download.orac...util/Timer.html

That's the link to the Timer class documentation.

http://download.orac...structor_detail

If you look there it shows that there are two ways to make Timer objects. One way is Timer() with no parameters or Timer(true) or Timer(False) if the timer's associated thread is to be run as a daemon.

You tried to construct a Timer object by using an integer. I'm not so sure but I don't think that would work. Sorry if I'm unclear.
Was This Post Helpful? 0
  • +
  • -

#4 BlakeRey  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 23
  • Joined: 08-February 10

Re: My 'Timer' constructor says it's undefined.

Posted 27 October 2010 - 08:56 PM

View PostKYA, on 27 October 2010 - 07:44 PM, said:

java.util.Timer


That constructor isn't there.


The constructor is there, at the top of second.java.

This post has been edited by BlakeRey: 27 October 2010 - 08:57 PM

Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: My 'Timer' constructor says it's undefined.

Posted 27 October 2010 - 08:58 PM

No, it's not part of the Timer class, it isn't "there". Your constructor is invalid.
Was This Post Helpful? 0
  • +
  • -

#6 BlakeRey  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 23
  • Joined: 08-February 10

Re: My 'Timer' constructor says it's undefined.

Posted 27 October 2010 - 09:00 PM

View Postaonbyte, on 27 October 2010 - 07:56 PM, said:

I'm still learning to program so I would not rely on my information too much. However, I think I'm pretty sure the problem is with how you are creating the object "t" from class Timer.


http://download.orac...util/Timer.html

That's the link to the Timer class documentation.

http://download.orac...structor_detail

If you look there it shows that there are two ways to make Timer objects. One way is Timer() with no parameters or Timer(true) or Timer(False) if the timer's associated thread is to be run as a daemon.

You tried to construct a Timer object by using an integer. I'm not so sure but I don't think that would work. Sorry if I'm unclear.


Hey, thanks for your help, even if you are still learning, it's still valuable! Thank's for the links, ill have a read of them, but I do need to declare the integer and the this statement within the timer.

I tried using
Timer f = new Timer(5, this);
and then declaring
f.start();
and that doesn't work either, if I got what I thought you was saying by using the 't'?
Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: My 'Timer' constructor says it's undefined.

Posted 27 October 2010 - 09:02 PM

Then you'll need to create a custom class that extends Timer that allows for whatever functionality you're seeking.
Was This Post Helpful? 0
  • +
  • -

#8 BlakeRey  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 23
  • Joined: 08-February 10

Re: My 'Timer' constructor says it's undefined.

Posted 27 October 2010 - 09:03 PM

View PostKYA, on 27 October 2010 - 07:58 PM, said:

No, it's not part of the Timer class, it isn't "there". Your constructor is invalid.


Thanks Einstein, Pardon the pun. XD.

I know my constructor is invalid, I can't make out what you're telling me, can you elaborate on this please?

I don't need a Timer class do I?
Was This Post Helpful? 0
  • +
  • -

#9 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: My 'Timer' constructor says it's undefined.

Posted 27 October 2010 - 09:05 PM

Why do you need a Timer? It would help to illustrate your thought process and ultimate goal.
Was This Post Helpful? 0
  • +
  • -

#10 BlakeRey  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 23
  • Joined: 08-February 10

Re: My 'Timer' constructor says it's undefined.

Posted 27 October 2010 - 09:08 PM

View PostKYA, on 27 October 2010 - 08:05 PM, said:

Why do you need a Timer? It would help to illustrate your thought process and ultimate goal.


The timer is there so that every 5 seconds it call's the ActionListener.
Was This Post Helpful? 0
  • +
  • -

#11 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: My 'Timer' constructor says it's undefined.

Posted 27 October 2010 - 09:11 PM

Use this timer instead
Was This Post Helpful? 0
  • +
  • -

#12 BlakeRey  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 23
  • Joined: 08-February 10

Re: My 'Timer' constructor says it's undefined.

Posted 27 October 2010 - 09:53 PM

I worked it out, using the same Timer method I had. But thanks for your help.

Source?

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Ellipse2D;
import javax.swing.*;

public class second extends JPanel implements ActionListener, KeyListener {

	Timer t = new Timer(5, this);
	double x = 0, y = 0, velx = 0, vely = 0;

	public second() {
		t.start();
		addKeyListener(this);
		setFocusable(true);
		setFocusTraversalKeysEnabled(false);
	}

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

	public void actionPerformed(ActionEvent e) {
		repaint();
		x += velx;
		y += vely;
	}

	public void up() {
		vely = -0.5;
		velx = 0;
	}

	public void down() {
		vely = 0.5;
		velx = 0;
	}

	public void left() {
		velx = -0.5;
		vely = 0;
	}

	public void right() {
		velx = 0.5;
		vely = 0;
	}

	public void keyPressed(KeyEvent e) {
		int code = e.getKeyCode();
		if (code == KeyEvent.VK_UP) {
			up();
		}
		if (code == KeyEvent.VK_DOWN) {
			down();
		}
		if (code == KeyEvent.VK_RIGHT) {
			right();
		}
		if (code == KeyEvent.VK_LEFT) {
			left();
		}
	}

	public void keyTyped(KeyEvent e) {
	}

	public void keyReleased(KeyEvent e) {
	}
}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1