繁体   English   中英

如何在Java中调用Paint Component方法

[英]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类中。 这种解压缩可以更轻松地单独测试Java应用程序的每个部分。

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

  3. 在guiMethod方法中,我重新排列了JFrame代码,以便按正确的顺序执行。 setVisible方法称为last。 我使用pack方法创建了JFrame。 除了创建图形面板时,您根本不应该指定Swing组件的大小。 在那里,您可以像在MyPanel类中一样设置绘图面板的大小。

  4. 我在您的NewPanel类中创建了一个setEndPoints方法,因此可以将端点从Timer Task传递到JPanel。

  5. 我在paintComponent方法中添加了对super paintComponent的调用。 这样可以确保清除绘图面板,并且Swing油漆链完整且完整。

  6. 我在计时器任务中添加了另一个invokeLater方法。 这样可以确保在事件调度线程上重新绘制了JPanel。

这是代码。 我希望这些更改和描述可以帮助您更好地理解“计时器任务”。

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

    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM