import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.text.*; public class SwimingPool extends Panel { private java.util.List sprites; private int index; private Image foregroundImage; private Image backgroundImage; private VolatileImage doubleBuffer; private int width; private int height; private int mode = READY; private final static int READY = 0; private final static int GAMING = 1; private final static int END = 2; private int location; private long startTime; private long endTime; private long fastestTime; private SwimDash parent; private DecimalFormat formatter = new DecimalFormat("0.000"); public SwimingPool(SwimDash parent, java.util.List sprites, Image foregroundImage, Image backgroundImage){ this.parent = parent; this.sprites = sprites; this.foregroundImage = foregroundImage; this.backgroundImage = backgroundImage; addMouseWheelListener(new MouseWheelListener(){ public void mouseWheelMoved(MouseWheelEvent e){ if(e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL){ if(mode == GAMING){ setLocationX(getLocationX() + e.getUnitsToScroll()); } } } }); addMouseListener(new MouseAdapter(){ public void mouseReleased(MouseEvent e){ if(mode != GAMING){ gameStart(); } } }); } private synchronized void setLocationX(int x){ location = x; if(location >= width - ((Image)sprites.get(0)).getWidth(this)){ location = width - ((Image)sprites.get(0)).getWidth(this); gameStop(); } } public synchronized int getLocationX(){ return location; } private synchronized void setStartTime(long time){ startTime = time; } private synchronized long getStartTime(){ return startTime; } private synchronized void setEndTime(long time){ endTime = time; } private synchronized long getEndTime(){ return endTime; } private String getCurrentTime(){ double currentTime = (double)(System.currentTimeMillis() - getStartTime())/1000.0; return formatter.format(currentTime); } private String getTime(){ double time = (double)(getEndTime() - getStartTime())/1000.0; return formatter.format(time); } private String getFastest(){ double time = fastestTime/1000.0; return formatter.format(time); } private String getResult(){ StringBuffer result = new StringBuffer("タイム : " + getTime() + "s"); if(getEndTime() - getStartTime() < fastestTime || fastestTime == 0){ fastestTime = getEndTime() - getStartTime(); result.append(" FASTEST!!!"); } return result.toString(); } private void initVolatileImage(){ if (doubleBuffer == null || width != getWidth() || height != getHeight()) { width = getWidth(); height = getHeight(); doubleBuffer = createVolatileImage(width, height); } } private void validateVolatileImage() { GraphicsConfiguration gc = this.getGraphicsConfiguration(); if (doubleBuffer.validate(gc) == VolatileImage.IMAGE_INCOMPATIBLE) { doubleBuffer = createVolatileImage(width, height); } } private void gameStart(){ mode = GAMING; location = 0; setStartTime(System.currentTimeMillis()); parent.gameStart(); } private void gameStop(){ mode = END; setEndTime(System.currentTimeMillis()); parent.gameStop(); repaint(); } public synchronized void increaseIndex(){ index++; if(index >= sprites.size()){ index = 0; } } public void update(Graphics g){ paint(g); } public void paint(Graphics g){ initVolatileImage(); try{ do { validateVolatileImage(); Graphics gBuffer = doubleBuffer.getGraphics(); gBuffer.setColor(Color.yellow); gBuffer.fillRect(0, 0, width, height); gBuffer.drawImage(backgroundImage, 0, 0, this); gBuffer.drawImage((Image)sprites.get(index), getLocationX(), getHeight() / 2 - 22, this); gBuffer.drawImage(foregroundImage, 0, 0, this); if(mode == GAMING){ gBuffer.setColor(Color.black); gBuffer.drawString("経過時間 : " + getCurrentTime() + "s" + " Fastest Time : " + getFastest() + "s", 250, 15); gBuffer.setColor(Color.white); gBuffer.drawString("ホイールを回転させると動きます", 300, getHeight() - 1); }else{ gBuffer.setColor(Color.white); gBuffer.drawString("ウィンドウの内部をクリックするとスタートします", 250, getHeight() - 2); if(mode == END){ gBuffer.setColor(Color.black); gBuffer.drawString("経過時間 : " + getCurrentTime() + "s" + " Fastest Time : " + getFastest() + "s", 250, 15); gBuffer.setColor(Color.white); String result = getResult(); gBuffer.drawString(result, 301, getHeight() / 2 + 11); gBuffer.setColor(Color.black); gBuffer.drawString(result, 300, getHeight() / 2 + 10); } } gBuffer.dispose(); g.drawImage(doubleBuffer, 0, 0, this); } while (doubleBuffer.contentsLost()); }catch(NullPointerException ex){} } }