I have the following code, and I am struggling to what to do after I have the JLabel loaded. How do I show it and give it coordinates as to where to be placed? I tried some solutions on the web, but they dont quite seem to work by giving errors like "Cant find getCodeBased" and similar. Can someone please help? I am still a beginner, so please dont be harsh.
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import java.awt.Image;
public class ChessBoard extends JFrame implements ActionListener
{
private JButton button;
private JPanel panel;
JLayeredPane layeredPane;
public static void main(String[] args)
{
ChessBoard demo = new ChessBoard();
demo.setSize(900,900);
demo.createGUI();
demo.setVisible(true);
}
private void createGUI()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new FlowLayout());
panel = new JPanel();
panel.setPreferredSize(new Dimension(800,800));
panel.setBackground(Color.white);
window.add(panel);
button = new JButton("start");
window.add(button);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent event)
{
int xLeft;
int yTop;
Graphics paper = panel.getGraphics();
paper.setColor(Color.black);
paper.fillRect(0,0,800,800);
paper.setColor(Color.white);
xLeft = 0;
for (int i = 100; i < 800; i += 100)
{
paper.drawLine(i,0,i,800);
}
for (int i = 100; i < 800; i += 100)
{
paper.drawLine(0, i, 800, i);
}
for (int j = 1; j < 9; j++)
{
paper.setColor(new Color(238, 221, 187));
for (int k = 100 * ((j+1) % 2); k < 800; k+=200)
{
paper.fillRect (k, (j-1) * 100, 100, 100);
}
paper.setColor(new Color(204,136,68));
for (int i = 100 * (j%2); i < 800; i+=200)
{
paper.fillRect(i, (j-1) * 100, 100, 100);
}
}
}
public void paint(Graphics g)
{
JLabel piece = new JLabel( new ImageIcon(getClass().getResource("Rooka8.png")));
}
}
P.S.
Managed to fix it myself, by trying posting the code below in the ActionPerformed method, as well as import java.awt.Image;
ImageIcon myImage = new ImageIcon(...);
myImage.paintIcon(this, paper, ...,...);