このページのコンテンツ一覧(5個位までにしてください。上が最新で下が最古です)
// 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); } }
// 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); } }
// 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); } }