-1

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, ...,...);
vs97
  • 5,765
  • 3
  • 28
  • 41
  • `public class ChessBoard..` Oh I [remember you](http://stackoverflow.com/q/21525019/418556). If you are going to ignore the sound advice of both me and Marco13, I fear it will take you a long time to create a broken GUI. Good luck with that, I have other people to help. – Andrew Thompson Feb 05 '14 at 10:33

2 Answers2

0

You can declare your JLabel variable globally like your button and panel. and then instantiate it in createGUI and add it to your panel at a specific position to display it. Hope this helps

Sanjeev
  • 9,876
  • 2
  • 22
  • 33
0
  • Don't use getGraphics, this is not how custom painting is done in Swing
  • Don't override paint of top level containers and always call super.paintXxx to maintain the paint chain

Take a look at Performing Custom Painting and 2D Graphics

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366