名前:上原和樹
番号:065709D
所属:情報工学科1年次
// Animation.java import java.applet.*; import java.awt.*; import java.awt.event.*; public class anima03 extends Applet { MyThread thread [] = {null, null, null}; int x [] = {50, 150, 250}; int y [] = {200, 150, 100}; int width, height, shift; Color color [] = {Color.red, Color.pink, Color.magenta, Color.blue, Color.cyan, Color.green}; int col [] = {0, 3, 5}; Button resumeButton, suspendButton; boolean threadSuspended = false; public void init(){ shift = 30; width = getSize().width; height = getSize().height - (shift + 50); resumeButton = new Button("Resume"); suspendButton = new Button("Suspend"); add(resumeButton); add(suspendButton); resumeButton.addActionListener(new ActionAdp()); suspendButton.addActionListener(new ActionAdp()); } public void start() { for (int i = 0; i < 3; i++) { if (thread[i] == null) { thread[i] = new MyThread(i, y[i]); thread[i].start(); } } } public void stop() { for (int i = 0; i < 3; i++) { if (thread[i] != null) { thread[i] = null; } } } public void paint(Graphics g) { g.setColor(color[col[0]]); g.fillOval(x[0], y[0], 50, 50); g.setColor(color[col[1]]); g.fillOval(x[1], y[1], 50, 50); g.setColor(color[col[2]]); g.fillOval(x[2], y[2], 50, 50); g.setColor(Color.black); g.drawLine(0, height + 79, width, height + 79); } class ActionAdp implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getSource() == resumeButton) { threadSuspended = false; myResume(); } else if (e.getSource() == suspendButton) { threadSuspended = true; } } } void myResume() { for (int i = 0; i < 3; i++) { thread[i].myResume(); } } class MyThread extends Thread { int index, maxHeight, shift; double time, maxTime; MyThread(int index, int maxHeight) { this.index = index; this.maxHeight = maxHeight; time = 0.0; shift = height - maxHeight; maxTime = Math.sqrt(maxHeight / 100.); } public void run() { Thread thisThread = Thread.currentThread(); while (thread[index] == thisThread) { while (threadSuspended) { synchronized(this) { try { wait(); } catch (InterruptedException e) { } } } repaint(); try { if (time >= maxTime) { time = -maxTime; } y[index] = (int)(100*time*time+30+shift); time += 0.05; this.sleep(50); }catch (InterruputedException 3) { } } public synchronized void my Resume() { notify(); } } } |
multi.java |
---|
import java.applet.*; import java.awt.*; import java.awt.event.*; public class multi extends Applet { MyThread thread [] = {null, null, null}; int x [] = {50, 150, 250}; int y [] = {200, 150, 100}; int width, height; int shift = 10; Color color [] = {Color.red, Color.blue, Color.cyan}; int col [] = {0,1,2}; Button resumeButton, suspendButton; boolean threadSuspended = false; public void init(){ shift = 30; width = getSize().width; height = getSize().height - (shift + 50); suspendButton = new Button("Suspend"); add(resumeButton); add(suspendButton); resumeButton.addActionListener(new ActionAdp()); suspendButton.addActionListener(new ActionAdp()); } public void start() { for (int i = 0; i<3; i++) { if(thread[i] == null){ thread[i] = new MyThread(i, y[i]); thread[i].start(); } } } public void stop() { for (int i = 0;i<3; i++){ if(thread[i] != null) { thread[i] = null; } } public void paint(Graphics g) { g.setColor(color[col[0]]); g.fillOval(x[0], y[0], 50, 50); g.setColor(color[col[1]]); g.fillOval(x[1], y[1], 50, 50); g.setColor(color[col[2]]); g.fillOval(x[2], y[2], 50, 50); g.setColor(Color.black); g.drawLine(0, height + 79, width, height + 79); } class ActionAdp implements ActionListener { public void actionPerformed(ActionEvent e){ if(e.getSource() == resumeButton){ threadSuspended = false; mResume(); }else if(e.getSource() == suspendButton){ threadSuspended = true; } } } void mResume() { for (int i=0;i<3;i++) { thread[i].mResume(); } } class MyThread extends Thread { int id,maxHeight, shift; double time, maxTime; MyThread(int id, int maxHeight){ this.id = id; this.maxHeight = maxHeight; time = 0.0; shift = height - maxHeight; maxTime = Math.sqrt(maxHeight / 100.); } public void run(){ Thread thisThread = Thread.currentThread(); while(thread[id] == thisThread){ while (threadSuspended) { synchronized(this){ try { wait(); }catch (InterruptedException e){ } } } repaint(); try{ x[0] += 2; if (x[0] >= width) { x[0] = 0; } x[1] += 4; if (x[1] >= width){ x[1] = 0; } x[2] += 6; if (x[2] >= width) { x[2] = 0; } if (time >= maxTime) { time = -maxTime; } y[id] = (int)(100*time*time+30+shift); time += 0.05; thisThread.sleep(50); }catch (InterruptedException e) {} } } public synchronized void mResume() { notify(); } } } |