home *** CD-ROM | disk | FTP | other *** search
Java Source | 2001-03-11 | 4.8 KB | 196 lines |
- package intuitive.quicktour;
-
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import java.awt.geom.*;
- import java.util.*;
- import java.net.URL;
-
- public class QuickTourApp extends JPanel implements ActionListener {
- Color colors[] = { Color.black, Color.red, Color.green, Color.blue, Color.yellow };
- ImageIcon optitLogo;
-
- public QuickTourApp() {
- super();
- try {
- UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
- } catch (Exception exc) {
- System.err.println("Error couldn't read L&F: " + exc);
- }
-
- /*
- * Loading resources does not work the same way on windows and solaris
- */
- URL url = null;
- url = getClass().getClassLoader().getResource("OptimizeIt.gif");
- if(url == null) {
- url = getClass().getResource("OptimizeIt.gif");
- }
- optitLogo = new ImageIcon(url);
-
- setLayout(null); // Absolute positioning
- addShape();
- addShape();
- }
-
- public void actionPerformed(ActionEvent e) {
- String action = e.getActionCommand();
- if(action.equals("more"))
- addShape();
- else if(action.equals("less"))
- removeLastShape();
- }
-
- public void addShape() {
- int index = getComponentCount() % colors.length;
- QTShape s;
- s = new QTShape(colors[index % colors.length], index * 50, index * 50);
- add(s);
- }
-
- public void removeLastShape() {
- if(getComponentCount() > 0) {
- QTShape s = (QTShape) getComponent(getComponentCount() - 1);
- s.stop();
- repaint(s.getBounds());
- remove(s);
- s.stop();
- }
- }
-
- public void paintComponent(Graphics g) {
- int w = optitLogo.getIconWidth();
- int h = optitLogo.getIconHeight();
-
- g.setColor(Color.lightGray);
- g.fillRect(0,0,getWidth(), getHeight());
- optitLogo.paintIcon(this, g, (getWidth() - w) / 2, (getHeight() - h) / 2);
- }
-
- public static void main(String[] args) {
- try {
- String vers = System.getProperty("java.version");
- if (vers.compareTo("1.1.2") < 0) {
- System.out.println("!!!WARNING: Swing must be run with a " +
- "1.1.2 or higher version VM!!!");
- }
- QuickTourApp myQuickTourApp = new QuickTourApp();
-
- JFrame frame = new JFrame("OptimizeIt quick tour");
- JMenuBar menuBar = new JMenuBar();
- JMenu onlyMenu=new JMenu("Control");
- menuBar.add(onlyMenu);
- frame.setJMenuBar(menuBar);
-
- ButtonGroup group = new ButtonGroup();
- JMenuItem rbMenuItem = new JMenuItem("Add object");
- rbMenuItem.setActionCommand("more");
- rbMenuItem.setMnemonic('a');
- group.add(rbMenuItem);
- onlyMenu.add(rbMenuItem);
- rbMenuItem.addActionListener(myQuickTourApp);
-
- rbMenuItem = new JMenuItem("Remove object");
- rbMenuItem.setMnemonic('r');
- rbMenuItem.setActionCommand("less");
- rbMenuItem.addActionListener(myQuickTourApp);
- group.add(rbMenuItem);
- onlyMenu.add(rbMenuItem);
-
- frame.getContentPane().setLayout(new BorderLayout());
- frame.getContentPane().add("Center", myQuickTourApp);
- frame.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }});
- frame.setSize(300, 300);
- frame.show();
- }
- catch (Throwable t) {
- System.out.println("uncaught exception: " + t);
- t.printStackTrace();
- }
- }
- }
-
- class QTShape extends JComponent implements ActionListener {
- Color baseColor;
- javax.swing.Timer timer;
- double xWay,yWay;
-
- public QTShape(Color color,int x,int y) {
- setBounds(x,y,15,15);
- this.baseColor = color;
- xWay=1; yWay=1;
- timer = new javax.swing.Timer(1,this);
- timer.addActionListener(this);
- timer.setRepeats(true);
- timer.start();
- }
-
- protected void stop() {
- timer.stop();
- }
-
- public boolean isOpaque() {
- return true;
- }
-
- public void paintComponent(Graphics g) {
- g.setColor(baseColor);
- g.fill3DRect(0,0,getWidth(),getHeight(), true);
- }
-
- // The only action performed is the tick of the timer
- public void actionPerformed(ActionEvent event) {
- Component parent = getParent();
- int maxWidth = parent.getWidth() - getWidth();
- int maxHeight = parent.getHeight() - getHeight();
- int x=getX(),y=getY();
-
- if (maxWidth > 0 && maxHeight > 0) {
- if (x >= maxWidth) {
- x=maxWidth;
- xWay = -1 * (5 * Math.random());
- } else if (x <= 0) {
- x=0;
- xWay = 1 * (5 * Math.random());
- }
-
- if (y >= maxHeight) {
- y=maxHeight;
- yWay = -1 * (5 * Math.random());
- } else if (y <= 0) {
- y=0;
- yWay = 1 * (5 * Math.random());
- }
-
- x = (int) Math.rint((double)x + xWay);
- y = (int) Math.rint((double)y + yWay);
-
- setLocation(x,y);
- }
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-