简体   繁体   中英

repaint method not calling paint method

trying to create a rectangle using paint but repaint method's not calling paint method.

even tried replacing paint with paintComponent, but still not working. So what changes to make to make it work. is there a problem with calling repaint method in run.

is there a problem with the instance.

trying to create a rectangle using paint but repaint method's not calling paint method.

even tried replacing paint with paintComponent, but still not working. So what changes to make to make it work. is there a problem with calling repaint method in run.

is there a problem with the instance.

package src;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;


public class main extends JFrame implements Runnable{

private final int Height = 480;
private final int Width = 640;
Thread gameloop; 

public static main instance = null;

private main(){

    JFrame frame = new JFrame();
    frame.setSize(this.Width,this.Height);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public static main getInstance(){

    if (instance == null){

        instance = new main();}
    return instance;
}

private void start(){

    gameloop = new Thread(this);
    gameloop.start();

}


@Override
public void paint(Graphics g){

    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(g2d.getBackground());
    g2d.fillRect(0, 0, instance.Width, instance.Height);
    g2d.setColor(Color.red);
    g2d.fillRect(0, 0, 50, 50);

}


@Override
public void run() {

    Thread current = Thread.currentThread();
    while (gameloop == current){
        try{
            Thread.sleep(5);
        }

        catch (InterruptedException e){
            e.printStackTrace();
        }

        repaint();
    }

}



  public static void main(String[] args){

      main.getInstance();

      main.getInstance().start();
}

}

The original problem was that you created a new JFrame in your constructor and then made that visible, instead of using the main instance you just created, so nothing in your main class was actually being displayed in your app, just a blank JFrame that does nothing. Also your run loop made no sense.

However, your code has a lot of other problems, both in logic and style. I'd advise you to just refactor everything . Here's a cleaner version that also solves your problem.

package src;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;


public final class Main extends JFrame implements Runnable {
    private volatile boolean running;

    private Main(int width, int height) {
        setSize(width, height);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private void start() {
        running = true;
        new Thread(this).start();
    }

    private void stop() {
        running = false;
    }

    @Override
    public void paintComponent(Graphics g){
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(g2d.getBackground());
        g2d.fillRect(0, 0, getWidth(), getHeight());
        g2d.setColor(Color.red);
        g2d.fillRect(0, 0, 50, 50);
    }

    @Override
    public void run() {
        while (running) {
            try {
                Thread.sleep(40);
            }
            catch (InterruptedException e){
                e.printStackTrace();
            }
            repaint();
        }
    }

    public static void main(String[] args) {
        new Main(640, 480).start();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM