Jump to content

  • Log In with Google      Sign In   
  • Create Account


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 thedude018   Members   -  Reputation: 100

Like
-1Likes
Like

Posted 15 September 2013 - 03:19 PM

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



Sponsor:

#2 thok   Members   -  Reputation: 665

Like
0Likes
Like

Posted 15 September 2013 - 03:51 PM

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

#3 molehill mountaineer   Members   -  Reputation: 593

Like
0Likes
Like

Posted 15 September 2013 - 03:54 PM

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, 15 September 2013 - 05:38 PM.


#4 ScottK   Members   -  Reputation: 255

Like
2Likes
Like

Posted 15 September 2013 - 06:14 PM

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





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS