如何在 java 中调用 Paint 组件方法

How to Invoke Paint Component Method in java

如何调用paintComponent(Graphics g)方法??

method(){
Timer timer = new Timer();
timer.schedule(new Class(), 1000,1000);
}
public void run() {
//invoke PaintComponent 
}

这里!这是更新后的最小代码 我想简单地每秒更改一次线的终点 如何调用 paintComponent(Graphics g) 方法 ??

public class SimpleTest extends TimerTask {

JFrame frame;
int count ,x1,y1,x2,y2;
public void run() {

  count+=5;
  x1=count;
  y1=count;
  x2=count-1;
  y2=count-1;
  // repaint();   i want to invoke paintcomponent method from here , simply to change the end point of line every secd
}
void guiMethod(){
     frame=new JFrame("Libra's");
    frame.setBounds(50, 50, 250, 250);
    frame.setVisible(true);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new NewPanel());
}
public static void main(String[] args){
SimpleTest c=new SimpleTest();
   c.guiMethod();

   Timer timer = new Timer();
   timer.schedule(new SimpleTest(), 1000,1000);
}
}
class NewPanel extends JPanel{
SimpleTest obj=new SimpleTest();
@Override
protected void paintComponent(Graphics g){

  Graphics2D g2=(Graphics2D)g;
  g2.setStroke(new BasicStroke(3));

  g.drawLine(120, 120, 70, 80);


  g.drawLine(120, 120, obj.x1, obj.y1);

  g.drawLine(120, 120, obj.x2, obj.y2);
}}

这是我根据您的 GUI 创建的 GUI。

我对你的代码做了很多修改。

  1. 我将您的代码解压缩到单独的 GUI、JPanel 和 TimerTask classes 中。这种解包可以更轻松地单独测试 Java 应用程序的每个部分。

  2. 我将您的主要方法代码包装到一个 Runnable 中,并调用了 SwingUtilities invokeLater 方法来启动您的 Swing GUI。 invokeLater 方法将 Swing 组件的创建和执行放在 Event Dispatch thread 上。 Oracle 和我要求您在启动每个 Swing 应用程序时调用 invokeLater 方法。

  3. 在 guiMethod 方法中,我重新排列了 JFrame 代码,以便它以正确的顺序执行。最后调用 setVisible 方法。我使用 pack 方法来创建 JFrame。您根本不应该指定 Swing 组件的大小,除非您创建绘图面板。在那里,您可以像我在 MyPanel class.

  4. 中那样设置绘图面板的大小
  5. 我在您的 NewPanel 中创建了一个 setEndPoints 方法 class 这样我就可以将结束点从您的 Timer Task 传递到 JPanel。

  6. 我在您的 paintComponent 方法中添加了对超级 paintComponent 的调用。这样可以确保绘图面板被清除,Swing 绘图链完整无缺。

  7. 我向您的计时器任务添加了另一个 invokeLater 方法。这确保了 JPanel 在 Event Dispatch 线程上重新绘制。

这是代码。我希望这些更改和描述能帮助您更好地理解使用 Timer Task。

package com.ggl.testing;

import java.awt.BasicStroke;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class SimpleTest {

    private JFrame frame;
    private NewPanel newPanel;

    public void guiMethod() {
        frame = new JFrame("Libra's");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        newPanel = new NewPanel();
        newPanel.setEndPoints(200, 100, 100, 200);
        frame.add(newPanel);

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                SimpleTest c = new SimpleTest();
                c.guiMethod();

                Timer timer = new Timer();
                timer.schedule(c.new SimpleTimerTask(), 1000, 1000);
            }
        };
        SwingUtilities.invokeLater(runnable);
    }

    public class NewPanel extends JPanel {
        private static final long serialVersionUID = -4695412639313981349L;

        private int x1, y1, x2, y2;

        public NewPanel() {
            this.setPreferredSize(new Dimension(250, 250));
        }

        public void setEndPoints(int x1, int y1, int x2, int y2) {
            this.x1 = x1;
            this.y1 = y1;
            this.x2 = x2;
            this.y2 = y2;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            Graphics2D g2 = (Graphics2D) g;
            g2.setStroke(new BasicStroke(3));
            g.drawLine(75, 75, 10, 10);
            g.drawLine(75, 75, x1, y1);
            g.drawLine(75, 75, x2, y2);
        }
    }

    public class SimpleTimerTask extends TimerTask {        
        int count;

        @Override
        public void run() {
            count += 5;
            final int x1 = 200 + count;
            final int y1 = 100 + count;
            final int x2 = 100 + count;
            final int y2 = 200 + count;
            // repaint(); i want to invoke paint component method from here , simply
            // to change the end point of line every second
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    newPanel.setEndPoints(x1, y1, x2, y2);
                    newPanel.repaint();
                }
            };
            SwingUtilities.invokeLater(runnable);
        }

    }

}