HW972 Wig MovingObject


このページのコンテンツ一覧(5個位までにしてください。上が最新で下が最古です)

宿題スレPart53(ダブルバッファリング Wig.java のみ修正UP) >>972

	
	// Wig.java
	// <applet code="Wig.class" width="640" height="480"></applet>
	
	import javax.swing.*;
	import java.awt.*;
	
	public class Wig extends JApplet implements Runnable{
		Thread redThread;
		Thread greenThread;
		Thread blueThread;
		MovingObject redBall = new MovingObject(0, 20, 20, 20, 10);
		MovingObject greenBall = new MovingObject(60, 40, 15, 15, 15);
		MovingObject blueBall = new MovingObject(30, 80, -10, -10, 20);
		Image buffer;
		int interval = 1;
	
		public void start(){
			redThread = new Thread(this);
			greenThread = new Thread(this);
			blueThread = new Thread(this);
			redThread.start();
			greenThread.start();
			blueThread.start();
			Dimension d = getSize();
			buffer = createImage(d.width, d.height);
		}
	
		public void run(){
			MovingObject obj, o, p;
			if(Thread.currentThread() == redThread){
				obj = redBall;
				o = greenBall;
				p = blueBall;
			}else if(Thread.currentThread() == greenThread){
				obj = greenBall;
				o = blueBall;
				p = redBall;
			}else{
				obj = blueBall;
				o = redBall;
				p = greenBall;
			}
			while(true){
				{
					try{
						Thread.sleep(50);
					}catch(InterruptedException e){}
				}
				if(obj.x + obj.dx < 0 || getWidth() < obj.x + obj.dx + obj.width
				|| (obj.x + obj.width >= o.x && obj.x < o.x + o.width
					&& obj.y + obj.width >= o.y && obj.y < o.y + o.width)
				|| (o.x + o.width >= p.x && o.x < p.x + p.width
					&& o.y + o.width >= p.y && o.y < p.y + p.width)
				|| (p.x + p.width >= obj.x && p.x < obj.x + obj.width
					&& p.y + p.width >= obj.y && p.y < obj.y + obj.width))
					obj.dx = -obj.dx;
				obj.x += obj.dx;
				if(obj.y + obj.dy < 0 || getHeight() < obj.y + obj.dy + obj.width
				|| (obj.x + obj.width >= o.x && obj.x < o.x + o.width
					&& obj.y + obj.width >= o.y && obj.y < o.y + o.width)
				|| (o.x + o.width >= p.x && o.x < p.x + p.width
					&& o.y + o.width >= p.y && o.y < p.y + p.width)
				|| (p.x + p.width >= obj.x && p.x < obj.x + obj.width
					&& p.y + p.width >= obj.y && p.y < obj.y + obj.width))
					obj.dy = -obj.dy;
				obj.y += obj.dy;
				repaint();
			}
		}
	
		public void paint(Graphics g){
			Graphics bg = g;
			g = buffer.getGraphics();
	
			super.paint(g);
			redBall.show(g, Color.RED);
			greenBall.show(g, Color.GREEN);
			blueBall.show(g, Color.BLUE);
	
			g = bg;
			g.drawImage(buffer, 0, 0, this);
		}
	}
	

