braju.com | Java fprintf, printf, sprintf (fscanf, scanf, sscanf) |
HomeDownload Purchase History Examples Documentation API/javadoc FAQ Feedback About Donate Java scanf |
AppletsMouse sensitive applet
1 import java.applet.Applet; 2 import java.awt.*; 3 import com.braju.applet.*; 4 import com.braju.format.*; 5 6 public class MouseApplet extends Applet implements DoneListener { 7 private LoaderPanel loaderPnl; 8 private Panel mainPnl; 9 10 String theMessage; 11 Button theButton; 12 13 int messageX, messageY; 14 int colorIndex = -1; 15 16 static Color[] someColors = { 17 Color.black, Color.red, Color.green, Color.blue, Color.magenta }; 18 19 public void init() { 20 setBackground(Color.white); 21 // The loader that is shown initially... 22 loaderPnl = new LoaderPanel(); 23 loaderPnl.addDoneListener(this); 24 add(loaderPnl); 25 // The main panel that will be shown after loading... 26 mainPnl = new Panel(); 27 mainPnl.hide(); 28 mainPnl.setBackground(Color.white); 29 theButton = new Button("Change Color"); 30 mainPnl.add(theButton); 31 add(mainPnl); 32 } 33 34 public void done(DoneEvent e) { 35 loaderPnl.hide(); 36 mainPnl.show(); 37 messageX = size().width/2; 38 messageY = size().height/2; 39 colorIndex = 0; 40 validate(); 41 repaint(); 42 } 43 44 public void paint(Graphics graphics) { 45 if (colorIndex == -1) return; 46 47 // Set the text color. 48 graphics.setColor(currentColor()); 49 50 theMessage = Format.sprintf("[%03d,%03d] [%#02.2x,%#02x]", 51 new Parameters(messageX).add(messageY) 52 .add((short)messageX).add((short)messageY)); 53 54 graphics.drawString(theMessage, messageX,messageY); 55 } // paint 56 57 public boolean mouseDrag(Event e, int x, int y) { 58 messageX = x; 59 messageY = y; 60 61 // Repaing everything in the window. 62 repaint(); 63 64 return true; 65 } // mouseDrag 66 67 68 public boolean action(Event e, Object arg) { 69 // If the button was clicked, change color... 70 if (e.target == theButton) { 71 changeColor(); 72 return true; 73 } 74 75 return false; 76 } // mouseMoved 77 78 synchronized private void changeColor() { 79 if (++colorIndex == someColors.length) 80 colorIndex = 0; 81 82 theButton.setForeground(currentColor()); 83 repaint(); 84 } // changeColor 85 86 synchronized private Color currentColor() { 87 return someColors[colorIndex]; 88 } // currentColor 89 }Download: MouseApplet.java |
|||||||||
|