Imはキャンバス上で画像を移動しようとしています。wait()とThread.sleep()doft work
import java.awt.*; class GraphicsProgram extends Canvas{
static int up = 0;
public GraphicsProgram(){
setSize(700, 700);
setBackground(Color.white);
}
public static void main(String[] argS){
//GraphicsProgram class is now a type of canvas
//since it extends the Canvas class
//lets instantiate it
GraphicsProgram GP = new GraphicsProgram();
//create a new frame to which we will add a canvas
Frame aFrame = new Frame();
aFrame.setSize(700, 700);
//add the canvas
aFrame.add(GP);
aFrame.setVisible(true);
}
public void paint(Graphics g){
Image img1 = Toolkit.getDefaultToolkit().getImage("Code.jpg");
g.drawImage(img1, up, up, this); }
public void Move() { up = up + 1; Move();
Thread.sleep(2000);
}
}
コンソールは、次に返し
GraphicsProgram.java:43: error: unreported exception InterruptedException; must be caught or declared to be thrown Thread.sleep(2000); ^1 error
私は本当に私はそれを検索しましたし、これは彼らが置かまさにであるように私Thread.sleep()
が動作しない理由を理解することはできません。
:
これは巻き込まれない場合があります例外があることを訴えてコンパイルエラーであること、のtry-catchステートメント、例を使って
Thread.sleep(2000)
を取り巻くしてみてください"私はそれを検索しているので、私のThread.sleep()がうまくいかない理由を本当に理解できません。これは正確に何を入れているのですか?" - ここでは、ペインティングメソッドの途中で眠り。必要な解決策は 'Thread.sleep'ではなくSwing Timerです。 –https://docs.oracle.com/javase/tutorial/essential/exceptions/catchOrDeclare.html – shmosel