Reputation: 21
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!
Upvotes: 0
Views: 78
Reputation: 307
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();
Upvotes: 1