宿題スレPart53(3個に対応 Wig.java のみ修正UP) >>972

	// Wig.java
	
	import javax.swing.*;
	import java.awt.*;
	
	public class Wig extends JApplet implements Runnable{
		MovingObject redBall = new MovingObject(0, 20, 20, 20, 10);
		MovingObject greenBall = new MovingObject(60, 40, 15, 15, 15);
		MovingObject blueBall = new MovingObject(30, 80, -10, -10, 20);
		Thread redThread = new Thread(this);
		Thread greenThread = new Thread(this);
		Thread blueThread = new Thread(this);
		int interval = 1;
	
		public void start(){
			redThread = new Thread(this);
			greenThread = new Thread(this);
			blueThread = new Thread(this);
			redThread.start();
			greenThread.start();
			blueThread.start();
		}
	
		public void run(){
			MovingObject obj, o, p;
			if(Thread.currentThread() == redThread){
				obj = redBall;
				o = greenBall;
				p = blueBall;
			}else if(Thread.currentThread() == greenThread){
				obj = greenBall;
				o = blueBall;
				p = redBall;
			}else{
				obj = blueBall;
				o = redBall;
				p = greenBall;
			}
			while(true){
				{
					try{
						Thread.sleep(50);
					}catch(InterruptedException e){}
				}
				if(obj.x + obj.dx < 0 || getWidth() < obj.x + obj.dx + obj.width
				|| (obj.x + obj.width >= o.x && obj.x < o.x + o.width
					&& obj.y + obj.width >= o.y && obj.y < o.y + o.width)
				|| (o.x + o.width >= p.x && o.x < p.x + p.width
					&& o.y + o.width >= p.y && o.y < p.y + p.width)
				|| (p.x + p.width >= obj.x && p.x < obj.x + obj.width
					&& p.y + p.width >= obj.y && p.y < obj.y + obj.width))
					obj.dx = -obj.dx;
				obj.x += obj.dx;
				if(obj.y + obj.dy < 0 || getHeight() < obj.y + obj.dy + obj.width
				|| (obj.x + obj.width >= o.x && obj.x < o.x + o.width
					&& obj.y + obj.width >= o.y && obj.y < o.y + o.width)
				|| (o.x + o.width >= p.x && o.x < p.x + p.width
					&& o.y + o.width >= p.y && o.y < p.y + p.width)
				|| (p.x + p.width >= obj.x && p.x < obj.x + obj.width
					&& p.y + p.width >= obj.y && p.y < obj.y + obj.width))
					obj.dy = -obj.dy;
				obj.y += obj.dy;
				repaint();
			}
		}
	
		public void paint(Graphics g){
			super.paint(g);
			redBall.show(g, Color.RED);
			greenBall.show(g, Color.GREEN);
			blueBall.show(g, Color.BLUE);
		}
	}
	

宿題スレPart53(Wig MovingObject?) >>972

	// Wig.java
	
	import javax.swing.*;
	import java.awt.*;
	
	public class Wig extends JApplet implements Runnable{
		MovingObject redBall = new MovingObject(0, 20, 20, 20, 10);
		MovingObject blueBall = new MovingObject(30, 80, -10, -10, 20);
		Thread redThread = new Thread(this);
		Thread blueThread = new Thread(this);
		int interval = 1;
	
		public void start(){
			redThread = new Thread(this);
			blueThread = new Thread(this);
			redThread.start();
			blueThread.start();
		}
	
		public void run(){
			MovingObject obj, o;
			if(Thread.currentThread() == redThread){
				obj = redBall;
				o = blueBall;
			}else{
				obj = blueBall;
				o = redBall;
			}
			while(true){
				{
					try{
						Thread.sleep(50);
					}catch(InterruptedException e){}
				}
				if(obj.x + obj.dx < 0 || getWidth() < obj.x + obj.dx + obj.width
				|| (obj.x + obj.width >= o.x && obj.x < o.x + o.width
					&& obj.y + obj.width >= o.y && obj.y < o.y + o.width))
					obj.dx = -obj.dx;
				obj.x += obj.dx;
				if(obj.y + obj.dy < 0 || getHeight() < obj.y + obj.dy + obj.width
				|| (obj.x + obj.width >= o.x && obj.x < o.x + o.width
					&& obj.y + obj.width >= o.y && obj.y < o.y + o.width))
					obj.dy = -obj.dy;
				obj.y += obj.dy;
				repaint();
			}
		}
	
		public void paint(Graphics g){
			super.paint(g);
			redBall.show(g, Color.RED);
			blueBall.show(g, Color.BLUE);
		}
	}
	
	// MovingObject.java
	
	import javax.swing.*;
	import java.awt.*;
	
	public class MovingObject{
		public int x;
		public int y;
		public int dx;
		public int dy;
		public int width;
	
		MovingObject(int _x, int _y, int _dx, int _dy, int _width){
			x = _x;
			y = _y;
			dx = _dx;
			dy = _dy;
			width = _width;
		}
	
		void show(Graphics g, Color clr){
			g.setColor(clr);
			g.fillOval(x, y, width, width);
		}
	}

CONTENTS

最新の20件

2020-11-14 2005-12-06
  • HW972 Wig MovingObject
2006-11-04 2012-07-15 2009-06-19 2011-03-03 2006-12-13 2007-11-05 2014-07-22 2014-07-19 2014-07-09 2014-01-14 2012-09-03 2012-03-28

今日の10件

人気の30件

  • counter: 2749
  • today: 1
  • yesterday: 0
  • online: 1