1 Replies - 174 Views - Last Post: 16 February 2012 - 06:51 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:50 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 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1131
  • View blog
  • Posts: 2,484
  • Joined: 05-May 05

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

Posted 16 February 2012 - 06:51 AM

*Please don't open duplicate topics.*

This post has been edited by blackcompe: 16 February 2012 - 06:51 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1