1

I'm new to Java and OOP programming in general so if you guys could give me dumbed down explanation I would appreciate it. I don't really understand how the Graphics object and paintComponent work in general.

The basic idea of the Simulation class is that it will do the following.

1) Create a JFrame for my animation to take place in.

2) Create a Panel (I tried using a Canvas but people told me not to do that) which fits inside my JFrame, the animation will we painted to this panel.

3) Depending on the value of 'type' (and maybe some other conditions which I have not yet coded) it will set up and paint (to the Panel) the background picture (the graphics) for the corresponding Animation.

4) After painting the background image I wan't to be able to return it to Simulation class and modify it (the background Image) in a Loop with my Simulation running on a thread (therefore creating an animation).

5) After the animation is finished I want the Simulation JFrame to close (I'm not even close to getting to this part though).

Everything I see on here just tells me to create the class InitSimGraphics and have paintComponent method within it. I want to be able to paint my lines/rectangles ect.. onto the Panel and then go on to use it again where I can modify parts of it (aka make a particle move through the screen).

What I am doing right now is clearly not working and I am really really new to this so please try to go easy on me. A different approach is probably best I think since this isn't working.

Thanks!

package comp4Pack;
import javax.swing.*;
import java.awt.*;

public class Simulation implements Runnable {

    private final int pwidth = 768, pheight = 432;
    private int type;

    private JFrame frame;
    private JPanel panel;

    private boolean running = false;
    private Thread thread;

    /* Type means the following
     * 
     * 0 - Kinematics 
     * 1 - Dynamics (Momentum)
     * 2 - Vectors 
     * 3 - ... 
     * 
     */

    public Simulation(int type){

        this.type = type;

        initSimulation();
        InitSimGraphics i = new InitSimGraphics();
        panel.add(i);

    }

    private void initSimulation(){

        if(type == 0){
            frame = new JFrame("Kinematics Simulation");
        } else if(type == 1){
            frame = new JFrame("Dynamics Simulation");
        } else {
            frame = new JFrame("Vectors Simulation");
        }

        frame.setSize(pwidth, pheight);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);

        panel = new JPanel(new BorderLayout());
        panel.setPreferredSize(new Dimension(pwidth, pheight));
        panel.setMaximumSize(new Dimension(pwidth, pheight));
        panel.setMinimumSize(new Dimension(pwidth, pheight));

        frame.add(panel);
        frame.pack();
        frame.setVisible(true);

    }

    private void initGraphics(){

    }

    public void run() {

        while(running){

            tick();
            render();

        }

        stop();
    }

    private void tick(){

    }

    private void render(){

    }

    public synchronized void start(){
        if(running)
            return;
        running = true;
        thread = new Thread(this);
        thread.start();
    }

    public synchronized void stop(){
        if(!running)
            return;
        running = false;
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

..

package comp4Pack;
import javax.swing.*;
import java.awt.*;

public class InitSimGraphics extends JPanel {

    Graphics g;

    private final int pwidth = 768, pheight = 432;

    public void paintComponent(Graphics g){
        super.paintComponent(g);

        g.drawLine(0, 0, pwidth, pheight);

        this.g = g;

    }

    public Graphics getGraphics(){

        return g;

    }

}
hotzst
  • 7,238
  • 9
  • 41
  • 64
Loua
  • 161
  • 7
  • 3
    You don't need to override/provide `getGraphics()`. `paintComponent()` will get the relevant Graphics object on every call. – Thomas Nov 24 '15 at 12:46
  • 2
    Besides my other comment: `What I am doing right now is clearly not working` - please elaborate. – Thomas Nov 24 '15 at 12:48
  • Looking at similar questions on Stack Overflow might be useful, for example: http://stackoverflow.com/questions/23934675/repeating-animation-using-java-swingtimer, http://stackoverflow.com/questions/23393664/swing-repaint-doesnt-work-in-loop-or-thread, and http://stackoverflow.com/questions/9986285/animating-a-rectangle-in-java. This slightly longer article could also be interesting: http://java-articles.info/articles/?p=425. (Google search: "java swing animation graphics site:stackoverflow.com".) – Freek de Bruijn Nov 24 '15 at 16:02

0 Answers0