/* The Genuts Project - http://www.genuts.com */ import java.applet.*; import java.awt.*; import java.awt.event.*; import com.genuts.gameui.*; /** * This applet, named "Remote Control" and figuring a joystick, allows moving the "RC Toy" applet's cursor. */ public class RemoteControl extends Applet implements MouseListener, MouseMotionListener { PlayField f; int xb, yb; int d; int w, h; int p; int xc, yc; int L; int s; int H; int xc2, yc2; public void init() { // width & height w = getSize().width; h = getSize().height; // depth p = (int)(Math.min(w,h)*0.05); // diameter of the ball d = (int)(Math.min(w,h)*0.35); // stick thickness s = d/3; // center xc = (w-p)/2; yc = (h-p)/2; // ball's height H = p*2; // center xc2 = xc-H; yc2 = yc-H; // liberty length (radius) L = Math.min(w,h)/2-d/2-H-p; // initial ball placement xb = xc-H; yb = yc-H; setLayout(new BorderLayout()); f = new PlayField(w,h) { public void paintPlayField(Graphics g) { // bg g.setColor(Color.white); g.fillRect(0,0,w,h); // box g.setColor(new Color(204,204,204)); g.fillRect(0+w/4,0+h/4,w-p-w/2,h-p-h/2); g.setColor(new Color(102,102,102)); int tx1[] = {w-p-w/4,w-w/4,w-w/4,w-p-w/4}; int ty1[] = {0+h/4,p+h/4,h-h/4,h-p-h/4}; g.fillPolygon(tx1, ty1, 4); g.setColor(new Color(153,153,153)); int tx2[] = {0+w/4,w-p-w/4,w-w/4,p+w/4}; int ty2[] = {h-p-h/4,h-p-h/4,h-h/4,h-h/4}; g.fillPolygon(tx2, ty2, 4); // stick g.setColor(new Color(102,102,102)); g.fillOval(xc-s/2,yc-s/2,s,s); drawThickLine(g, xc, yc, xb, yb, s); // ball g.setColor(Color.red); g.fillOval(xb-d/2,yb-d/2,d,d); g.setColor(Color.white); g.fillOval(xb-d/4,yb-d/4,d/6,d/6); } }; f.setSleepTime(1000/25); f.setDisplayManager(null); f.addMouseListener(this); f.addMouseMotionListener(this); add(f,BorderLayout.CENTER); } public void start() { f.setPause(false); } public void stop() { f.stop(); } public void mouseClicked(MouseEvent e) { // Nothing } public void mouseEntered(MouseEvent e) { showStatus(getAppletInfo()); } public void mouseExited(MouseEvent e) { showStatus(""); } public String getAppletInfo() { return "Remote Control"; } public void mousePressed(MouseEvent e) { moveBall(e.getX(),e.getY()); } void moveBall(int nx, int ny) { xb = nx; yb = ny; // limit xb & yb to a LxL square if (xb > xc2+L) xb = xc2+L; else if (xb < xc2-L) xb = xc2-L; if (yb > yc2+L) yb = yc2+L; else if (yb < yc2-L) yb = yc2-L; f.repaint(); sendXY(); } void sendXY() { int x,y; double l = Math.sqrt(Math.pow(xb-xc2,2)+Math.pow(yb-yc2,2)); x = (xb-xc2)*100/L; y = (yb-yc2)*100/L; // The "Remote Control" applet try to find the "RC Toy" // applet by looking it up by name, specified with a // tag, using the AppletContext getApplet // method. Applet rc = null; String name = getParameter("RC"); rc = getAppletContext().getApplet(name); if (rc != null) { if (!(rc instanceof RemotelyControllable)) { //System.out.println("Found applet named " + rc + ", " + "but it's not a RemotelyControllable."); } else { //System.out.println("Found applet named " + rc + ". Sending message to it."); // The "Remote Control" sends a message by invoking // the "RC Toy" applet's method. ((RemotelyControllable)rc).setXY(x,y); } } } public void mouseReleased(MouseEvent e) { moveBall(xc2,yc2); } public void mouseDragged(MouseEvent e) { moveBall(e.getX(),e.getY()); } public void mouseMoved(MouseEvent e) { // Nothing } /** * From Real's HowTo, http://www.rgagnon.com/ */ public void drawThickLine(Graphics g, int x1, int y1, int x2, int y2, int thickness) { // The thick line is in fact a filled polygon int dX = x2 - x1; int dY = y2 - y1; // line length double lineLength = Math.sqrt(dX * dX + dY * dY); double scale = (double)(thickness) / (2 * lineLength); // The x and y increments from an endpoint needed to create a rectangle... double ddx = -scale * (double)dY; double ddy = scale * (double)dX; ddx += (ddx > 0) ? 0.5 : -0.5; ddy += (ddy > 0) ? 0.5 : -0.5; int dx = (int)ddx; int dy = (int)ddy; // Now we can compute the corner points... int xPoints[] = new int[4]; int yPoints[] = new int[4]; xPoints[0] = x1 + dx; yPoints[0] = y1 + dy; xPoints[1] = x1 - dx; yPoints[1] = y1 - dy; xPoints[2] = x2 - dx; yPoints[2] = y2 - dy; xPoints[3] = x2 + dx; yPoints[3] = y2 + dy; g.fillPolygon(xPoints, yPoints, 4); } }