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

时间:2015-12-06 14:30:29

标签: java swing constructor paintcomponent graphics2d

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

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

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);


    }

}

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

1 个答案:

答案 0 :(得分:2)

您可能将组件添加到JFrame的contentPane中,该组件使用BorderLayout作为其默认布局,因此输入的最后一个组件覆盖所有其他组件,因为它们默认添加到BorderLayout。中心位置。

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

  • 使用一个将JPanel扩展为绘图JPanel的类,并覆盖其paintComponent方法。
  • 将此JPanel添加到您的JFrame。
  • 将gleis类的名称更改为Gleis,使其符合Java命名conventinons,并使其成为逻辑类,而不是GUI组件类,因此不要覆盖paintComponent。
  • 给Gleis一个public void draw(Graphics2D g2)方法。
  • 向绘图JPanel添加尽可能多的Gleis对象,然后通过迭代绘图JPanel所持有的Gleis的ArrayList,在每个Gleis实例上调用draw(...),在JPanel的paintComponent方法中绘制它们。

例如:

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);
    }    
}