Jump to content
  • Advertisement
Sign in to follow this  
thedude018

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

This topic is 2135 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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();
	
}

Share this post


Link to post
Share on other sites
Advertisement

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). 

Edited by molehill mountaineer

Share this post


Link to post
Share on other sites

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();
	}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!