import java.awt.*; import java.util.Date; import java.applet.Applet; public class MobiusApplet extends Applet implements Runnable { gCanvas tafel; iPanel instructionPanel; Thread runner; Date currentTime; public void init() { setBackground(Color.white); GridBagLayout gridbag = new GridBagLayout(); setLayout(gridbag); GridBagConstraints c = new GridBagConstraints(); currentTime = new Date(); tafel = new gCanvas(); c.fill = GridBagConstraints.BOTH; c.gridwidth = GridBagConstraints.REMAINDER; c.weightx = 1.0; c.weighty = 20.0; c.anchor = GridBagConstraints.WEST; gridbag.setConstraints(tafel,c); add(tafel); instructionPanel = new iPanel(); c.fill = GridBagConstraints.BOTH; c.gridwidth = GridBagConstraints.REMAINDER; c.weightx = 1.0; c.weighty = 0.0; gridbag.setConstraints(instructionPanel,c); add(instructionPanel); } public void start() { if (runner == null) { runner = new Thread(this); runner.start(); } } public void stop() { if (runner != null) { runner.stop(); runner = null; } } public void run() { while (true) { currentTime = new Date(); tafel.updateLocation(); repaint(); try {Thread.sleep(1000);} catch (InterruptedException e) {} } } } class iPanel extends Panel{ iPanel() { setLayout(new BorderLayout()); Label l = new Label("2-Dimensionsal Crab Moving in a M\u00f6bius Strip.", Label.CENTER); add("Center", l); } } class gCanvas extends Canvas{ Color rectBackground,topColor,bottomColor; double locationx = 0.45,locationy = 0.45; double vx,vy; double crabwidth = 0.2, crabheight = 0.14; Point Origin; int xscale, yscale; gCanvas() { setBackground(new Color(255,255,192)); rectBackground = new Color(192,255,255); topColor = Color.red; bottomColor = Color.green; vx = 2*Math.PI/100; vy = 4.0/100; Origin = new Point(30,20); xscale = 330; yscale = 250; repaint(); } private void ConvertCoordinates(double x, double y, Point p) { p.x = (int)(Origin.x + xscale*x); p.y = (int)(Origin.y + yscale*y); } public void updateLocation() { Color tempColor; if (locationy+vy<=0) { locationx = locationx-(locationy/vy)*vx; locationy = 0; vy = -vy; } else if (locationy+vy >=1-crabheight) { locationx = locationx +((1-crabheight-locationy)/vy)*vx; locationy = 1-crabheight; vy = -vy; } else { locationx = locationx+vx; locationy = locationy + vy; } if (locationx >1.0) { locationx = locationx - 1.0; locationy = 1.0 - locationy - crabheight; vy = -vy; tempColor = topColor; topColor = bottomColor; bottomColor = tempColor; } repaint(); } public void paint(Graphics g) { Point Coords = new Point(0,0); g.clipRect(Origin.x,Origin.y,xscale,yscale); g.setColor(rectBackground); g.fillRect(Origin.x,Origin.y,xscale,yscale); ConvertCoordinates(locationx, locationy, Coords); g.setColor(topColor); g.fillArc(Coords.x,Coords.y,(int)(xscale*crabwidth),(int)(yscale*crabheight),30,180); g.setColor(bottomColor); g.fillArc(Coords.x,Coords.y,(int)(xscale*crabwidth),(int)(yscale*crabheight),180,150); if (locationx>1-crabwidth) { ConvertCoordinates(locationx-1,1-locationy-crabheight,Coords); g.setColor(bottomColor); g.fillArc(Coords.x,Coords.y,(int)(xscale*crabwidth),(int)(yscale*crabheight),30,180); g.setColor(topColor); g.fillArc(Coords.x,Coords.y,(int)(xscale*crabwidth),(int)(yscale*crabheight),180,150); } } }