Java图形-使用构造函数两次删除旧图形



我有一个程序,它在不同的位置显示一个类的许多元素。为此,我多次使用该类的构造函数。但问题是,只显示了最后一个元素,而没有显示其他元素。

帧的初始化以及构造函数的调用位置:

private void initComponents() {
    setExtendedState(MAXIMIZED_BOTH);
    int width = (int) getContentPane().getBounds().getWidth();
    int height = (int) getContentPane().getBounds().getHeight();
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    add(new gleis(0, 0, 0, 0));
    add(new gleis(1, 0, 1, 0));
    add(new gleis(2, 0, 2, 0));
    this.setVisible(true);
}

名为"gleis"的元素的类:

public class gleis extends JPanel {
    int xposition;
    int yposition;
    int id;
    int status;
    public gleis(int xpos, int ypos, int id, int status){
        this.xposition = xpos * 11;
        this.yposition = ypos * 11 + 4;
        this.id = id;
        this.status = status;
        System.out.println(this.id);
    }
    public void paintComponent(Graphics g1) {
        super.paintComponent(g1);
        Graphics2D g = (Graphics2D) g1;
        g.setColor(Color.black);
        g.fillRect(this.xposition, this.yposition, 11, 4);
        if (this.status == 0)
            g.setColor(Color.gray);
        else if (this.status == 1)
            g.setColor(Color.red);
        else if (this.status == 2)
            g.setColor(Color.yellow);
        else 
            g.setColor(Color.gray);
        g.fillRect(this.xposition + 2, this.yposition + 1, 7, 2);

    }
}

那么,我该如何展示我想要的所有元素呢?

您可能会将组件添加到JFrame的contentPane中,该组件使用BorderLayout作为其默认布局,因此最后输入的组件覆盖了所有其他组件,因为它们是按默认方式添加到BorderLayout.CENTER位置的。

根据你的代码和我对你的用例的假设,我建议改变你的整个方法:

  • 使用一个扩展JPanel的类作为绘图JPanel,并覆盖其paintComponent方法
  • 将这个JPanel添加到您的JFrame中
  • 将gleis类的名称更改为gleis,使其符合Java命名约定,并使其成为逻辑类,而不是GUI组件类,这样就不会让它覆盖paintComponent
  • 给Gleis一个public void draw(Graphics2D g2)方法
  • 将尽可能多的Gleis对象添加到绘图JPanel中,然后在JPanel的paintComponent方法中绘制它们,方法是迭代绘图JPanell所包含的Gleis的ArrayList,在每个Gleis实例上调用draw(...)

例如:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
public class GleisMain {
    private static void createAndShowGui() {
        GleisPainter gleisPainter = new GleisPainter();
        gleisPainter.addGleis(new Gleis(10, 10, 0, 0));
        gleisPainter.addGleis(new Gleis(20, 20, 1, 1));
        gleisPainter.addGleis(new Gleis(30, 30, 2, 2));
        // I try to avoid having classes extend JFrame and instead use JFrames when needed
        JFrame frame = new JFrame("Gleis Example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(gleisPainter);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}
// a single class that extend JPanel and that draws all the Gleis objects
class GleisPainter extends JPanel {
    // an ArrayList to hold all the Gleis objects
    private List<Gleis> gleisList = new ArrayList<>();
    // to allow outside classes to add Gleis objects
    public void addGleis(Gleis gleis) {
        gleisList.add(gleis);
    }
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        // loop through our gleis list and draw each instance
        Graphics2D g2 = (Graphics2D) g;
        for (Gleis gleis : gleisList) {
            gleis.draw(g2);  // by calling its draw(...) method
        }
    }
}
// don't have this extend JPanel
class Gleis {
    // fields should be private
    private int xposition;
    private int yposition;
    private int id;
    private int status;
    public Gleis(int xpos, int ypos, int id, int status) {    
        this.xposition = xpos * 11;
        this.yposition = ypos * 11 + 4;
        this.id = id;
        this.status = status;
        System.out.println(this.id);
    }
    // give this a public draw method where each instance can draw itself
    public void draw(Graphics2D g2) {
        g2.setColor(Color.black);
        // you'll want to avoid "magic" numbers like you're using here:
        g2.fillRect(this.xposition, this.yposition, 11, 4);
        if (this.status == 0)
            g2.setColor(Color.gray);
        else if (this.status == 1)
            g2.setColor(Color.red);
        else if (this.status == 2)
            g2.setColor(Color.yellow);
        else
            g2.setColor(Color.gray);
        // ditto about use of "magic" numbers
        g2.fillRect(this.xposition + 2, this.yposition + 1, 7, 2);
    }    
}

最新更新