Author Topic: Adding a keyEvent  (Read 1773 times)

0 Members and 1 Guest are viewing this topic.

Offline m1kesanders

  • Serf
  • *
  • Posts: 48
  • Cookies: -5
  • I'm sarcastic, somewhat new, and a grey hat
    • View Profile
Adding a keyEvent
« on: March 14, 2014, 01:13:13 am »
Hey guys I'm trying to create a game in Java is for a final in my Computer Science class, and I was wondering what I'm doing wrong with my keyEvent I basically just wand it to shift my GoodGuy up using W, all the images are initialized properly, well anyone here's the code for my WizardGrid class

Thanks  :)

Code: (javascript) [Select]
import java.awt.*;
import java.awt.event.KeyEvent;

import javax.swing.JLabel;
import javax.swing.JPanel;


@SuppressWarnings("serial")

public class WizardBattleGrid extends GameWindowWizard {
private static JLabel[][] WizardCells = new JLabel [20][20];
private static int pos1 = 0;
private static int pos2 = 0;
private static JPanel WizardPanel = new JPanel(new GridLayout(20, 20, 0, 0));



public static void CreateGrid(){


for(int i=0; i<WizardCells.length; i++ ){
for(int j=0; j<WizardCells[i].length; j++){

JLabel label = new JLabel();
WizardCells[i][j] = label;
}
}

for(int i=0; i<WizardCells.length; i++){
for(int j=0; j<WizardCells[i].length;j++){


WizardPanel.add(WizardCells[i][j]);


}

}

WizardCells[pos1+10][pos2].setIcon(GoodGuyWizard());
WizardCells[pos1+10][pos2+19].setIcon(BadGuyWizard());





Window.add(WizardPanel);

}

public static void MoveGoodGuyWizard(KeyEvent e){

int key = e.getKeyCode();

if (key == KeyEvent.VK_W){
WizardCells[pos1+11][pos2].setIcon(GoodGuyWizard());

}



}



}





Thanks, I realized my problem I forgot to cal the method (idiot move haha) anyway I guess my next question is how do I call a keyEvent into and ActionEvent, heres my code for the mainmenu

Code: (javascript) [Select]
import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;




@SuppressWarnings("serial")
public class GameWindowWizard extends JFrame {

final static JFrame Window = new JFrame();
static JButton Start = new JButton("Start");
static JButton Exit = new JButton("Exit");
static JPanel MainMenuButtons = new JPanel();
private static JLabel WizardBackground = new JLabel();


public static ImageIcon BadGuyWizard(){

ImageIcon BadGuy = new ImageIcon("resources/badguynobackground.png");

return BadGuy;
}


public static ImageIcon GoodGuyWizard(){

ImageIcon GoodGuy = new ImageIcon("resources/goodguynobackground.png") ;

return GoodGuy;

}


public static Image createimgpng(){



BufferedImage WizardBattleIcon = null;

try{
File wizardBattleIconFile = new File("resources/goodguynobackground.png");
WizardBattleIcon = ImageIO.read(wizardBattleIconFile);
ImageIO.write(WizardBattleIcon, "png", wizardBattleIconFile);
   
}
catch(IOException e){
e.printStackTrace();
}



return WizardBattleIcon;


}

public static JLabel createimgjpg(){

BufferedImage MainMenuBackground = null;

try{
File MainMenuBackgroundFile = new File("resources/WizardBattleBackground.jpg");
MainMenuBackground = ImageIO.read(MainMenuBackgroundFile);
ImageIO.write(MainMenuBackground, "jpg", MainMenuBackgroundFile);

}
catch(IOException e){
e.printStackTrace();
}

        WizardBackground = new JLabel(new ImageIcon(MainMenuBackground));


return WizardBackground;



}
/*
public static void destroyImageJpg(JLabel WizardBackground, BufferedImage MainMenuBackground){

WizardBackground.remove(MainMenuBackground);



}
*/





public static void MainMenu(){


Window.setSize(640, 480);
Window.add(MainMenuButtons);
Window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Window.setLocationRelativeTo(null);
Window.setTitle("Wizard Battle");
MainMenuButtons.add(Start);
MainMenuButtons.add(Exit);
Window.getContentPane().add(MainMenuButtons, BorderLayout.SOUTH);
Window.setIconImage(createimgpng());
Window.add(createimgjpg());
Window.setVisible(true);
Window.setResizable(false);

Exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){

Window.dispose();

}



});

Start.addActionListener(new ActionListener(){
@SuppressWarnings("static-access")
public void actionPerformed(ActionEvent e){

Window.setExtendedState(Window.MAXIMIZED_BOTH);
Window.remove(WizardBackground);
Window.remove(MainMenuButtons);
WizardBattleGrid.CreateGrid();
WizardBattleGrid.MoveGoodGuyWizard((KeyEvent)e);

}
});
}
}

« Last Edit: March 14, 2014, 03:18:35 am by m1kesanders »

Offline Matriplex

  • Knight
  • **
  • Posts: 323
  • Cookies: 66
  • Java
    • View Profile
Re: Adding a keyEvent
« Reply #1 on: March 14, 2014, 01:24:04 am »
1. What graphics library?
2. If Java2D, porque??
3. Why would your player ever move if the static method MoveGoodGuyWizard ever be called? There's nothing that calls it, and if there is we haven't a clue where. Methods aren't called magically.
4. What's WizardGrid?
5. If you have some sort of InputListener class, show it to us.

