![](/img/trans.png)
[英]How to invoke Java Mule component with enum value as method parameter?
[英]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。
我對您的代碼進行了許多更改。
我將您的代碼解壓縮到單獨的GUI,JPanel和TimerTask類中。 這種解壓縮可以更輕松地單獨測試Java應用程序的每個部分。
我將您的主要方法代碼包裝到Runnable中,並調用了SwingUtilities invokeLater方法來啟動Swing GUI。 invokeLater方法將Swing組件的創建和執行放在事件分發線程上 。 Oracle和我要求您通過調用invokeLater方法來啟動每個Swing應用程序。
在guiMethod方法中,我重新排列了JFrame代碼,以便按正確的順序執行。 setVisible方法稱為last。 我使用pack方法創建了JFrame。 除了創建圖形面板時,您根本不應該指定Swing組件的大小。 在那里,您可以像在MyPanel類中一樣設置繪圖面板的大小。
我在您的NewPanel類中創建了一個setEndPoints方法,因此可以將端點從Timer Task傳遞到JPanel。
我在paintComponent方法中添加了對super paintComponent的調用。 這樣可以確保清除繪圖面板,並且Swing油漆鏈完整且完整。
我在計時器任務中添加了另一個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.