Adding a JLabel on top of a JPanel?

Don't forget to revalidate and repaint if adding or removing components from a container. I've modified your SSCCE, and have gotten rid of the need to use images to make it runnable by folks who don't have access to your image files (like me! ).

Changes are noted by the! Comments.

Don't forget to revalidate and repaint if adding or removing components from a container. I've modified your SSCCE, and have gotten rid of the need to use images to make it runnable by folks who don't have access to your image files (like me! ).

Changes are noted by the //! Comments: import java.awt. Color; import java.awt.

Dimension; import javax.swing. *; import java.awt. *; import java.awt.event.

*; import java.awt.image. BufferedImage; public class Gui2 extends JFrame implements MouseListener { private static final long serialVersionUID = -973341728129968945L; JLayeredPane layeredPane; JPanel board; JLabel piece; int numSquares; private boolean currentPlayer; //! Private ImageIcon whiteIcon; private ImageIcon blackIcon; public Gui2() { //!

WhiteIcon = createIcon(Color. White); blackIcon = createIcon(Color. Black); Dimension boardSize = new Dimension(600, 600); numSquares = 6; currentPlayer = true; layeredPane = new JLayeredPane(); getContentPane().

Add(layeredPane); layeredPane. SetPreferredSize(boardSize); layeredPane. AddMouseListener(this); board = new JPanel(); layeredPane.

Add(board, JLayeredPane. DEFAULT_LAYER); board. SetLayout(new GridLayout(numSquares, numSquares)); board.

SetPreferredSize(boardSize); board. SetBounds(0, 0, boardSize. Width, boardSize.

Height); for (int I = 0; I SetBorder(BorderFactory. CreateLineBorder(Color. Black)); square.

SetBackground(Color. Green); square. SetName(String.

Format("%d, %d", I % numSquares, I / numSquares)); //! Board. Add(square); } } //!

Private ImageIcon createIcon(Color color) { int width = 40; int height = width; BufferedImage img = new BufferedImage(width, height, BufferedImage. TYPE_INT_ARGB); Graphics2D g2 = img.createGraphics(); g2. SetRenderingKEY_ANTIALIASING, RenderingVALUE_ANTIALIAS_ON); g2.

SetColor(color); g2. FillOval(0, 0, width, height); g2.dispose(); ImageIcon icon = new ImageIcon(img); return icon; } public static void main(String args) { JFrame frame = new Gui2(); frame. SetDefaultCloseOperation(DISPOSE_ON_CLOSE); frame.pack(); frame.

SetResizable(true); frame. SetLocationRelativeTo(null); frame. SetVisible(true); } @Override //!

Public void mousePressed(MouseEvent e) { JPanel temp = (JPanel) board. FindComponentAt(e.getX(), e.getY()); System.out. Println(e.getX() + " " + e.getY()); System.out.

Println(temp.getName()); //! If (currentPlayer) { //! ImageIcon white = new ImageIcon("l/Images/white.

Jpg"); //! Piece = new JLabel(white); piece = new JLabel(whiteIcon); //! Temp.

Add(piece); } else { //! ImageIcon black = new ImageIcon("/Images/black. Jpg"); //!

Piece = new JLabel(black); piece = new JLabel(blackIcon); //! Temp. Add(piece); } temp.revalidate(); //!Temp.repaint(); //!

CurrentPlayer =! CurrentPlayer; } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseExited(MouseEvent arg0) { } @Override public void mouseClicked(MouseEvent arg0) { } @Override public void mouseReleased(MouseEvent e) { } } Also class names should be capitalized, and also you should again make your ImageIcons once. Again, one ImageIcon can be shared by many JLabels.

You'll also want to respond to mousePressed not mouseClicked as mouseClicked can be fussy, especially if you move the mouse between press down and mouse release. Hopefully you've also seen the value of an SSCCE. :).

A minor non-programming suggestion - name the squares by a letter A-H (for the column) and a number 1-8 (for the row). E.g. The White King starts on E1.

Just like in real chess. – user949300 Nov 7 at 1:22 Actually the game I think the OP is creating is Reversi, not Chess, but the naming of the ranks and files is the same as in chess if my memory serves me right. – Hovercraft Full Of Eels Nov 7 at 1:25 for posting SSCCE – mKorbel Nov 7 at 8:20.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions