1

I want to repaint() my JPanel using a Thread in Java .

I have two different classes which i am using .

The class with JFrame (Main class) and another class wich extends a JPanel.

Here are the classes :

This one is The Frame it selve

public class Frame {
    static int width = 1280 ;
    static int height = 720 ;

public Frame(){
        InGamePanel inGame = new InGamePanel();
        JFrame menu = new JFrame();
        Menu.setSize(width, height);
        Menu.setTitle("STICK FACTORY");
        Menu.setResizable(false);
        Menu.add(InGame);
        Menu.setVisible(true);
    }

    public static void main(String[] args) {
        new Frame();

    }

}

And this one is the on with the Thread , which is not running :

import javax.swing.JPanel;

import BackGround.BackGround;
import Enums.Player;
import Enums.Stick;

public class InGamePanel extends JPanel implements KeyListener{

    //Thread for FPS
    FPS reloader = new FPS();

    //Background
    BackGround gameBG = new BackGround(Frame.width , Frame.height);

    //Player
    int xPosition_Player = 400;
    int yPosition_Player = 180;
    Player jeff = new Player();

    //Stick 
    int xPosition_Stick = 600 ;
    int yPosition_Stick = 180 ;
    Stick fluffy = new Stick();

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        gameBG.drawBackGround(g);
        jeff.drawPlayer(g, XPosition_Player, YPosition_Player);
        fluffy.drawStick(g, XPosition_Stick, YPosition_Stick);
        reloader.run();
    }







    @Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub
        if(e.getKeyChar() == 's'){
            yPosition_Player+= 40 ;
            jeff.direction = 's';
        }
    }




    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub

    }




    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub

    }   


    class FPS implements Runnable{

        int fps = 60 ;
        @Override
        public void run() {
            try {
                Thread.sleep(1000 / fps);
                repaint();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }


}

So how can I get the Thread run and repaint my Program ?

Touko
  • 31
  • 1
  • 6
  • 2
    Variable names should NOT start with an upper case character. Follow Java conventions. Fix your code and update the question. Also, a painting method is for painting only. You should NOT start a Thread in a painting method. Get rid of that code. If you want animation then start a `Swint Timer` in the constructor of your class and then invoke `repaint()` on the component. – camickr Aug 11 '15 at 15:15
  • 1
    See [*Concurrency in Swing*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) and [*How to Use Swing Timers*](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html). – trashgod Aug 11 '15 at 15:17
  • Ok I will update my code – Touko Aug 11 '15 at 15:18
  • 2
    Related: [**DO NOT EVER USE `THREAD.SLEEP` ON THE EVENT DISPATCH THREAD**](http://stackoverflow.com/a/31776980/1768232) – durron597 Aug 11 '15 at 15:21
  • I start reading the articles right now hope they will help me :) – Touko Aug 11 '15 at 15:22
  • And one more link: http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html. Seriously, you should read the provided material and then start from scratch. – Sleafar Aug 11 '15 at 15:24
  • Yea normally i just programm Android apps i am really new in Java (three days or something like this ) so sry :D – Touko Aug 11 '15 at 15:30
  • You've got a Runnable and are calling `run()` on it... why? And what's worse, you call this in the paintComponent method, something that is (sorry to be blunt) but doubly crazy. Why create a Runnable if you aren't going to put it into a Thread and call `start()` on the Thread? You will want to read up on basic Java threading as well. I would scrap this code and start over using basic principles. Also, the paintComponent method is for painting and painting only. It should not be used to start threads (even if you did it correctly, which you don't). – Hovercraft Full Of Eels Aug 11 '15 at 16:26
  • From where i have to start the Thread else ? Everytime i try to start it it doesnt work – Touko Aug 11 '15 at 16:54
  • Don't even use a Thread -- use a Swing Timer as has been suggested above. But if you ever use a Thread in your future code, then do so correctly, by creating a Thread, passing in the Runnable, and then calling `start()` on the Thread. You almost never want to call `run()` on the Runnable as that's the Thread's job. – Hovercraft Full Of Eels Aug 11 '15 at 17:12
  • Hey me again i created a swing timer but i dont want to use a button to start it should start at opening the program so what can i do ? – Touko Aug 11 '15 at 17:32

0 Answers0