import java.awt.*; // AWT(AbstractWindowingToolkit): Graphics import java.awt.image.*; // BufferedImage など import java.io.*; // File など import javax.swing.*; // Window, GUI: JPanel, JFrame import javax.imageio.*; // ImageIO public class Ex3_2 extends JPanel implements Runnable { int x, y; // 描画位値用(x,y)座標 double ang; // 角度ang static int wid, hei; // 画面サイズ:幅wid x 高さhei BufferedImage img = null; Thread th = null; // スレッド Ex3_2() { // コンストラクタ try { img = ImageIO.read(new File("img/sub4_ani.gif")); // 画像の読み込み } catch (Exception e) { e.printStackTrace(); } x = 50; y = 100; ang = 0.0; wid = 400; hei = 300; if (th == null) { th = new Thread(this); th.start(); // スレッドの開始 } } public void paintComponent(Graphics g) { if (img != null) { g.drawImage(img, x, y, this); // 画像の描画 } } public static void main(String[] args) { JFrame app = new JFrame(); app.add(new Ex3_2()); app.setSize(wid, hei); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); app.setVisible(true); } public void run() { while (th != null) { try { Thread.sleep(150); } catch (Exception e) { } x = (x+10) % wid; ang += Math.PI / 12; y = 100 + (int)(50*Math.cos(ang)); repaint(); } th = null; } }