Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Moving Icon in gridlayout SWING

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Moving Icon in gridlayout SWING

    Quote Originally Posted by Loodistobilo View Post
    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.
    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.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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.

  4. #4
    Junior Member
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    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.
    Last edited by Loodistobilo; November 24th, 2010 at 08:02 PM.

Similar Threads

  1. How to change JTree's node icon manually?
    By LeonLanford in forum AWT / Java Swing
    Replies: 2
    Last Post: July 27th, 2010, 03:28 AM
  2. How to place icon on a picture base on (x,y) axis
    By FaintSmile in forum Java Theory & Questions
    Replies: 1
    Last Post: July 13th, 2010, 07:27 AM
  3. [SOLVED] I cant load the icon!!!!
    By chronoz13 in forum AWT / Java Swing
    Replies: 12
    Last Post: January 22nd, 2010, 03:52 AM
  4. Icon change and lib folder problem
    By LeonLanford in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 21st, 2009, 03:00 AM
  5. [SOLVED] "GridLayout" problem in Java program
    By antitru5t in forum AWT / Java Swing
    Replies: 3
    Last Post: April 16th, 2009, 10:26 AM