Alright so I'm back, I figured out the keyEvent thanks everyone who helped
, but now my problem is getting the spell to fire and continuously move across the screen until it hits the right border, as it is I have it so it moves, but only increments one every time I press space I want it to automatically increment after just one press, well here's my code any help is greatly appreciated thanks
I think those are the only classes you'll need, thanks again I looked everywhere on Google and couldn't find a thing anyway thanks
KeyHandeler
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
@SuppressWarnings("serial")
public class WizardKeyHandeler extends WizardBattleGrid implements KeyListener{
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_W){
if(getGoodGuy().getx() != 0){
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().changex(getGoodGuy().getx()-1)][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
}
}
else if(key == KeyEvent.VK_S){
if(getGoodGuy().getx() != 19){
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().changex(getGoodGuy().getx()+1)][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
}
}
else if(key == KeyEvent.VK_D){
if(getGoodGuy().gety() != 9){
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().getx()][getGoodGuy().changey(getGoodGuy().gety()+1)].setIcon(getGoodGuy().getIcon());
}
}
else if(key == KeyEvent.VK_A){
if(getGoodGuy().gety() != 0){
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().getx()][getGoodGuy().changey(getGoodGuy().gety()-1)].setIcon(getGoodGuy().getIcon());
}
}
else if(key == KeyEvent.VK_SPACE){
if(getBlueSpell().gety() != 19){
WizardCells[getBlueSpell().getx()][getBlueSpell().gety()].setIcon(null);
WizardCells[getBlueSpell().changex(getGoodGuy().getx())][getBlueSpell().changey(getBlueSpell().gety() +1)].setIcon(getBlueSpell().getIcon());
}
}
}
@Override
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_W){
if(getGoodGuy().getx() != 0){
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
}
}
else if(key == KeyEvent.VK_S){
if(getGoodGuy().getx() != 19){
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
}
}
else if(key == KeyEvent.VK_D){
if(getGoodGuy().gety() != 9){
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
}
}
else if(key == KeyEvent.VK_A){
if(getGoodGuy().gety() != 0){
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
}
}
else if(key == KeyEvent.VK_SPACE){
if(getBlueSpell().gety() != 19){
WizardCells[getBlueSpell().getx()][getBlueSpell().gety()].setIcon(null);
WizardCells[getBlueSpell().getx()][getBlueSpell().gety()].setIcon(getBlueSpell().getIcon());
}
else{
WizardCells[getBlueSpell().getx()][getBlueSpell().gety()].setIcon(null);
}
}
}
@Override
public void keyTyped(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_W){
if(getGoodGuy().getx() != 0){
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().changex(getGoodGuy().getx()-1)][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
}
}
else if(key == KeyEvent.VK_S){
if(getGoodGuy().getx() != 19){
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().changex(getGoodGuy().getx()+1)][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
}
}
else if(key == KeyEvent.VK_D){
if(getGoodGuy().gety() != 9){
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().getx()][getGoodGuy().changey(getGoodGuy().gety()+1)].setIcon(getGoodGuy().getIcon());
}
}
else if(key == KeyEvent.VK_A){
if(getGoodGuy().gety() != 0){
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().getx()][getGoodGuy().changey(getGoodGuy().gety()-1)].setIcon(getGoodGuy().getIcon());
}
}
else if(key == KeyEvent.VK_SPACE){
if(getBlueSpell().gety() != 19){
WizardCells[getBlueSpell().getx()][getBlueSpell().gety()].setIcon(null);
WizardCells[getBlueSpell().changex(getGoodGuy().getx())][getBlueSpell().changey(getBlueSpell().gety()+1)].setIcon(getBlueSpell().getIcon());
}
}
}
}
BlueSpell
import javax.swing.ImageIcon;
public class BlueSpell extends WizardBattleGrid{
private int BlueSpellx;
private int BlueSpelly;
private ImageIcon Blue;
public BlueSpell(int x, int y, ImageIcon BlueSpell){
BlueSpellx = x;
BlueSpelly = y;
Blue = BlueSpell;
}
public int getx(){
return BlueSpellx;
}
public int gety(){
return BlueSpelly;
}
public ImageIcon getIcon(){
return Blue;
}
public int changex(int x){
BlueSpellx = x;
return x;
}
public int changey(int y){
BlueSpelly = y;
return y;
}
}
Grid
import java.awt.*;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class WizardBattleGrid extends GameWindowWizard {
public static JLabel[][] WizardCells = new JLabel [20][20];
public static int pos1 = 0;
public static int pos2 = 0;
public 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(GoodGuyWizardIcon());
WizardCells[pos1+10][pos2+19].setIcon(BadGuyWizard());
Window.add(WizardPanel);
}
}
Window
import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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 JLabel GoodGuyWizardLabel = new JLabel();
private static GoodGuy GoodGuyWizard = new GoodGuy(10, 0, GoodGuyWizardIcon());
private static BlueSpell GoodSpell = new BlueSpell(getGoodGuy().getx(), getGoodGuy().gety(), BlueSpellWizard());
public static GoodGuy getGoodGuy(){
return GoodGuyWizard;
}
public static BlueSpell getBlueSpell(){
return GoodSpell;
}
public static ImageIcon BlueSpellWizard(){
ImageIcon BlueSpell = new ImageIcon("resources/bluespellnobackground.png");
return BlueSpell;
}
public static ImageIcon BadGuyWizard(){
ImageIcon BadGuy = new ImageIcon("resources/badguynobackground.png");
return BadGuy;
}
public static ImageIcon GoodGuyWizardIcon(){
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 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.setFocusable(true);
Window.remove(WizardBackground);
Window.remove(MainMenuButtons);
WizardBattleGrid.CreateGrid();
WizardKeyHandeler WizardKey = new WizardKeyHandeler();
Window.addKeyListener(WizardKey);
}
});
}
}