簡體   English   中英

repaint()函數-JAVA Swing

[英]repaint() function - JAVA Swing

我在嘗試使用Timer對象和調用repaint()時遇到問題。

這是我的窗口類:

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

    public class Window extends JFrame{


    Panel pan = new Panel();
    JPanel container, north,south, west;
    JButton ip,print,cancel,ok;
    JTextArea timeStep;
    JLabel legend;
    double temperature=0.0;

    public static void main(String[] args) {
        new Window();

    }

    public Window()
    {
        System.out.println("je suis là");
        this.setSize(700,400);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.setTitle("Assignment2 - CPU temperature");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        container = new JPanel(new BorderLayout());

        north = new JPanel();
        ip = new JButton ("New");
        north.add(ip);
        north.add(new JLabel("Time Step: "));
        timeStep = new JTextArea("10",1,5);
        north.add(timeStep);
        print = new JButton ("Print");
        north.add(print);

        south = new JPanel();
        legend = new JLabel("Legends are here");
        south.add(legend);

        west = new JPanel();
        JLabel temp = new JLabel("°C");
        west.add(temp);

        container.add(north, BorderLayout.NORTH);
        container.add(west,BorderLayout.WEST);
        container.add(pan, BorderLayout.CENTER);
        container.add(south, BorderLayout.SOUTH);

        this.setContentPane(container);
        this.setVisible(true);
    }

}

這是我的Panel課:

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

    public class Panel extends JPanel implements ActionListener {

    Timer chrono = new Timer(1000,this);
    int i = 0;
    int t = 10;

    public Panel()
    {

        super();
        chrono.start();

    }

    public void paintComponent(Graphics g)
    {

        g.drawLine(20, 20, 20, this.getHeight()-50);
        g.drawLine(20, this.getHeight()-50, this.getWidth()-50, this.getHeight()-50);
        g.drawLine(20, 20, 15, 35);
        g.drawLine(20, 20, 25, 35);
        g.drawLine(this.getWidth()-50, this.getHeight()-50, this.getWidth()-65, this.getHeight()-45);
        g.drawLine(this.getWidth()-50, this.getHeight()-50, this.getWidth()-65, this.getHeight()-55);
        g.drawLine(20+t, this.getHeight()-50-i, 20+t, this.getHeight()-50);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        /*i = (int)(50+(20 + (Math.random() * (60 - 20))));
        t = t+10;
        repaint();
        System.out.println("chrono");*/
    }

}

這是不調用repaint()函數時的樣子:

界面:

最后是調用repaint()函數時的樣子:

GUI錯誤:

似乎我所有的JPanels都只重新繪制一次,然后一切正常...

任何想法?

問題是您永遠都不會繪制之前繪制的內容。 默認情況下, JPanel應該是不透明的,這意味着它將在每次重繪時在其整個區域上繪制,從而釋放了框架,不必擔心清理該空間。 但是,您正在從JPanel刪除該功能。

paintComponent方法中,將其添加到頂部:

super.paintComponent(g);

這將使JPanel正確重繪自身(通過調用其自己的paintComponent方法),以便您可以在干凈的板上進行繪制。

根據您的評論,保留舊線條的最佳方法是跟蹤要繪制的每條線條,並每次都重新繪制它們。 為此,您需要將ti替換為列表,如下所示:

List<Integer> is = new ArrayList<>();
List<Integer> ts = new ArrayList<>();
int lastT = 0;

然后添加到其中,而不是簡單地設置值:

int i = (int)(50 + (20 + (Math.random() * (60 - 20))));
lastT += 10;
is.add(i);
ts.add(lastT);
repaint();

最后,遍歷每個值:

for(int j = 0; i < is.size() && ts.size(); i++){
    int i = is.get(j);
    int t = ts.get(j);
    g.drawLine(20 + t, this.getHeight() - 50 - i,
               20 + t, this.getHeight() - 50);
}

我沒有測試上面的代碼,因此可能會有一些錯誤,但這應該是正確的想法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM