作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的第一个java动画,我希望我的标志开始使用repaint(),但是该方法什么也没做,我不知道为什么。这是关于 moveFlag 方法,它应该让旗帜运动起来,让人想起在风中飘扬的旗帜。应用程序只是显示该标志,但不会移动它。
public class Testing extends JPanel implements ActionListener {
private int x,y, xinc, yinc;
public void paintComponent(Graphics g){
xinc = 10;
yinc = 10;
x = 101;
y = 151;
Graphics2D gimg = (Graphics2D) g;
Rectangle2D rect = new Rectangle2D.Double(50,50,10,300);
CubicCurve2D cub1 = new
CubicCurve2D.Double(60,50,x,10,y,100,200,50);
gimg.draw(cub1);
CubicCurve2D cub2 = new
CubicCurve2D.Double(60,100,x,60,y,150,200,100);
gimg.draw(cub2);
CubicCurve2D cub3 = new
CubicCurve2D.Double(60,150,x,110,y,200,200,150);
gimg.draw(cub3);
Line2D l1 = new Line2D.Double(200,50,200,150);
gimg.draw(l1);
GeneralPath gp1 = new GeneralPath();
GeneralPath gp2 = new GeneralPath();
gp1.moveTo(60,50);
gp1.curveTo(x,10,y,100,200,50);
gp1.lineTo(200,100);
gp1.curveTo(y,150,x,60,60,100);
gp1.lineTo(60,50);
gimg.setColor(Color.WHITE);
gimg.fill(gp1);
gimg.setColor(Color.GRAY);
gimg.fill(rect);
gp2.moveTo(60,100);
gp2.curveTo(x,60,y,150,200,100);
gp2.lineTo(200,150);
gp2.curveTo(y,200,x,110,60,150);
gp2.lineTo(60,100);
gimg.setColor(Color.RED);
gimg.fill(gp2);
}
public void moveFlag(){ //waving animation
Timer timer = new Timer(20, this);
timer.start();
x = x + xinc;
y = y + yinc;
if(x<=60||(x+xinc)>=130)
xinc*=-1;
if(y<=50||(y+yinc)>=230)
yinc*=-1;
//revalidate();
repaint();
}
@Override
public void actionPerformed(ActionEvent e) {
}
public static void main(String [] args){
JFrame frame = new JFrame();
Testing test = new Testing();
frame.setContentPane(test);
frame.setSize(500,500);
frame.setVisible(true);
test.moveFlag();
}
}
最佳答案
您的想法是正确的,但某些代码位于错误的位置。我会在评论中解释。
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
public class Testing extends JPanel implements ActionListener {
private int x, y, xinc, yinc;
public void paintComponent(Graphics g) {
// added this statement.
super.paintComponent(g);
Graphics2D gimg = (Graphics2D) g;
Rectangle2D rect = new Rectangle2D.Double(50, 50, 10, 300);
CubicCurve2D cub1 =
new CubicCurve2D.Double(60, 50, x, 10, y, 100, 200, 50);
gimg.draw(cub1);
CubicCurve2D cub2 =
new CubicCurve2D.Double(60, 100, x, 60, y, 150, 200, 100);
gimg.draw(cub2);
CubicCurve2D cub3 =
new CubicCurve2D.Double(60, 150, x, 110, y, 200, 200, 150);
gimg.draw(cub3);
Line2D l1 = new Line2D.Double(200, 50, 200, 150);
gimg.draw(l1);
GeneralPath gp1 = new GeneralPath();
GeneralPath gp2 = new GeneralPath();
gp1.moveTo(60, 50);
gp1.curveTo(x, 10, y, 100, 200, 50);
gp1.lineTo(200, 100);
gp1.curveTo(y, 150, x, 60, 60, 100);
gp1.lineTo(60, 50);
gimg.setColor(Color.WHITE);
gimg.fill(gp1);
gimg.setColor(Color.GRAY);
gimg.fill(rect);
gp2.moveTo(60, 100);
gp2.curveTo(x, 60, y, 150, 200, 100);
gp2.lineTo(200, 150);
gp2.curveTo(y, 200, x, 110, 60, 150);
gp2.lineTo(60, 100);
gimg.setColor(Color.RED);
gimg.fill(gp2);
}
public void moveFlag() { // waving animation
Timer timer = new Timer(20, this);
timer.start();
// moved the next four statements from paintComponents. They
// are initializations. In paintComponent you kept resetting them.
xinc = 10;
yinc = 10;
x = 101;
y = 151;
repaint();
}
@Override
public void actionPerformed(ActionEvent e) {
//moved all of these from moveFlag to here. This is called by
//the timer event to update the x and y coordinates.
x = x + xinc;
y = y + yinc;
if (x <= 60 || (x + xinc) >= 130)
xinc *= -1;
if (y <= 50 || (y + yinc) >= 230)
yinc *= -1;
// revalidate();
repaint();
}
public static void main(String[] args) {
JFrame frame = new JFrame();
Testing test = new Testing();
frame.setContentPane(test);
frame.setSize(500, 500);
frame.setVisible(true);
test.moveFlag();
}
}
我必须添加的唯一代码是 super.paintComponent(g) 调用。这是必要的,因为您要重写paintComponent(),您需要确保在绘制图形之前调用父方法。
关于java - 为什么当我想重新绘制我的画时 repaint() 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56351446/
使用登录后,我想吐出用户名。 但是,当我尝试单击登录按钮时, 它给了我力量。 我看着logcat,但是什么也没显示。 这种编码是在说。 它将根据我在登录屏幕中输入的名称来烘烤用户名。 不会有任何密码。
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎是题外话,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或include a min
我是一名优秀的程序员,十分优秀!