0

I have done a lot of research and I am still unsure how to solve this problem. I am trying to make a game, and in it I need an icon to display on the screen in various locations. For now, I am just trying to make an icon visible.

I have a class that handles input from the keyboard (named KeyInputHandler) and another class (named DrawGameBoard) that creates the window and background. In DrawGameBoard, I have a method, called moveIcon, that is supposed to display an Icon.

public class DrawGameBoard extends JPanel
{ 
 public static DrawGameBoard panel = new DrawGameBoard();
 public static JFrame window = new JFrame("Fill the Boxes");
 public static Container c = window.getContentPane();

 public void moveIcon(int x, int y, JLabel label)
{
  c.add(label);
  //label.setLocation(x,y);
  c.validate();
  c.repaint();
  System.out.println("tried to show a label");
}
public void paintComponent(Graphics g) 
{
 super.paintComponent(g);
 g.setColor(Color.GRAY);
 g.fillRoundRect(5, 5, 585, 585, 15, 15);
 for(int x = 25; x < 515; x+=79)
 {
   for(int y = 25; y< 515; y+=79)
   {
     g.setColor(Color.BLACK);
     g.fillRect(x, y, 68, 68);
     g.setColor(Color.WHITE);
     g.fillRect(x+10, y+10, 48, 48);
   } 
 }
} 
   public static void main(String[] args)
 {
 KeyInputHandler k = new KeyInputHandler();

//create the window
//JFrame window = new JFrame("Fill the Boxes");
window.setBounds(0, 0, 600, 630);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);

//create the panel
//DrawGameBoard panel = new DrawGameBoard();
//Container c = window.getContentPane();
panel.setBackground(new Color(0,222,222));
panel.addKeyListener(k);
panel.setFocusable(true);
//panel.setLayout(null);//doesnt use default locations

c.add(panel);
window.setVisible(true);
}
}
}

This gets called in KeyInputHandler

   public void keyPressed(KeyEvent e)
   {
    System.out.println("keyPressed: ");
    BoxGame b = new BoxGame();
    DrawGameBoard d = new DrawGameBoard();

    //create icons to be moved
    MovingPlayerIcon icon1 = new MovingPlayerIcon(1,0);
    JLabel p1fill = new JLabel();
    p1fill.setIcon(icon1);

////////////////////////////////////////////////////////////////     
    //controls for p1
    if (e.getKeyCode() == KeyEvent.VK_A) {
      if(b.getX1()>0){
        b.setPos(b.getX1()-1, b.getY1(), 1);
      }
    System.out.println("A");
    d.moveIcon(0,0,p1fill);
    }
////////////////////////////////////////////////////////////////    
}

So, when I hit the 'A' key, the moveIcon method gets called. I know that the moveIcon method is being called because upon hitting the A key, "A" is printed and "tried to show a label" is printed. I have tried replacing my icon with a normal text JLabel and that didn't display either.

Additional info:

I have the setLocation() commented out of the moveIcon class because before I had set the Layout of the JPanel to null(it is not set tat way anymore). (I wanted to put the icons in specific locations, but I am not worried about this at the moment)

BoxGame is a class that handles the information regarding the locations of players, such as the current X and Y values. It shouldn't be affecting the display.

MovingPlayerIcon is a class that paints an icon with colors based on the parameters. Again, I don't think that this is affecting the display as I tried replacing the icon with a normal text JLabel and nothing happened there either.

So, any idea why the JLabel isn't showing up?

I have been programming in java for a little over a year. I really appreciate your time and help. Let me know if you need any additional information (I tried to be as specific as I could). Thank you very much!

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Doing `DrawGameBoard d = new DrawGameBoard();` inside `keyPressed` is probably the start of your problems – MadProgrammer May 28 '18 at 02:52
  • 1
    Instead of using a `JLabel` in this way, consider just painting the image within the `paintComponent` method directly - as the second example [from this previous answer](https://stackoverflow.com/questions/50536948/make-an-jlabel-move-with-key-bidings/50537185#50537185) demonstrates – MadProgrammer May 28 '18 at 02:54
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson May 28 '18 at 08:12
  • I couldn't get paintComponent to work as I need the painting to happen when a key is pressed – Natalie Hahn May 28 '18 at 19:01
  • why is DrawGameBoard d.. a problem? Thanks again for the help – Natalie Hahn May 28 '18 at 19:02
  • *"why is DrawGameBoard d.. a problem?"* Tip: Add @MadProgrammer (or whoever, the `@` is important) to *notify* the person of a new comment. – Andrew Thompson May 29 '18 at 02:31
  • 1
    @NatalieHahn *"I couldn't get paintComponent to work as I need the painting to happen when a key is pressed"* - I linked a runnable example which demonstrates using a custom painting route and key bindings - so far, you've not provided runnable which demonstrates your problem – MadProgrammer May 29 '18 at 02:34
  • @NatalieHahn *"why is DrawGameBoard d.. a problem"* - You're create another instance of `DrawGameBoard` which is not applied to anything which can paint it - this is not how this should be done. A good place to start is to drop the reliance on `static` and learn how to better manage the state references of your objects – MadProgrammer May 29 '18 at 02:36
  • @NatalieHahn *"So, any idea why the JLabel isn't showing up?"* - Because the instance of `DrawGameBoard` you're adding the label to isn't displayed on the screen. You shouldn't need to create a new instance of `DrawGameBoard` or the `JLabel` in `keyPressed` – MadProgrammer May 29 '18 at 02:40

1 Answers1

1

So,you need to draw the label as the key 'A' is pressed. Instead of using your method 'moveIcon', you could simply draw the label at desired position and at the desired time by using 'paintComponent' method. Declare those variables in your class ..

boolean paint = false;
int x = 100;
int y = 100;
  • Now, add the following lines of the code to your 'paintComponent' method

    if(paint == true){ g.drawString("Your Desired Text",x,y); }

  • Make 'KeyInputHandler' class as an inner class of 'Draw Game Board' class

  • Now, in your 'keyPressed' method, execute the following line of code, when 'A' key is pressed

    DrawGameBoard.this.paint = true; DrawGameBoard.this.x = 100; DrawGameBoard.this.y = 100; repaint();

Miles Morales
  • 307
  • 1
  • 15