救救孩子吧
杂鱼一条 2020-05-30 01:44:42 求助,Java到底怎么做出来个流畅的动画啊,
贴下源码,有点混乱,
关键在run方法那里,我这个flag的值变化经常不正常,本来设置的每次减一,但是增加线程睡眠后,不知道为啥就会减的多,导致画面出错,并且现在运行一会,画面就乱了,旗杆不见了,红旗也不稳定,画面还一闪一闪的。有无大佬解答下咋回事啊
package hw_draw;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class drawhouse extends JFrame implements ActionListener//,Runnable
{
private JButton flagbegin_button;//升旗开始
private JButton flagcut_button;//升旗中断
private JButton sunbegin_button;//太阳开始
private JButton suncut_button;//太阳中断
private Canvas canvas;
Thread moveingthr;
public drawhouse ()
{
super("图形绘制");
Dimension dim = this.getToolkit().getScreenSize();
this.setBounds(dim.width/8,dim.height/8,dim.width/4*3,dim.height/4*3);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);//默认关闭按钮
JPanel cmd = new JPanel();
cmd.setBackground(new Color(0x277FD2B5, true));
this.flagbegin_button = new JButton("升旗开始");
this.flagcut_button = new JButton("升旗中断");
this.sunbegin_button = new JButton("太阳移动");
this.suncut_button = new JButton("太阳中断");
this.flagbegin_button.addActionListener(this);
this.flagcut_button.addActionListener(this);
this.sunbegin_button.addActionListener(this);
this.suncut_button.addActionListener(this);
cmd.add(flagbegin_button);
cmd.add(flagcut_button);
cmd.add(sunbegin_button);
cmd.add(suncut_button);
this.add(cmd,"North");
// this.moveingthr = new Thread(this,"动画");
this.canvas = new HouseCanvas();
this.canvas.setBackground(new Color(0x277FD2B5, true));
this.getContentPane().add(this.canvas,"Center");
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
// if (e.getSource() == flagbegin_button)
}
public void run()
{
}
private static class HouseCanvas extends Canvas implements Runnable
{
private Thread th001;
public void paint(Graphics graphics)
{
super.paint(graphics);
Color purewhite = new Color(0xFFFFFFFF, true);
Color greengrey = new Color(207,222,215);
graphics.setColor(new Color(0x08B7FC));
graphics.fillRect(0,0,1800,330);
graphics.setColor(greengrey);
graphics.fillRect(350, 300, 220, 180);//房子主体长方形
graphics.fillRect(330,480,260,15);//房子底座
graphics.setColor(new Color(146,187,173));
graphics.fillRect(350,380,220,100);//房子下面的墙
graphics.setColor(new Color(117,156,143));
graphics.fillRect(450,330,90,150);//最外层门框
graphics.setColor(purewhite);
graphics.fillRect(460,340,70,140);//中间层门框
graphics.setColor(new Color(0x40B6B6));
graphics.fillRect(465,345,60,135);//内层门框
graphics.setColor(purewhite);
graphics.fillOval(505,410,10,10);//门把手
graphics.setColor(new Color(117,156,143));
graphics.fillRoundRect(350,340,20,15,2,2);//左侧石砖
graphics.fillRoundRect(430,360,20,15,2,2);//门旁石砖
graphics.setColor(new Color(0x4A2C8E8E, true));
graphics.fillRoundRect(500,315,20,15,2,2);//门上方石砖
graphics.setColor(new Color(117,156,143));
graphics.fillRoundRect(365,360,60,60,4,4);//深色窗框
graphics.setColor(purewhite);
graphics.fillRoundRect(370,365,50,50,2,2);//白色内窗框
graphics.setColor(new Color(117,156,143));
graphics.fillRoundRect(375,370,40,40,2,2);//窗户
graphics.setColor(purewhite);
graphics.fillRoundRect(370,387,50,5,1,1);//窗户边
graphics.fillRoundRect(392,365,5,50,1,1);//窗户边
int[] x ={340,580,560,360};
int[] y = {300,300,220,220};
graphics.setColor(new Color(0xFF9A7C));
graphics.fillPolygon(x,y,4);//屋顶
graphics.setColor(new Color(0x759C8F));
graphics.fillRoundRect(385,180,40,60,2,2);
graphics.setColor(purewhite);
graphics.fillOval(410,155,30,15);
graphics.fillOval(430,135,40,20);//烟囱
graphics.setColor(new Color(0xFF957C3E, true));
graphics.fillRoundRect(210,300,40,180,2,2);
graphics.setColor(new Color(0x83F51F));
int[] xtree ={130,330,240,300,240,280,230,180,220,160,220,130};
int[] ytree ={300,300,260,260,220,220,190,220,220,260,260,300};
graphics.fillPolygon(xtree,ytree,12);//树
graphics.setColor(purewhite);
graphics.fillOval(600,135,50,40);
graphics.fillOval(630,130,40,50);
graphics.fillOval(660,130,40,50);
graphics.fillOval(648,107,35,50);
graphics.fillOval(675,140,40,30);
graphics.fillOval(610,110,50,40);
graphics.fillOval(675,110,40,40);//云朵
graphics.setColor(new Color(0xEEFFBF00, true));
graphics.fillOval(960,160,80,80);
int []x1sun = {1001,1026,1030,1042,1057,1066,1056,1045,1020,1003,987,968,949,948,941,946,957,981};
int []y1sun = {255,259,245,225,203,190,171,148,140,136,136,151,171,187,206,225,244,255};
int []x2sun = {1001,1044,1085,1090,1124,1105,1118,1065,1036,1005,980,958,904,919,876,921,919,970};
int []y2sun = {315,290,283,242,204,183,147,125,93,105,92,134,140,180,205,237,281,280};
for (int i=0 ; i<18 ; i++)
graphics.drawLine(x1sun[i],y1sun[i],x2sun[i],y2sun[i]);//太阳
graphics.setColor(new Color(0xD7D6D6));
graphics.fillRect(650,480,200,10);//旗子底座
graphics.setColor(new Color(0xFFFBF1));
graphics.fillRect(745,180,10 ,300 );//旗杆
this.th001 = new Thread(this);
th001.start();
// Thread thmov = new Thread(this);
// thmov.start();
}
@Override
public void update(Graphics g)
{
super.update(g);
}
public void repaint()
{
super.repaint();
}
public void run()
{
Graphics graphics = this.getGraphics();
int []xflag = {755,755,810};
int []yflag = {180,220,200};
graphics.setColor(new Color(0xFF0000));
graphics.fillPolygon(xflag,yflag,3);
// ******异常情况******
while (true)//*
{
try
{
Thread.sleep(1000);
if (yflag[0]>10)
{
yflag[0] -= 1;
yflag[1] -= 1;
yflag[2] -= 1;
}
else
break;
this.repaint();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
//*******异常******
}
}
}
public static void main(String[] args)
{
new drawhouse() ;
}
}