新创建的图形对象仅在调整框架大小后显示

发布于 2024-12-06 18:17:45 字数 3155 浏览 2 评论 0原文

这是 这篇文章

我在 JPanel 组件上添加和绘制了一组随机大小的图形。我有一个按钮,它将新的绘制对象添加到同一个 JPanel,但在我重新调整窗口大小之前不会显示。我已经添加了 这篇文章 中提到的 EDT 信息,并且还调用了 repaint() 方法成分。我还没有按照 Hovercraft 的建议使用 ArrayList,但我会的。我的大脑需要一边走一边慢慢地理解事物。

谢谢。

该代码分为两个类。

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ZombieDance extends JComponent {

JFrame canvas = new JFrame();
JPanel actionPanel = new JPanel();
JButton newZombie = new JButton("Add new Zombie");

ZombieDance(){
//create a couple default zombies
    buildGUI();
    Random rand = new Random();
    int i,x,y,w,h;

    //add zombies to my canvas
    for (i=1;i<8;i++) {
        float r = rand.nextFloat();
        float g = rand.nextFloat();
        float b = rand.nextFloat();
        x = rand.nextInt(50);
        y = rand.nextInt(50);
        w = rand.nextInt(50);
        h = rand.nextInt(50); 
        canvas.add(new Zombie(x,y,w,h,r,g,b));
    }   
}

//prep the canvas
void buildGUI(){
    actionPanel.add(newZombie);
    canvas.add(actionPanel);
    canvas.setLayout(new GridLayout(3,3));
    canvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    canvas.setSize(400,400);
    canvas.setBackground(Color.WHITE);
    canvas.setVisible(true);

    newZombie.addActionListener(new NewZombieClickHandler());
}

public class NewZombieClickHandler implements ActionListener{
    public void actionPerformed(ActionEvent e){
        Random rand = new Random();
        int x,y,w,h;
        float r = rand.nextFloat();
        float g = rand.nextFloat();
        float b = rand.nextFloat();
        x = rand.nextInt(50);
        y = rand.nextInt(50);
        w = rand.nextInt(50);
        h = rand.nextInt(50); 
        canvas.add(new Zombie(x,y,w,h,r,g,b));
        canvas.repaint();
    }
}   

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
          new ZombieDance();
        }
    });
  }

}

第二节课

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;

public class Zombie extends JPanel{

private int x,y,w,h;
private float r,g,b;

Zombie(int argx, int argy, int argw, int argh, float argr, float argg, float argb){
    x = argx;
    y = argy;
    w = argw;
    h = argh;
    r = argr;
    g = argg;
    b = argb;
}

public Dimension getPreferredSize() {
    return new Dimension(20,20);
}

protected void paintComponent(Graphics gr) {
    super.paintComponent(gr);       
    //g.drawString("Drawing canvas...",10,20);
    gr.setColor(new Color(r,g,b));
    gr.fillRect(x,y,h,w);
} 
}

This is a continuation from this post

I have a set of random sized graphics adding and drawing onto a JPanel component. I have a button that is adding a new draw object to the same JPanel but is not displaying until i re-size the window. I have added the EDT information mentioned in this post and have also called the repaint() method on the component. I am not using an ArrayList yet, as suggested by Hovercraft, but I will. My brain needs to understand things slowly as i go.

Thank you.

The code is in two classes.

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ZombieDance extends JComponent {

JFrame canvas = new JFrame();
JPanel actionPanel = new JPanel();
JButton newZombie = new JButton("Add new Zombie");

ZombieDance(){
//create a couple default zombies
    buildGUI();
    Random rand = new Random();
    int i,x,y,w,h;

    //add zombies to my canvas
    for (i=1;i<8;i++) {
        float r = rand.nextFloat();
        float g = rand.nextFloat();
        float b = rand.nextFloat();
        x = rand.nextInt(50);
        y = rand.nextInt(50);
        w = rand.nextInt(50);
        h = rand.nextInt(50); 
        canvas.add(new Zombie(x,y,w,h,r,g,b));
    }   
}

//prep the canvas
void buildGUI(){
    actionPanel.add(newZombie);
    canvas.add(actionPanel);
    canvas.setLayout(new GridLayout(3,3));
    canvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    canvas.setSize(400,400);
    canvas.setBackground(Color.WHITE);
    canvas.setVisible(true);

    newZombie.addActionListener(new NewZombieClickHandler());
}

public class NewZombieClickHandler implements ActionListener{
    public void actionPerformed(ActionEvent e){
        Random rand = new Random();
        int x,y,w,h;
        float r = rand.nextFloat();
        float g = rand.nextFloat();
        float b = rand.nextFloat();
        x = rand.nextInt(50);
        y = rand.nextInt(50);
        w = rand.nextInt(50);
        h = rand.nextInt(50); 
        canvas.add(new Zombie(x,y,w,h,r,g,b));
        canvas.repaint();
    }
}   

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
          new ZombieDance();
        }
    });
  }

}

Second class

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;

public class Zombie extends JPanel{

private int x,y,w,h;
private float r,g,b;

Zombie(int argx, int argy, int argw, int argh, float argr, float argg, float argb){
    x = argx;
    y = argy;
    w = argw;
    h = argh;
    r = argr;
    g = argg;
    b = argb;
}

public Dimension getPreferredSize() {
    return new Dimension(20,20);
}

protected void paintComponent(Graphics gr) {
    super.paintComponent(gr);       
    //g.drawString("Drawing canvas...",10,20);
    gr.setColor(new Color(r,g,b));
    gr.fillRect(x,y,h,w);
} 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感情洁癖 2024-12-13 18:17:45

我有一个按钮,正在将新的绘制对象添加到同一个 JPanel,但在我重新调整窗口大小之前不会显示

当您将组件添加到可见 GUI 时,代码应该是:(

canvas.add(...);
canvas.validate();
//canvas.repaint(); // sometimes needed

编辑器:更改为 validate< /em>)

I have a button that is adding a new draw object to the same JPanel but is not displaying until i re-size the window

When you add a component to a visible GUI the code should be:

canvas.add(...);
canvas.validate();
//canvas.repaint(); // sometimes needed

(editor: changed to validate)

丘比特射中我 2024-12-13 18:17:45

我自己尝试了一下,发现 PaintChildren() 方法解决了这个问题。

I tried myself and i found that paintChildren() method resolved the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文