求助,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() ;
}
}
uj5u.com熱心網友回復:
救救孩子吧??uj5u.com熱心網友回復:
影片需要用雙快取,不然可定一閃一閃的。
uj5u.com熱心網友回復:
emm,我后來查了查,發現swing組件自動雙快取,并且這個程式的執行緒放錯地方了,所以現在已經解決了,謝謝轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/59179.html
標籤:Java SE
上一篇:Java小白提問,IDEA創建專案,為什么要在module里創建package
下一篇:java界面小白求助
