Can't find the issue in my javagame.java class file?

Started by
2 comments, last by sakocs 10 years, 7 months ago

I can't find/solve the bugs with this code here. There are a lot of red "!"s meaning something is immensely wrong.


 package javagame;
 
 import java.awt.Graphics;
 import java.awt.event.KeyAdapter;
 import java.awt.KeyEvent;
 import javax.swing.JFrame;

 
 public class javagame extends JFrame {
 
	int x, y;
 
	public class AL extends Keyadapter {
		public void keyPressed (KeyEvent e) {
			int keyCode = e.getKeyCode();
			if(keyCode == e.VK_LEFT)
				x--;
			}
			if(keyCode == e.VK_RIGHT}
				x++;
			}	
			if(keyCode == e.VK_UP)
				y--;
			}	
			if(keyCode == e.VK_DOWN)
				y++;
			}		
		}
		public void keyReleased(KeyEvent e) {
			
		}
	}
 {
	public JavaGame(){
		addKeyListener(new AL();
		setTitle("Java Game");}
		setSize(250, 250);
		setResizable(false);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
		x = 150;
		y = 150;
	
 }
	public void paint(Graphics g) 
		g.fillOval(x, y, 15, 15)
	
	
		repaint();
	
	public static void main(String[] args) 
		new JavaGame();
	
}

Advertisement
You have not provided sufficient information for anyone to help you, short of copying your code and debugging it themselves (and that actually wouldn't be helping you). You haven't said anything about your environment or any specific error messages you are seeing.

And at risk of being "that guy", please read this: http://www.catb.org/esr/faqs/smart-questions.html

Hi, welcome to the forums. It would be helpful if you could tell us what errors the compiler is outputting, that will make it easier for us to debug your code. Copy paste the ouput of the "console" tab at the bottom (might be "output", I can't remember exactly) or mouse over the exclamation marks to see an error message for that specific line of code.

EDIT: your main and paint functions are missing a body (that is, { and } brackets).

Well I can tell you one thing your code needs a LOT of work. There were so many small mistakes in it. Like improper brackets {}, spelling, semi-colons ; and more.

I fixed all of the errors and this will compile BUT it will not do anything at all...

My suggestion is that you do things in smaller steps

1: learn how to create a window

2: learn how to make an object

3:learn how to make it move.

Also FIX ERRORS (when possible) AS THEY APPEAR IN THE IDE! Don't let them stack up you see a red ! or line or whatever in your code work to fix it.


package javagame;

import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
	 
@SuppressWarnings("serial")
public class JavaGame extends JFrame{
	
public int x, y;
	 
	public class AL extends KeyAdapter {
			
		public void keyPressed (KeyEvent e) {

			int keyCode = e.getKeyCode();
			
			if(keyCode == e.VK_LEFT){
				x--;
			}
			if(keyCode == e.VK_RIGHT){
				x++;
			}	
			if(keyCode == e.VK_UP){
				y--;
			}	
			if(keyCode == e.VK_DOWN){
				y++;
			}		
		}
			
		public void keyReleased(KeyEvent e) {
				
		}

		
	 
		public void JavaGame(){
			
			AL key = new AL();
			
			addKeyListener(key);
			setTitle("Java Game");
			setSize(250, 250);
			setResizable(false);
			setVisible(true);
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
			x = 150;
			y = 150;
		}
		
		public void paint(Graphics g){
			g.fillOval(x, y, 15, 15);
		
		
			repaint();
		}
	}
	
	public static void main(String[] args){
		new JavaGame();
	}
}

This topic is closed to new replies.

Advertisement