0

I'm trying to create a GUI for "Connect Four" with java swing but can't figure out how I can set the play field that will be filled up with the pawns(sorry if it isn't the right word but I'm Italian). Could anyone help me out?

  • You can either use [Custom painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/) to paint each slot or you could use a [GridLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html) with a `JLabel` with an `Icon` of the image. If you still have doubts, post a valid [mcve] that demonstrates your best approach into solving the problem – Frakcool Jun 27 '17 at 14:00
  • I actually built a grid made by Jbuttons in an ArrayList so that when the user clicks a button that is in a certain column the program add the pawn and to do so, it change the color of the right button itself.I figured out the logic but I can't build up a GUI – Andreascopp Jun 27 '17 at 14:50
  • Explain your question clearly and what exactly is not working, post your MCVE while [edit]ing your question :) – Frakcool Jun 27 '17 at 14:56
  • Setting the background image is a relatively common question, [for example](https://stackoverflow.com/questions/22162398/how-to-set-a-background-picture-in-jpanel/22162430#22162430) and [example](https://stackoverflow.com/questions/13791984/add-an-background-image-to-a-panel/13792503#13792503) - but, I don't actually think that's the question you're trying to ask (if it is we'll close the question as a duplicate) - you migth want to clarify you question and update the title to match – MadProgrammer Jun 27 '17 at 20:46
  • Actually it is,i want to set a background but using an ImageIcon if it's possible – Andreascopp Jun 28 '17 at 08:41

1 Answers1

1

You need to create a custom JPanel:

class BackgroundPanel extends JPanel {
    private BufferedImage image;

    public BackgroundPanel() {
        URL resource = getClass().getResource("background.jpg");
        try {
            image = ImageIO.read(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, this);
    }
}

and add it to your JFrame

Oneiros
  • 4,328
  • 6
  • 40
  • 69
  • Unfortunately we haven't study nothing like that. Our teacher gave us a class that he wrote and it uses imageIcon so I thought to do something like this instead of your solution: private class BackgroundPanel extends JPanel { private ImageIcon image; public BackgroundPanel() { image = caricaIcona("Griglia.png"); public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(image, 0, 0, this); } } – Andreascopp Jun 27 '17 at 14:46
  • I've tried like 10 times to write my code properly :( sorry – Andreascopp Jun 27 '17 at 14:46
  • Write it in your question :) – Frakcool Jun 27 '17 at 14:48
  • @Andreascopp If you're using an `ImageIcon`, then use a `JLabel`, it will solve at least one of the core issues with this answer – MadProgrammer Jun 27 '17 at 20:47
  • @Andreascopp using a BufferedImage is not very different from using an ImageIcon in this case. Anyway I think you can use an ImageIcon, just read the Graphics Java API to discover if there's a proper function to use :) – Oneiros Jun 27 '17 at 20:50
  • @MadProgrammer I strongly disagree: when you're developing Java games it is better to not introduce new JComponent for each graphic element, it would decrease performance and mantainability. Just use a single JPanel and draw on it with Graphics object – Oneiros Jun 27 '17 at 20:53
  • @Oneiros I don't think it's "that" type of game, also, given the inexperience of the developer, using a `JLabel` and `icon` as the background component is a simple and common solution, it also solves to immediate shortcomings of your example. Given the minimal information, I could produce a solution with nothing more than `JLabel`s and a `GridLayout` which would be simpler and easier for the OP to understand - not everything has to be custom painted – MadProgrammer Jun 27 '17 at 21:36
  • It would be a bad practice and OP would learn nothing. Java Graphics is easy, simple and powerful enough to build any basic game and OP would have full control of the rendering process. With JComponents he would just waste time on focus-related problems, hierarchy of parents and child, layout annoying issues and so on. Just my opinion anyway, OP ask your teacher what kind of approach he prefers :) and share with us his code, it would help us to understand how to help you (sono anche io italiano comunque!) – Oneiros Jun 27 '17 at 21:45
  • (Full example of ImageIcon painting: https://stackoverflow.com/a/21406024/555336) – Oneiros Jun 27 '17 at 21:47
  • I wrote the class that my teacher gave us, know only that kind of things so I prefer not to write things that I don't understand (@Oneiros noi italiani siamo ovunque!) – Andreascopp Jun 28 '17 at 09:40
  • Ok so your teacher wants you to use Swing components to build a game... shame on him! ;) joking aside, follow @MadProgrammer suggestion: use a JLabel (you can pass the icon to it using the constructor: `JLabel background=new JLabel(icon);`) – Oneiros Jun 28 '17 at 09:44
  • @MadProgrammer should I use a JLabel of the size of the window?But how can I put the buttons in the front layer? – Andreascopp Jun 28 '17 at 09:57
  • Yes, set JLabel's size to the size of the window. Then you can add buttons in front of it by using `label.add(button)` – Oneiros Jun 28 '17 at 09:58
  • I tried as you can see in my answer below but the image isn't as big as the frame – Andreascopp Jun 28 '17 at 10:47