Alice Community

Alice Community (http://www.alice.org/community/index.php)
-   The Lounge (http://www.alice.org/community/forumdisplay.php?f=4)
-   -   Help in Java? (http://www.alice.org/community/showthread.php?t=7630)

Nighthawk0973 11-20-2011 04:39 PM

Help in Java?
 
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]

[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]

[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;
}
}
[/CODE]

And, don't question the class file names :D (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

arty-fishL 11-20-2011 07:10 PM

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

dubastot 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:

[URL="http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html"]http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html[/URL]

[URL="http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html"]http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html[/URL]

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

legolizard 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{[/code]
Keep in mind you have to have the appropriate imports to use these interfaces. ;)

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

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:cool::cool:


All times are GMT -5. The time now is 01:42 PM.

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