The paintComponent() will not be called even if you write paint()...

Asked 2 years ago, Updated 2 years ago, 89 views

I'm making a simple game using swing, but I'm having trouble with the drawing process...

I think I wrote the process of continuing to repaint components using parallel processing, but no matter how many times I try, paintComponent() is never called.

If anyone knows the cause, please let me know…

PlayView.java (partial excerpt)

import java.awt.*;
import java.swing.*;

public class PlayView extensions JPanel implements KeyListener, Runnable {

    public JPanel panel=new JPanel(); // JPanel carrying all components
    public static JPanel[]lane=new JPanel[4];
    public JLabel[]judge=new JLabel[4];
    public JLabel combo=new JLabel();
    public Dimension panelsize=null;

publicPlayView(){

        for(inti=0;i<4;i++){ 
            lane[i] = new JPanel();
            judge[i] = new JLabel();
        }

        panel.setSize(800,600);
        panel.setLayout(null);

        for(inti=0;i<4;i++){//Place components with coordinates
            lane[i].setBounds (200*i, 0, 200, 500);
            judge[i].setBounds (200*i, 520, 200, 60);
            lane[i].setBackground (new Color (211, 211, 211));
            lane[i].setBorder (new EtchedBorder (EtchedBorder.RAISED));
            judge[i].setBorder(new EtchedBorder(EtchedBorder.RAISED));
            judge[i].setFont(newFont("Arial", Font.PLAIN, 27));
            judge[i].setForeground (new Color (220, 20, 60);
            panel.add(lane[i]);
            panel.add(judge[i]);
        }

        this.panelsize=lane[0].getSize();

        panel.setBackground(new Color(135,206,235);
        panel.addKeyListener(this);

    }

@ Override
    public void run() {
        while(this.isVisible()){
            panel.repaint(); // Redrawing the panel every 10 ms
            try{
                Thread.sleep(10);
            } catch(InterruptedExceptione) {
                e.printStackTrace();
            }
        }
    }

    @ Override
    public void paintComponent (Graphics g) {
        // System.out.println("paintComponent");
        // super.paintComponent(g);
        g.setColor(newColor(255,255,255)); //211,211,211
        for(inti=0;i<4;i++){
            g.fillRect(200*i,0,(int)this.panelsize.getWidth(),(int)this.panelsize.getHeight());
        }
        // Draw an image by randomly changing coordinates
        for(inti=0;i<4;i++){
       g.drawImage((new ImageIcon("hoge.png")).getImage(), (int) Math.random()*(int)this.panelsize.getWidth(), 
           (int) Math.random()*(int)this.panelize.getHeight(),lane[i]);
        }
    }

    @ Override
    public void keyPressed (KeyEvent arg0) {
    If(arg0.getKeyCode()==KeyEvent.VK_SPACE) {//space and run() call
          Thread thread = new Thread(this);
          US>Execute the contents of thread.start();//this.run()?
    }
}

MainFrame.java

import java.awt.*;
import java.swing.*;

public class MainFrame extensions JFrame implements KeyListener {
    PlayView pv = new PlayView();
    publicMainFrame(){
        This.add(pv.panel);
        pv.panel.setVisible(true);
        This.setBounds (200, 100, 800, 600);
        This.addKeyListener(pv);
    }
    public static void main(String[]args) {
        MainFrame f = new MainFrame();
        f.setDefaultCloseOperation(EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}

java swing

2022-09-30 16:22

1 Answers

Moving code

MainFrame.java

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;

public class MainFrame extensions JFrame {
    PlayView pv = new PlayView();
    publicMainFrame(){
        This.add(pv.panel);
        pv.setVisible(true);
        This.setBounds (200, 100, 800, 600);
        This.addKeyListener(pv);
    }
    public static void main(String[]args) {
        MainFrame f = new MainFrame();
        f.setDefaultCloseOperation(EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}

PlayView.java

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
import javax.swing.border.EtchedBorder;

public class PlayView extensions JPanel implements KeyListener, Runnable {

    // public JPanel panel=this; // Solution No. 2
    public JPanel panel = new JPanel(); // Solution #1
    public static JPanel[]lane=new JPanel[4];
    public JLabel[]judge=new JLabel[4];
    public JLabel combo=new JLabel();
    public Dimension panelsize=null;

    publicPlayView(){

        for(inti=0;i<4;i++){
            lane[i] = new JPanel() {
                @ Override
                Protected void paintComponent (Graphics g) {
                    super.paintComponent(g);
                    Image img = new ImageIcon("hoge.png").getImage();
                    intx=(int)(Math.random()*getWidth());
                    inty=(int)(Math.random()*getHeight());
                    g.drawImage(img, x, y, img.getWidth(this), img.getHeight(this), this);
                }
            };
            judge[i] = new JLabel();
        }

        panel.setSize(800,600);
        panel.setLayout(null);

        for(inti=0;i<4;i++){//Place components with coordinates
            lane[i].setBounds (200*i, 0, 200, 500);
            judge[i].setBounds (200*i, 520, 200, 60);
            lane[i].setBackground (new Color (211, 211, 211));
            lane[i].setBorder (new EtchedBorder (EtchedBorder.RAISED));
            judge[i].setBorder(new EtchedBorder(EtchedBorder.RAISED));
            judge[i].setFont(newFont("Arial", Font.PLAIN, 27));
            judge[i].setForeground (new Color (220, 20, 60);
            panel.add(lane[i]);
            panel.add(judge[i]);
        }

        this.panelsize=lane[0].getSize();

        panel.setBackground(new Color(135,206,235);
        panel.addKeyListener(this);

    }

    @ Override
    public void run() {
        while(this.isVisible()){
            //repaint(); // Solution No. 2
            panel.repaint(); // Solution #1
            try{
                Thread.sleep(10);
            } catch(InterruptedExceptione) {
                e.printStackTrace();
            }
        }
    }

    @ Override
    public void keyPressed (KeyEvent arg0) {
        If(arg0.getKeyCode()==KeyEvent.VK_SPACE) {//space and run() call
            Thread thread = new Thread(this);
            US>Execute the contents of thread.start();//this.run()?
        }
    }

    @ Override
    public void keyType(KeyEventke){}

    @ Override
    public void keyReleased (KeyEventke){}
}

Corrects

Points


2022-09-30 16:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.