/* Garrett M. Groff (groffg@gmgDesign.com) Tutorial software to learn Java This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY, implied or otherwise. See the GNU General Public License for more details. */ package triangle; import java.awt.*; import java.awt.event.*; import java.applet.*; public class mytriangle extends Applet { // member declarations java.awt.Button btnAddSpeed = new java.awt.Button(); java.awt.Button btnSubSpeed = new java.awt.Button(); java.awt.List listShapes = new java.awt.List(); java.awt.Button btnAbout = new java.awt.Button(); boolean bAbout=false; boolean isStandalone = false; private int x,y; // location of object on the screen private int nSpeed; // speed of the object private int nShape; // the current shape // 0=circle // 1=square public mytriangle() { // default values nShape=0; // circle nSpeed=5; // slow // coordinates for the position on the screen x=10; y=10; listShapes.add("Circle"); listShapes.add("Square"); } public static void main(String[] args) { // let this be used as an app Frame frame = new Frame(); mytriangle applet = new mytriangle(); frame.add(applet); frame.setSize(400, 400); frame.show(); applet.init(); } // Retrieve the value of an applet parameter public String getParameter(String key, String def) { return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); } // Get info on the applet parameters public String[][] getParameterInfo() { return null; } // Get applet information public String getAppletInfo() { return "Programmer: Garrett M. Groff; website: http://www.gmgDesign.com"; } // Initialize the applet public void init() { requestFocus(); // applet gets focus // necessary for KeyListener events try { initComponents(); } catch (Exception e) { e.printStackTrace(); } repaint(); } public void paint(Graphics g) { if (bAbout) g.drawString("Programmer: Garrett M. Groff; website: www.gmgDesign.com",0,15); switch(nShape) { case 0: g.drawOval(x,y,10,10); break; case 1: g.drawRect(x,y,10,10); break; } requestFocus(); } public void initComponents() throws Exception { // the following code sets the frame's initial state btnAddSpeed.setLocation(new java.awt.Point(520, 0)); btnAddSpeed.setLabel("Speed+"); btnAddSpeed.setVisible(true); btnAddSpeed.setSize(new java.awt.Dimension(64, 20)); btnSubSpeed.setLocation(new java.awt.Point(520, 20)); btnSubSpeed.setLabel("Speed-"); btnSubSpeed.setVisible(true); btnSubSpeed.setSize(new java.awt.Dimension(64, 20)); listShapes.setLocation(new java.awt.Point(520, 40)); listShapes.setVisible(true); listShapes.setSize(new java.awt.Dimension(64, 40)); btnAbout.setLocation(new java.awt.Point(540, 420)); btnAbout.setLabel("About"); btnAbout.setVisible(true); btnAbout.setSize(new java.awt.Dimension(54, 23)); setLocation(new java.awt.Point(0, 0)); setLayout(null); setSize(new java.awt.Dimension(606, 473)); add(btnAddSpeed); add(btnSubSpeed); add(listShapes); add(btnAbout); btnAddSpeed.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { btnAddSpeedActionPerformed(e); } }); btnSubSpeed.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { btnSubSpeedActionPerformed(e); } }); listShapes.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent e) { listShapesItemStateChanged(e); } }); btnAbout.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { btnAboutActionPerformed(e); } }); addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent e) { thisMouseClicked(e); } public void mouseEntered(java.awt.event.MouseEvent e) { thisMouseEntered(e); } public void mouseExited(java.awt.event.MouseEvent e) { thisMouseExited(e); } }); addKeyListener(new java.awt.event.KeyAdapter() { public void keyPressed(java.awt.event.KeyEvent e) { thisKeyPressed(e); } }); addComponentListener(new java.awt.event.ComponentAdapter() { public void componentResized(java.awt.event.ComponentEvent e) { thisComponentResized(e); } }); } public void thisMouseEntered(java.awt.event.MouseEvent e) { showStatus("Mouse entered"); } public void thisMouseExited(java.awt.event.MouseEvent e) { showStatus("Mouse has left the building"); } public void btnAddSpeedActionPerformed(java.awt.event.ActionEvent e) { if (nSpeed<10) nSpeed++; else showStatus("Speed limit already hit!"); requestFocus(); } public void btnSubSpeedActionPerformed(java.awt.event.ActionEvent e) { if (nSpeed>1) nSpeed--; else showStatus("You're going too slow."); requestFocus(); } public void listShapesItemStateChanged(java.awt.event.ItemEvent e) { nShape=listShapes.getSelectedIndex(); repaint(); requestFocus(); } public void thisComponentResized(java.awt.event.ComponentEvent e) { Dimension dim=getSize(); btnAddSpeed.setLocation(new java.awt.Point(dim.width-70, 0 )); btnSubSpeed.setLocation(new java.awt.Point(dim.width-70, 20 )); listShapes.setLocation(new java.awt.Point(dim.width-70, 40 )); btnAbout.setLocation(new java.awt.Point(dim.width-60, dim.height-30 )); } public void thisKeyPressed(KeyEvent e) { int ch=e.getKeyCode(); Dimension dim; dim=getSize(); if (x<1 || x>dim.width-20) return; if (y<1 || y>dim.height-50) return; switch(ch) { case KeyEvent.VK_UP: y=y-nSpeed; break; case KeyEvent.VK_DOWN: y=y+nSpeed; break; case KeyEvent.VK_LEFT: x=x-nSpeed; break; case KeyEvent.VK_RIGHT: x=x+nSpeed; break; } repaint(); // refresh the applet } public void thisMouseClicked(java.awt.event.MouseEvent e) { x=e.getX(); y=e.getY(); repaint(); } public void btnAboutActionPerformed(java.awt.event.ActionEvent e) { if (bAbout) bAbout=false; else bAbout=true; repaint(); } } // end of applet