Alice Community  

Go Back   Alice Community > General Discussion > The Lounge

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Help in Java?
Old
Nighthawk0973
Senior Member
 
Nighthawk0973's Avatar
 
Status: Offline
Posts: 240
Join Date: May 2011
Location: In front of my computer.
Default Help in Java? - 11-20-2011, 03:39 PM

Finally moving to the realm of Java game design, I found a few troubles. After a good month or so I managed to actually get my Images to draw on the JFrame! But now I need to get the keyinput....

If anybody here knows some Java, I would definately love to get some help with this. Here's the code:

Code:
//Game
//by Nighthawk
//Main.java - app launching point and GUI setup

import javax.swing.*;

public class Main extends JFrame{
	
	Tuna panel = new Tuna();
	
	public Main(){
		
		setTitle("Java Game");
		setSize(800, 600);
		setResizable(false);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		add(panel);
		setVisible(true);
	}
	
	public static void main(String[] args){
		
		Main start = new Main();
	}
}
Code:
//Game
//by Nighthawk
//Apples.java - Player Information

public class Apples{
	
	public int x;
	public int y;
	
	public Apples(){
		
		x = 25;
		y = 75;
	}
	
	public int getX(){
		
		return x;
	}
	
	public int getY(){
		
		return y;
	}
}
Code:
//Game
//by Nighthawk
//Tuna.java - the JPanel that does everything

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Tuna extends JPanel{
	
	public Image player;
	public Image bg;
	public int x;
	public int y;
	public boolean painted = false;
	Apples playerClass = new Apples();
	
	public Tuna(){
		
		ImageIcon img = new ImageIcon("C:/QuickFiles/Java/BG.png");
		bg = img.getImage();
		ImageIcon img2 = new ImageIcon("C:/QuickFiles/Java/Player.png");
		player = img2.getImage();
		setFocusable(true);
		x = getX();
		y = getY();
	}
	
	public void keyPressed(KeyEvent e){
		
		int keycode = e.getKeyCode();
		switch(keycode){
		
		case KeyEvent.VK_LEFT:
			x = x + 1;
			break;
		case KeyEvent.VK_RIGHT:
			x = x - 1;
			break;
		}
		
	}
	
	public void init(){
		
		while(true){
			if(painted == true){
				repaint();
			}
		}
	}
	
	public void paint(Graphics g){
		
		g.drawImage(bg, 0, 0, null);
		g.drawImage(player, x, y, null);
		painted = true;
	}
}
And, don't question the class file names (well actually this is a project that I use in the workspace for various things, naturally having classes that fit one project would confuse me on another one, so I made some names that would make sense across all the projects I do. On a real game I would actually name the class files something reasonable, such as 'JPanel.class'

Thanks for any help,
Nighthawk
   
Reply With Quote
 


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Copyright ©2024, Carnegie Mellon University
Alice 2.x © 1999-2012, Alice 3.x © 2008-2012, Carnegie Mellon University. All rights reserved.