Give us more information, with the tiny bit that you've given us now we can tell you practically nothing.
\x64\x6F\x75\x65\x76\x65\x6E\x00

Offline m1kesanders

  • Serf
  • *
  • Posts: 48
  • Cookies: -5
  • I'm sarcastic, somewhat new, and a grey hat
    • View Profile
Re: Adding a keyEvent
« Reply #2 on: March 14, 2014, 03:19:37 am »
Thanks Matriplex I edited my question above and the graphics is just sprites created through paint and put on through ImageIcon

Offline Matriplex

  • Knight
  • **
  • Posts: 323
  • Cookies: 66
  • Java
    • View Profile
Re: Adding a keyEvent
« Reply #3 on: March 14, 2014, 04:24:01 pm »
You didn't answer 1,2, and 4. 2 doesn't really matter but still. I'm guessing you're using Java2D as well.

For a video game you're going to want a seperate inputlistener class to store all your input listenening functions. I'm not going to show you how to do this or program the input for you, you can find all that online very easily. This isn't the classroom anymore, you're not copying down stuff from a book.

As a side note try and make your code a little more organized. Group similar calls together and comment your code if you like. It helps, trust me.
\x64\x6F\x75\x65\x76\x65\x6E\x00

Offline m1kesanders

  • Serf
  • *
  • Posts: 48
  • Cookies: -5
  • I'm sarcastic, somewhat new, and a grey hat
    • View Profile
Re: Adding a keyEvent
« Reply #4 on: March 14, 2014, 07:40:38 pm »
Ok thank you I will look the input class up online

Offline Matriplex

  • Knight
  • **
  • Posts: 323
  • Cookies: 66
  • Java
    • View Profile
Re: Adding a keyEvent
« Reply #5 on: March 14, 2014, 11:46:30 pm »
No problem, and have fun with it.
I've been making games with an assortment of libraries in Java for about 3 years and it's great, I'm happy to see others getting into it :)
\x64\x6F\x75\x65\x76\x65\x6E\x00

Offline m1kesanders

  • Serf
  • *
  • Posts: 48
  • Cookies: -5
  • I'm sarcastic, somewhat new, and a grey hat
    • View Profile
Re: Adding a keyEvent
« Reply #6 on: March 15, 2014, 10:31:12 pm »
Ok, I looked it up and I think I have it created, it's just not working here's my input code if you can take a look at it, and how I called it into the window

Code: (javascript) [Select]
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;


@SuppressWarnings("serial")
public class WizardKeyHandeler extends WizardBattleGrid implements KeyListener {

@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();

if(key == KeyEvent.VK_W){

WizardCells[pos1+11][pos2].setIcon(GoodGuyWizard());
}

}

@Override
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_W){

WizardCells[pos1+11][pos2].setIcon(GoodGuyWizard());

}

}

@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub

}



}


Code: (javascript) [Select]
Start.addActionListener(new ActionListener(){
@SuppressWarnings("static-access")
public void actionPerformed(ActionEvent e){

Window.setExtendedState(Window.MAXIMIZED_BOTH);
Window.remove(WizardBackground);
Window.remove(MainMenuButtons);
WizardBattleGrid.CreateGrid();
WizardKeyHandeler WizardKey = new WizardKeyHandeler();
//WizardKeyInfo.setFocusable(true);
//WizardKeyInfo.addKeyListener(WizardKey);
Window.addKeyListener(WizardKey);

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Adding a keyEvent
« Reply #7 on: March 16, 2014, 12:25:05 pm »
Ok, I looked it up and I think I have it created, it's just not working here's my input code if you can take a look at it, and how I called it into the window

Code: (javascript) [Select]
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;


@SuppressWarnings("serial")
public class WizardKeyHandeler extends WizardBattleGrid implements KeyListener {

@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();

if(key == KeyEvent.VK_W){

WizardCells[pos1+11][pos2].setIcon(GoodGuyWizard());
}

}

@Override
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_W){

WizardCells[pos1+11][pos2].setIcon(GoodGuyWizard());

}

}

@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub

}



}


Code: (javascript) [Select]
Start.addActionListener(new ActionListener(){
@SuppressWarnings("static-access")
public void actionPerformed(ActionEvent e){

Window.setExtendedState(Window.MAXIMIZED_BOTH);
Window.remove(WizardBackground);
Window.remove(MainMenuButtons);
WizardBattleGrid.CreateGrid();
WizardKeyHandeler WizardKey = new WizardKeyHandeler();
//WizardKeyInfo.setFocusable(true);
//WizardKeyInfo.addKeyListener(WizardKey);
Window.addKeyListener(WizardKey);

What does that mean "it's not working"?
And why do you suppress the static-access warning instead of removing it? Warnings are there for a reason and most of them should be acted upon. However this won't be the problem you have.

So far I find it weird that you add a keylistener upon pressing a button and that you do the same action on pressing and releasing the key.
Post the whole code again. Maybe I can get some sense out of it.
Also: Java code conventions! It is hard to read your code, if you don't follow the conventions: http://www.oracle.com/technetwork/java/codeconv-138413.html