3 Replies - 228 Views - Last Post: 26 May 2019 - 02:31 PM Rate Topic: -----

#1 lordvile   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-May 19

Moving a JLabel set as an ImageIcon with ArrowKeys

Posted 26 May 2019 - 01:49 PM

I've been trying to get a JLabel to move around with arrow keys. However, the input seems to not be registered. I've tried changing the requestinfocuswindow, but beyond that, I'm not sure why my code isn't working.
public class Main extends JPanel {
	private Clip clip;
    Character pro = new Protag();
	private int protagX = pro.getX();
	private int protagY = pro.getY();
	JLabel protag; 
	public static void main(String[] args) {
		JFrame mainFrame = new JFrame();
		Main main = new Main();
		mainFrame.add(main);
		
	
		mainFrame.setSize(1366,800);
		mainFrame.setLocation(90,10);
		mainFrame.setResizable(false);
		
		
		
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainFrame.setVisible(true);
		main.requestFocusInWindow();
		 
	}
	public Main() {
		playMusic("battle.wav");
		this.setLayout(null);
//		Keyboard keyListener = new Keyboard(this);
//		this.addKeyListener(keyListener);
		ImageIcon proImage = pro.getImageIcon();
		//ImageIcon enemyImage = new ImageIcon(("yosuke-stance.gif"));
		protag = new JLabel();
		//JLabel enemy = new JLabel();
		protag.setIcon(proImage);
		
		//enemy.setIcon(enemyImage);
		add(protag);
		//add(enemy);
		Dimension size = protag.getPreferredSize();
		System.out.print(protagX);
		protag.setBounds(protagX,protagY,size.width,size.height);
		protag.addKeyListener(new KeyAdapter(){
			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub
				int code = e.getKeyCode();
				if (code == KeyEvent.VK_LEFT) {
					System.out.println("test");
					ImageIcon proImage = pro.getImageIcon();
					pro.setImageIcon(new ImageIcon("yu-walkback.gif"));
					protag.setIcon(proImage);
					protag.setLocation(protag.getX()-5,protag.getY());
					repaint();
				}
				else if (code == KeyEvent.VK_RIGHT) {  
					System.out.println("test");
					ImageIcon proImage = pro.getImageIcon();
					pro.setImageIcon(new ImageIcon("yu-walkback.gif"));
					protag.setIcon(proImage);
					protag.setLocation(protag.getX()+5,protag.getY());
					repaint();
				}
				else if (code == KeyEvent.VK_DOWN) {
					System.out.println("test");	
				}
				else if (code == KeyEvent.VK_Z) {
					System.out.println("test");	
					ImageIcon proImage = pro.getImageIcon();
					pro.setImageIcon(new ImageIcon("yu-spinslash.gif"));
					protag.setIcon(proImage);
					repaint();
				}
				protag.requestFocusInWindow();
			}
		});
	
		
	}


This post has been edited by macosxnerd101: 26 May 2019 - 01:49 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Moving a JLabel set as an ImageIcon with ArrowKeys

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12634
  • View blog
  • Posts: 45,799
  • Joined: 27-December 08

Re: Moving a JLabel set as an ImageIcon with ArrowKeys

Posted 26 May 2019 - 01:57 PM

Moving a JLabel is not a good approach. Manipulating the GUI in this manner is messy. It will be hard to configure the layout in the desired manner after each update. There are some better approaches.
  • If your game setting is a grid, then you can use a GridLayout of JLabels and simply use the JLabel setIcon() method to update the cell in which the character resides.

  • Alternatively, you can extend a JComponent (like JPanel) and override the paintComponent() method. This will allow you to handle all the painting yourself.

Was This Post Helpful? 0
  • +
  • -

#3 lordvile   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-May 19

Re: Moving a JLabel set as an ImageIcon with ArrowKeys

Posted 26 May 2019 - 02:12 PM

View Postmacosxnerd101, on 26 May 2019 - 01:57 PM, said:

Moving a JLabel is not a good approach. Manipulating the GUI in this manner is messy. It will be hard to configure the layout in the desired manner after each update. There are some better approaches.
  • If your game setting is a grid, then you can use a GridLayout of JLabels and simply use the JLabel setIcon() method to update the cell in which the character resides.

  • Alternatively, you can extend a JComponent (like JPanel) and override the paintComponent() method. This will allow you to handle all the painting yourself.

How should I draw the image and move it with the arrow keys then?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12634
  • View blog
  • Posts: 45,799
  • Joined: 27-December 08

Re: Moving a JLabel set as an ImageIcon with ArrowKeys

Posted 26 May 2019 - 02:31 PM

If you use the second approach, then you should extend JPanel and override the paintComponent() method to draw a static frame. The KeyListener interface can be used to update the character's position.

Then you can have a javax.swing.Timer call the repaint() method every so-often (e.g., every 50-100 milliseconds).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1