Moving Icon in gridlayout SWING
Hi, im very new at Java so please consider this your easiest answer for the day! :)
Im trying to build a game but i suck at the basics. So I just want to get the problem fixed where so i can move an Player Object across my gridlayout and having the image updating for the keyevents like Left, Right, Up, Down... for starters.
the code i have is something like this.
public class Graphics extends JPanel implements ActionListener {
public static JButton grid[][] = new JButton[12][12];
public JFrame window;
public Point gridLoc;
public Player player1;
public Player player2;
public Graphics() {
addKeyListener(new TAdapter());
setFocusable(true);
setBackground(Color.BLACK);
setDoubleBuffered(true);
setLayout(new GridLayout(12, 12));
for(int x = 0; x < 12; x++){
for(int y = 0; y < 12; y++){
grid[x][y] = new JButton();
add(grid[x][y]);
grid[x][y].addActionListener(this);
}
}
}
public void addPlayers() throws SpriteException{
player1 = new Player("Fire", 1);
grid[Player.currentRow][Player.currentCol].setIcon(player1.getPic());
player2 = new Player("Wizard", 2);
grid[Player.currentRow][Player.currentCol].setIcon(player2.getPic());
}
private class TAdapter extends KeyAdapter {
public void keyPressed(KeyEvent e) {
player1.keyPressed(e);
}
}
I have left alot of code which i consider not be critical for you guys to give me some pointers on how i will get my KeyEvents to just move my Player player1 object for example.
Thanks in advance.
Re: Moving Icon in gridlayout SWING
Quote:
Originally Posted by
Loodistobilo
Hi, im very new at Java so please consider this your easiest answer for the day! :)
Im trying to build a game but i suck at the basics. So I just want to get the problem fixed where so i can move an Player Object across my gridlayout and having the image updating for the keyevents like Left, Right, Up, Down... for starters.
the code i have is something like this.
Code java:
public class Graphics extends JPanel implements ActionListener {
public static JButton grid[][] = new JButton[12][12];
public JFrame window;
public Point gridLoc;
public Player player1;
public Player player2;
public Graphics() {
addKeyListener(new TAdapter());
setFocusable(true);
setBackground(Color.BLACK);
setDoubleBuffered(true);
setLayout(new GridLayout(12, 12));
for(int x = 0; x < 12; x++){
for(int y = 0; y < 12; y++){
grid[x][y] = new JButton();
add(grid[x][y]);
grid[x][y].addActionListener(this);
}
}
}
public void addPlayers() throws SpriteException{
player1 = new Player("Fire", 1);
grid[Player.currentRow][Player.currentCol].setIcon(player1.getPic());
player2 = new Player("Wizard", 2);
grid[Player.currentRow][Player.currentCol].setIcon(player2.getPic());
}
private class TAdapter extends KeyAdapter {
public void keyPressed(KeyEvent e) {
player1.keyPressed(e);
}
}
I have left alot of code which i consider not be critical for you guys to give me some pointers on how i will get my KeyEvents to just move my Player player1 object for example.
Thanks in advance.
Does it have a method actionPerformed?
Also, your player1 doesn't appear to be defined in your constructor. I think it may be being lost after the method where you defined it ends.
So it won't recognize it in the TAdapater class.
Re: Moving Icon in gridlayout SWING
Did you ever tell it to set those buttons to be visible?
Actually, I don't know what problem you're having as you've never said.
Re: Moving Icon in gridlayout SWING
Hi thanks for your quick answear.
I might aswell say that I have an ActionPerformed but for now it might aswell just be a
System.out.println("test");
that says if the button actually does something.
The player class atm looks like this.
Code java:
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.ImageIcon;
public class Player {
public static ImageIcon image;
public static ImageIcon fire = new ImageIcon("pieces/brook.png");
public ImageIcon wizard = new ImageIcon("pieces/bking.png");
public static int currentRow;
public static int currentCol;
public Player(String str, int player) throws SpriteException {
if(player == 1){
currentRow = 5;
currentCol = 0;
}
if(player == 2){
currentRow = 0;
currentCol = 0;
}
}
public void moveLeft(){
grid[Player.currentRow][Player.currentCol].setIcon(null);
grid[Player.currentRow][Player.currentCol+1].setIcon(player1.getPic());
}
public void setImage(String str){
if(str.equals("Wizard")){
image = wizard;
}
if(str.equals("Fire")){
image = fire;
}
}
public static ImageIcon getPic(){
ImageIcon p = image;
return p;
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
moveLeft();
}
}
}
I havent really been doing the movement yet. I just want the keyEvent to work. Since now i dont even get a system.out.print if i put on for Space. It just gives me alot of Nullpointerexceptions etc.
Thanks.