Alice Community  

Go Back   Alice Community > General Discussion > The Lounge

Reply
 
Thread Tools Display Modes
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, 04: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
Old
arty-fishL
Senior Member
 
arty-fishL's Avatar
 
Status: Offline
Posts: 1,878
Join Date: Mar 2008
Location: In the corner of your eye
Default 11-20-2011, 07:10 PM

I could tell you how to do that in Jython, but I'm still learning Java


█░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█
█░░▓░░░░░░░▓░░░░░░░░░░░▓▓░░▓░░░░░░▓░░░▓░░░░█
█░▓░▓░▓▓▓░▓▓▓░▓░▓░░░░░░▓▒▒░░▒░░▓▓░▓▓▓░▓▒░░░█
█░▓▓▓▒▓▒▒▒░▓▒▒▓▓▓▒▓▓▓░▓▓▓░░▓░░░▓▒▒▓▒▓▒▓▒░░░█
█░▓▒▓▒▓▒░░░▓▓░░▒▓▒░▒▒▒░▓▒▒░▓▓░▓▓▒░▓▒▓▒▓▒░░░█
█░▓▒▓▒░▒░░░░▒▒▓▓▓▒░░░░▓▓▒░░░▒▒░▒▒░░▒░▒▓▓▓░░█
█░░▒░▒░░░░░░░░░▒▒▒░░░░░▒▒░░░░░░░░░░░░░░▒▒▒░█
█░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█

I have mostly moved on from Alice, but may still respond to messages if important [-]
   
Reply With Quote
Old
dubastot
Senior Member
 
dubastot's Avatar
 
Status: Offline
Posts: 661
Join Date: Apr 2009
Location: Two Steps From Hell
Default 11-21-2011, 09:51 AM

I am a bit rusty with this area of Java, but I am pretty sure you are going to need to implement a KeyListener.

Just a quick Java search:

http://docs.oracle.com/javase/tutori...ylistener.html

http://docs.oracle.com/javase/tutori...eybinding.html


I'm a web developer/ designer now.

Last edited by dubastot; 11-21-2011 at 09:54 AM.
   
Reply With Quote
Old
Nighthawk0973
Senior Member
 
Nighthawk0973's Avatar
 
Status: Offline
Posts: 240
Join Date: May 2011
Location: In front of my computer.
Default 11-21-2011, 04:09 PM

ugh. Java Docs. Hate those horribly organized things.

Welp thanks for trying anyways. I'll just leave this thread here in case somebody does help. (I do have other resources to ask at such as the Java Forums...)
   
Reply With Quote
Old
legolizard
Senior Member
 
legolizard's Avatar
 
Status: Offline
Posts: 242
Join Date: Jan 2011
Location: Aboard the Hyperion escaping the zerg.
Default 11-21-2011, 05:01 PM

You have to implement Actionlistener and KeyListener.

So the sig. of your Tuna class should look like this :

Code:
public class Tuna extends JPanel implements ActionListener , KeyListener{
Keep in mind you have to have the appropriate imports to use these interfaces.

Also since they are interfaces you must implement each method that comes with them. For example the KeyListener has three, those being, keyPressed, keyRealeased , and keyTyped and while you may only need keyPressed you still must implement the other two.

Hope this helps!

Also I hate Java doc as well, but it shows how silly the makers of Java are, but then again aren't all programmers a little silly. hehe


"Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe."-Albert Einstein
   
Reply With Quote
Reply

Thread Tools
Display Modes

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 ©2019, Carnegie Mellon University
Alice 2.x 1999-2012, Alice 3.x 2008-2012, Carnegie Mellon University. All rights reserved.