如何让绿色圆圈涂抹? [英] How to get the green circle to smear?
问题描述
我正在尝试制作一个简单的动画,其中一个绿色圆圈以污迹的对角线在名为 panel
的小部件上以涂抹的方式移动,该小部件是 class MyPanel
的一个实例扩展了 JPanel
.
I am trying to do a simple animation, in which a green circle moves diagonally in a smeared pattern on a widget named panel
which is an instance of a class MyPanel
which extends JPanel
.
JFrame
有一个开始按钮,当按下该按钮时,它应该通过调用 actionPerformed
方法(在此方法中称为animate方法,该动画方法调用动画来开始动画). repaint
方法,同时在本身是侦听器的主类中依次增加圆的x和y坐标.
The JFrame
has a start button, which when pressed is supposed to start the animation by calling the actionPerformed
method (in which I call the animate method, which calls the repaint
method while successively incrementing the x and y coordinates of the circle) in the main class which itself is the listener.
相反,当按下按钮时,该圆在初始坐标处显示,然后在延迟之后另一个圆在最终坐标处显示.有人可以帮我弄清楚我要去哪里错吗?我是Java的初学者,几年前用C做过一些基本编程.
Instead when the button is pressed, the circle shows up at the initial coordinates, and then after a delay another circle shows up at the final coordinates. Could someone please help me figure out where I am going wrong? I am a beginner in Java, who has done some basic programming in C years ago.
先谢谢了.这是我的代码:
Thanks in advance. Here is my code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Smear implements ActionListener{
JFrame frame;
MyPanel panel;
JButton button;
Smear animgui1;
int x=70;
int y=70;
public static void main(String[] args) {
Smear animgui=new Smear();
animgui.project();
animgui.set(animgui);
}
public void set(Smear anim) {
animgui1=anim;
}
public void project() {
frame=new JFrame();
panel=new MyPanel();
button=new JButton("Start");
button.addActionListener(this);
frame.getContentPane().add(BorderLayout.NORTH, button);
frame.getContentPane().add(BorderLayout.CENTER, panel);
frame.setSize(300,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void animate() {
while(x!=200) {
panel.repaint();
x++;
y++;
System.out.println("++++");
try {
Thread.sleep(50);
}
catch(Exception ex) {};
}
}
public void actionPerformed(ActionEvent event) {
animgui1.animate();
}
class MyPanel extends JPanel {
public void paintComponent(Graphics g) {
g.setColor(Color.green);
g.fillOval(x,y,40,40);
}
}
}
但是,与此同时,我制作了另一个没有该按钮的程序SmearGui(我删除了与按钮和侦听器有关的代码),并且可以按预期的方式工作;圆以涂抹模式缓慢移动.的代码是:
But at the same time, I have made another program SmearGui without that button(I removed the code pertaining to the button and the listener), and it works the way its intended to; the circle slowly moves in a smear pattern. The code for that is:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SmearGui{
JFrame frame;
MyPanel panel;
//JButton button;
SmearGui animgui1;
int x=70;
int y=70;
public static void main(String[] args){
SmearGui animgui=new SmearGui();
animgui.project();
animgui.set(animgui);
animgui.animate();
}
public void set(SmearGui anim){
animgui1=anim;
}
public void project(){
frame=new JFrame();
panel=new MyPanel();
//button=new JButton("Start");
//button.addActionListener(this);
//frame.getContentPane().add(BorderLayout.NORTH, button);
frame.getContentPane().add(BorderLayout.CENTER, panel);
frame.setSize(300,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void animate(){
while(x!=200){
panel.repaint();
x++;
y++;
try{
Thread.sleep(50);
}
catch(Exception ex){};
}
}
/*public void actionPerformed(ActionEvent event){
animgui1.animate();
}*/
class MyPanel extends JPanel{
public void paintComponent(Graphics g){
g.setColor(Color.green);
g.fillOval(x,y,40,40);
}
}
}
上面的代码将animate方法放置在主体本身中.
The code above places the animate method in the main itself.
推荐答案
重新绘制是异步的,因此您的代码无需等待面板重新绘制即可继续.您的循环代码比重新绘制面板快得多.使用一个摆动计时器,该计时器在重绘发生的同一线程上执行,这样您就不会在计算时序和重绘时序上出现这种不匹配.
repaint is asynchronous, so your code doesn't wait for the panel to redraw before continuing. Your loop code is significantly faster than redrawing the panel. Use a swing timer which is executed on the same thread that the repaint occurs, so that you don't have this mismatch in calculation vs. redraw timings.
这篇关于如何让绿色圆圈涂抹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!