0

I am trying to make a program where I have two JPanels inside a JFrame, one of which contains a canvas. I am trying to find a way to get that canvas to be constantly updating so that I could create something like a game inside the canvas. I was wondering how I could make it so that the JPanel with the canvas in it is constantly refreshing so that you can see when something is changed in the canvas. Here is my code:

package main;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main
{
    private JFrame frame;
    private JPanel mainPanel;
    private JPanel gamePanel;
    private JPanel sidePanel;

    public void setup()
    {
        frame = new JFrame("Testing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setLocation(100, 50);

        mainPanel = new JPanel();
        Dimension d = new Dimension(800, 600);
        mainPanel.setMaximumSize(d);
        mainPanel.setMinimumSize(d);
        mainPanel.setPreferredSize(d);
        frame.add(mainPanel);
        frame.pack();

        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));
    }

    public void setupPanels()
    {
        gamePanel = new JPanel();

        Dimension d = new Dimension(600, 600);

        gamePanel.setMaximumSize(d);
        gamePanel.setMinimumSize(d);
        gamePanel.setPreferredSize(d);
        gamePanel.setBackground(Color.RED);

        mainPanel.add(gamePanel);

        sidePanel = new JPanel();

        d = new Dimension(600, 600);

        sidePanel.setMaximumSize(d);
        sidePanel.setMinimumSize(d);
        sidePanel.setPreferredSize(d);
        sidePanel.setBackground(Color.BLUE);

        mainPanel.add(sidePanel);
    }

    public void setupGame()
    {
        GameArea game = new GameArea();

        gamePanel.add(game);
        game.start();
    }

    public static void main(String[] args)
    {
        Main main = new Main();
        main.setup();
        main.setupPanels();

        main.setupGame();
    }
  • basicly you need to have a seperate thread for the game itself and for the value updating/ graphic drawing process in order to not freeze everything constantly. just look up some `Jaava game Tutorial` videos, most of them explain the basics pretty okaish – SomeJavaGuy Jul 21 '17 at 08:57
  • You could use MVC pattern or just the observer pattern... – deHaar Jul 21 '17 at 08:59
  • You mean, something like [this for example](https://stackoverflow.com/questions/45139253/how-to-make-painted-objects-on-applet-canvas-blink/45141219#45141219) or [this](https://stackoverflow.com/questions/35472233/load-a-sprites-image-in-java/35472418#35472418) or [this](https://stackoverflow.com/questions/23815472/how-to-animate-rectangle-in-jpanel/23817255#23817255) or [this](https://stackoverflow.com/questions/35837747/how-to-generate-multiple-circle-shapes-and-animate-those-shapes-going-down-the-f/35838933#35838933) – MadProgrammer Jul 21 '17 at 09:11

0 Answers0