0
這是通過單擊清潔和污垢按鈕的初始狀態。 Initial Image如何延遲顯示Java中按鈕上的圖像
輸出將是這樣的。 Final Image
但我想顯示每個按鈕3秒延遲的圖像。也就是第二臺吸塵器在第一臺吸塵器出現3秒鐘後出現,第二臺吸塵器從第二張圖像開始3秒後出現。等等。我用Thread.sleep()。但每次只顯示一個結果。 我已經搜索瞭解決方案,但無法管理它。
對不起英語不好。 這是我的代碼。
package vacuumcleaner;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.PopupMenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import static java.lang.Math.abs;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**
*
* @author Md.Rezwanul Islam
*/
public class VC {
final int sz = 7;
int board [][] = new int [sz*100][sz*100], dirtclick=0,cleanerclick=0,noway=0,cnt=0,s=0,vcnt=0;
int dirtx[] = new int[sz*100], dirty[]=new int[sz*100], dis[][]=new int[sz*100][sz*100];
int startx=0,starty=0;
int x=0,y=0;
JFrame frm = new JFrame("Clean It !");
JPanel container = new JPanel();
JPanel buttonholder = new JPanel();
JPanel info = new JPanel();
JPanel dirtP= new JPanel();
JPanel cleanerP=new JPanel();
JPanel playP = new JPanel();
JPanel resetP= new JPanel();
JButton btn[][] = new JButton[sz][sz];
JButton dirt=new JButton("Dirt");
JButton cleaner=new JButton("Cleaner");
JButton play=new JButton("Clean it !");
JButton reset=new JButton("Reset");
ImageIcon dirt1= new ImageIcon("C:\\Users\\Md.Rezwanul Islam\\Documents\\NetBeansProjects\\VacuumCleaner\\Images\\Dirt.png");
ImageIcon cleaner1=new ImageIcon("C:\\Users\\Md.Rezwanul Islam\\Documents\\NetBeansProjects\\VacuumCleaner\\Images\\VacuumCleaner.png");
ImageIcon dirt2= new ImageIcon("C:\\Users\\Md.Rezwanul Islam\\Documents\\NetBeansProjects\\VacuumCleaner\\Images\\dirtBig.png");
ImageIcon cleaner2=new ImageIcon("C:\\Users\\Md.Rezwanul Islam\\Documents\\NetBeansProjects\\VacuumCleaner\\Images\\cleanerBig.png");
JLabel label_dirt=new JLabel(dirt1);
JLabel label_cleaner=new JLabel(cleaner1);
VC(){
dirt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
for (int i = 0; i < sz; i++) {
for (int j = 0; j < sz; j++) {
btnactiondirt(btn[i][j], i, j);
}
}
}
});
cleaner.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae){
for (int i = 0; i < sz; i++) {
for (int j = 0; j < sz; j++) {
btnactioncleaner(btn[i][j], i, j);
}
}
}
});
play.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae){
noway = 1;
disout();
}
});
reset.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae){
frm.dispose();
VC dis = new VC();
}
});
for(int i=0;i<sz;i++)
{
for(int j=0;j<sz;j++)
{
btn[i][j]=new JButton();
buttonholder.add(btn[i][j]);
}
}
buttonholder.setPreferredSize(new Dimension(700, 700));
buttonholder.setLayout(new GridLayout(sz, sz));
dirtP.setBackground(Color.LIGHT_GRAY);
dirtP.add(dirt);
dirtP.add(label_dirt);
cleanerP.setBackground(Color.LIGHT_GRAY);
cleanerP.add(cleaner);
cleanerP.add(label_cleaner);
playP.setBackground(Color.LIGHT_GRAY);
playP.add(play);
resetP.setBackground(Color.LIGHT_GRAY);
resetP.add(reset);
info.setLayout(new BoxLayout(info, BoxLayout.X_AXIS));
info.add(dirtP);
info.add(cleanerP);
info.add(playP);
info.add(resetP);
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
container.add(buttonholder);
container.add(info);
frm.add(container);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setSize(700, 700);
frm.setResizable(false);
frm.setLocationRelativeTo(null);
frm.setVisible(true);
}
final void btnactiondirt(JButton tmpbtn, int a,int b) {
tmpbtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
dirtclick++;
setdirt(a,b);
}
});
}
final void btnactioncleaner(JButton tmpbtn, int a,int b) {
tmpbtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
cleanerclick+=1;
setcleaner(a,b);
}
});
}
void setcleaner(int a,int b)
{
if(cleanerclick==1){
btn[a][b].setIcon(cleaner2);
board[a][b]=11;
startx=a;
starty=b;
}
}
void setdirt(int a, int b)
{
if(dirtclick>0 & noway==0){
cnt+=1;
btn[a][b].setIcon(dirt2);
dirtx[x]=a;
dirty[y]=b;
dis[dirtx[x]][dirty[y]]=cntdis(dirtx[x],dirty[y]);
x++;
y++;
}
}
int cntdis(int a,int b){
int dcnt=0;
int tstartx=startx;
int tstarty=starty;
for(;;){
if(tstartx==a)
{
if(tstarty==b)
{
dcnt++;
break;
}
if(tstarty<b)
{
tstarty++;
}
else
tstarty--;
dcnt++;
}
else if(tstarty==b)
{
if(tstartx==a)
{
dcnt++;
break;
}
if(tstartx<a)
{
tstartx++;
}
else
tstartx--;
dcnt++;
}
else if(tstartx>a && tstarty>b){
tstartx--;
tstarty--;
dcnt++;
}
else if(tstartx>a && tstarty<b){
tstartx--;
tstarty++;
dcnt++;
}
else if(tstartx<a && tstarty>b){
tstartx++;
tstarty--;
dcnt++;
}
else if(tstartx<a && tstarty<b){
tstartx++;
tstarty++;
dcnt++;
}
}
return dcnt-1;
}
void check(int desx, int desy)
{
for(;;){
if(startx==desx)
{
if(starty==desy)
{
vcnt++;
break;
}
if(starty<desy)
{
starty++;
}
else
starty--;
btn[startx][starty].setIcon(cleaner2);
board[startx][starty]=1;
}
else if(starty==desy)
{
if(startx==desx)
{
vcnt++;
break;
}
if(startx<desx)
{
startx++;
}
else
startx--;
btn[startx][starty].setIcon(cleaner2);
board[startx][starty]=1;
}
else if(startx>desx && starty>desy){
startx--;
starty--;
}
else if(startx>desx && starty<desy){
startx--;
starty++;
}
else if(startx<desx && starty>desy){
startx++;
starty--;
}
else if(startx<desx && starty<desy){
startx++;
starty++;
}
btn[startx][starty].setIcon(cleaner2);
board[startx][starty]=1;
}
}
void disout() {
for (int ii = 0; ii < cnt; ii++) {
int max = 100;
int tx = 0;
int ty = 0;
for (int i = 0; i < sz; i++) {
for (int j = 0; j < sz; j++) {
if (dis[i][j] > 0 && dis[i][j] < max) {
tx = i;
ty = j;
max = dis[i][j];
}
}
}
dis[tx][ty] = 0;
check(tx, ty);
}
}
}