2 Replies - 1219 Views - Last Post: 16 February 2012 - 07:03 AM Rate Topic: -----

#1 goddessman3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 16-February 12

The constructor Timer(int, sub) is undefined and other error

Posted 16 February 2012 - 06:48 AM

Alright guys I am fairly new to programming and I would like to know how to fix this error in this Timer. Right now I'm trying to make a image (a square) move in a JFrame but I am getting an error. I can get rid of the Timer but then the square won't move. I have tried so much stuff to try and fix it but I have gotten no results.


Here is my main class

import javax.swing.JFrame;


public class move {
	public static void main(String args[]){
		
		
		
			JFrame frame = new JFrame("hello");
			frame.setSize(1024,542);
			frame.add(new sub(0,0));
			frame.setVisible(true);
	}
		

}


Here is my second class

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Timer;
import javax.swing.*;
import javax.swing.ImageIcon;
import javax.swing.JPanel;



public class sub extends JPanel implements ActionListener, KeyListener{
		private static final long serialVersionUID = 1L;
		
		
		
	private ImageIcon square1 = new ImageIcon("C:\\Documents and Settings\\asdf\\My Documents\\My Pictures\\square.jpg");
	private Image square = square1.getImage();
		
	
		public int x = 0, y = 0, x1 = 0, y1 = 0;
		
	
	
		
		Timer t = new Timer(0,this); // The error is "The constructor Timer(int,sub) is undefined" 
		
		public sub(int startX,int startY){
			
		t.start(); // The error is "The method start() is undefined for the type Timer"
		x = startX;
		y = startY;
		
		addKeyListener(this);
		setFocusable(true);
		setFocusTraversalKeysEnabled(false);
		
		}
		
		
	protected void paintComponent(Graphics g){
		super.paintComponents(g);
		
		if (square1 != null){
			g.drawImage(square, x, y, 111, 101, this);
		}
	}
	
	public void actionPerformed(ActionEvent e){
		repaint();
		
		x += x1;
		y += y1;
		
	}
	
	
		public void moveRight(){
			x1 = 2;
			y1 = 0;
		}
		public void moveLeft(){
			x1 = -2;
			y1 = 0;
		}
		
	public void moveUp(){
		x1 = 0;
		y1 = -2;
	}
	public void moveDown(){
		x1 = 0;
		y1 = 2;
	}
		public void release(){
			x1 = 0;
			y1 = 0;
		}
		public void keyPressed(KeyEvent e){
			int key = e.getKeyCode();
			if (key == KeyEvent.VK_DOWN){
				moveDown();
			}
			if (key == KeyEvent.VK_LEFT){
				moveLeft();
			}
			if (key == KeyEvent.VK_RIGHT){
				moveRight();
			}
			if (key == KeyEvent.VK_UP){
				moveUp();
			}
		}
			public void keyTyped(KeyEvent e1){
				int key1 = e1.getKeyCode();
				
				if(key1 == KeyEvent.VK_SPACE){
					moveUp();}
				}
			public void keyReleased(KeyEvent e1){
				int key1 = e1.getKeyCode();
				if (key1 == KeyEvent.VK_DOWN){
					release();
				}
				if (key1 == KeyEvent.VK_LEFT){
					release();
				}
				if (key1 == KeyEvent.VK_RIGHT){
					release();
				}
				if (key1 == KeyEvent.VK_UP){
				}
				}
		}



Is This A Good Question/Topic? 0
  • +

Replies To: The constructor Timer(int, sub) is undefined and other error

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2101
  • View blog
  • Posts: 3,203
  • Joined: 21-June 11

Re: The constructor Timer(int, sub) is undefined and other error

Posted 16 February 2012 - 06:55 AM

There are multiple classes named Timer in the Java standard library. The one you imported (java.util.Timer) can't be used in the way you try to use it. You want to import the one from swing instead.
Was This Post Helpful? 1
  • +
  • -

#3 goddessman3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 16-February 12

Re: The constructor Timer(int, sub) is undefined and other error

Posted 16 February 2012 - 07:03 AM

View Postsepp2k, on 16 February 2012 - 06:55 AM, said:

There are multiple classes named Timer in the Java standard library. The one you imported (java.util.Timer) can't be used in the way you try to use it. You want to import the one from swing instead.


Alright it worked thank you very much. There is one problem that I have though with it it doesn't get rid of the square when it repaints I will have to figure that out. But thank you very much so.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1