/* * @(#)MovingHR.java * * Copyright (c) 1997 Masami Nakagawa * * Permission to use, copy, modify, and distribute this software * for NON-COMMERCIAL purpose and without fee is hereby granted. */ import java.awt.*; import java.applet.*; import java.util.*; /** * @author Masami Nakagawa * @version 1.1, 21 Dec 1997 */ public class MovingHR extends Applet implements Runnable { final int SWING = 0; final int WAVE = 1; final int RUN = 2; final int UP = 3; final int DOWN = 4; final int FULL = 0; final int UPPER = 1; final int LOWER = 2; int number; int hrY; int hrHeight; String imageDir; boolean raised; Image[] image; int[] type; int[] time; int[] area; int[] speed; MoveImage[] mover; Color bgColor; Image bgImage; Dimension mySize; Rectangle hr; Image backImage; Image offImage; Thread thread; MediaTracker mt; public void init() { String s; int n; mySize = size(); backImage = createImage(mySize.width, mySize.height); offImage = createImage(mySize.width, mySize.height); mt = new MediaTracker(this); number = getIntParam("number", 0); hrHeight = getIntParam("hrHeight", 3) - 1; hrY = getIntParam("hrY", (mySize.height-hrHeight)/2); raised = getBoolParam("raised", false); imageDir = getParameter("imageDir"); if (imageDir == null) imageDir = "."; hr = new Rectangle(0, hrY, mySize.width-1, hrHeight); image = new Image[number]; type = new int[number]; time = new int[number]; speed = new int[number]; area = new int[number]; for (int i = 0; i < number; i++) { // default setting type[i] = SWING; time[i] = 100; speed[i] = 4; area[i] = FULL; String str = getParameter("Image"+(i+1)); if (str != null) { StringTokenizer st = new StringTokenizer(str, "|"); image[i] = getImage(getDocumentBase(), imageDir+"/"+st.nextToken().trim()); mt.addImage(image[i], 0); if (st.hasMoreTokens()) { s = st.nextToken().trim().toLowerCase(); if (s.equals("swing")) { type[i] = SWING; } else if (s.equals("wave")) { type[i] = WAVE; } else if (s.equals("run")) { type[i] = RUN; } else if (s.equals("up")) { type[i] = UP; } else if (s.equals("down")) { type[i] = DOWN; } if (st.hasMoreTokens()) { n = Integer.parseInt(st.nextToken().trim()); if (n > 0) { time[i] = n; } if (st.hasMoreTokens()) { s = st.nextToken().trim().toLowerCase(); if (s.equals("upper")) { area[i] = UPPER; } else if (s.equals("lower")) { area[i] = LOWER; } else { area[i] = FULL; } if (st.hasMoreTokens()) { n = Integer.parseInt(st.nextToken().trim()); if (n != 0) { speed[i] = n; } } } } } } } s = getParameter("bgColor"); if (s != null) { bgColor = new Color(Integer.parseInt(s, 16)); } else { bgColor = getBackground(); } s = getParameter("bgImage"); if (s != null) { bgImage = getImage(getDocumentBase(), imageDir+"/"+s); mt.addImage(bgImage, 1); } drawBackground(); } private int getIntParam(String name, int def) { String s = getParameter(name); if (s != null) { return Integer.parseInt(s); } return def; } private boolean getBoolParam(String name, boolean def) { String s = getParameter(name); if (s != null) { return Boolean.valueOf(s).booleanValue(); } return def; } private Graphics createGraphics(int type) { Graphics g = null; switch (type) { case FULL: g = offImage.getGraphics(); g.clipRect(hr.x, 0, hr.width, mySize.height); break; case UPPER: g = offImage.getGraphics(); g.clipRect(hr.x, 0, hr.width, hr.y+(raised?0:hr.height)); break; case LOWER: g = offImage.getGraphics(); int y = hr.y+(raised?hr.height:0)+1; g.clipRect(hr.x, y, hr.width, mySize.height-y); break; } return g; } public void start() { if (thread == null) { thread = new Thread(this); thread.start(); } } public void stop() { if (thread != null) { thread.stop(); thread = null; } } void drawBackground() { Graphics g = backImage.getGraphics(); g.setColor(bgColor); g.fillRect(0, 0, mySize.width, mySize.height); if (bgImage != null) { int w = bgImage.getWidth(null); int h = bgImage.getHeight(null); if (w > 0 && h > 0) { for (int x = 0; x < mySize.width; x += w) { for (int y = 0; y < mySize.height; y += h) { g.drawImage(bgImage, x, y, null); } } } } g.draw3DRect(hr.x, hr.y, hr.width, hr.height, raised); g.dispose(); } void createMover() { mover = new MoveImage[number]; for (int i = 0; i < number; i++) { if (image[i] == null) continue; switch (type[i]) { case SWING: mover[i] = new SwingImage(image[i], time[i], mySize, 0, hr.y+hr.height/2); break; case WAVE: mover[i] = new WaveImage(image[i], time[i], mySize, speed[i], 0); break; case UP: mover[i] = new UpImage(image[i], time[i], mySize, 0, 0); break; case DOWN: mover[i] = new DownImage(image[i], time[i], mySize, 0, 0); break; case RUN: mover[i] = new RunImage(image[i], 0, mySize, speed[i], hr.y+hr.height/2); break; } mover[i].setGraphics(createGraphics(area[i])); } } public synchronized void paint(Graphics g) { g.drawImage(offImage, 0, 0, null); } public void update(Graphics g) { paint(g); } public void run() { boolean moverCreated = false; Graphics offGraph = offImage.getGraphics(); Graphics graph = getGraphics(); while (true) { if (!mt.checkAll(true)) { offGraph.drawImage(backImage, 0, 0, null); } else { if (!moverCreated) { createMover(); drawBackground(); moverCreated = true; } synchronized(this) { offGraph.drawImage(backImage, 0, 0, null); for (int i = 0; i < mover.length; i++) { if (mover[i] == null) continue; mover[i].draw(); } } } paint(graph); try { Thread.currentThread().sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } } } abstract class MoveImage { protected Image image; protected int roundTime; protected Dimension area; protected int speed; protected int hrY; protected Dimension mySize; protected Graphics screen; public MoveImage(Image image, int roundTime, Dimension area, int speed, int hrY) { this.image = image; this.roundTime = roundTime; this.area = area; this.speed = speed; this.hrY = hrY; mySize = new Dimension(image.getWidth(null), image.getHeight(null)); init(); } abstract protected void init(); abstract protected void draw(Graphics g); public void draw() { draw(screen); } public void setGraphics(Graphics g) { this.screen = g; } } class UpImage extends MoveImage { private int x; private int y; private int delay; private double deg; private int count; public UpImage(Image image, int roundTime, Dimension area, int speed, int hrY) { super(image, roundTime, area, speed, hrY); } protected void init() { x = (int)((area.width-mySize.width)*Math.random()); deg = 0; y = (int)(area.height * (Math.cos(deg)+1)/2); delay = (int)(Math.random()*30); count = 0; } protected void draw(Graphics g) { if (delay-- > 0) return; if (++count >= roundTime) init(); deg += 2*Math.PI/roundTime; y = (int)(area.height*(Math.cos(deg)+1)/2); g.drawImage(image, x, y, null); } } class DownImage extends MoveImage { private int x; private int y; private int delay; private double deg; private int count; public DownImage(Image image, int roundTime, Dimension area, int speed, int hrY) { super(image, roundTime, area, speed, hrY); } protected void init() { x = (int)((area.width-mySize.width)*Math.random()); deg = Math.PI; y = (int)(area.height * (Math.cos(deg)+1)/2)-mySize.height; delay = (int)(Math.random()*30); count = 0; } protected void draw(Graphics g) { if (delay-- > 0) return; if (++count >= roundTime) init(); deg += 2*Math.PI/roundTime; y = (int)(area.height*(Math.cos(deg)+1)/2)-mySize.height; g.drawImage(image, x, y, null); } } class WaveImage extends MoveImage { private int x; private int y; private double deg; private int delay; public WaveImage(Image image, int roundTime, Dimension area, int speed, int hrY) { super(image, roundTime, area, speed, hrY); } protected void init() { x = (speed > 0)?-mySize.width:area.width; deg = 2*Math.PI*Math.random(); y = (int)((area.height-mySize.height)*(Math.cos(deg)+1)/2); delay = (int)(60*Math.random()); } protected void draw(Graphics g) { if (delay-- > 0) return; x += speed; if (x < -mySize.width || x > area.width) { init(); } else { deg += 2*Math.PI/roundTime; y = (int)((area.height-mySize.height)*(Math.cos(deg)+1)/2); } g.drawImage(image, x, y, null); } } class SwingImage extends MoveImage { private int x; private int y; private double deg; public SwingImage(Image image, int roundTime, Dimension area, int speed, int hrY) { super(image, roundTime, area, speed, hrY); } protected void init() { deg = Math.random()*2*Math.PI; x = (int)((Math.cos(deg)+1)*(area.width-mySize.width)/2); y = hrY - mySize.height/2; } protected void draw(Graphics g) { deg += 2*Math.PI/roundTime; x = (int)((Math.cos(deg)+1)*(area.width-mySize.width)/2); g.drawImage(image, x, y, null); } } class RunImage extends MoveImage { private int x; private int y; private int delay; public RunImage(Image image, int roundTime, Dimension area, int speed, int hrY) { super(image, roundTime, area, speed, hrY); } protected void init() { x = (speed > 0)?-mySize.width:area.width; y = hrY - mySize.height/2; delay = (int)(Math.random()*60); } protected void draw(Graphics g) { if (delay-- > 0) return; x += speed; g.drawImage(image, x, y, null); if (x > area.width || x < -mySize.width) { init(); } } }