Homework Answers - AWT and Applets

  1. //  this applet draws 15 shapes (circles and squares) in random locations
    //  choosing one of 6 colors randomly, and it writes the author's name
    //  in the lower right corner
    
    import java.awt.Graphics;
    import java.awt.Color;
    import java.util.Random;
    import java.awt.FontMetrics;
    
    public class RandDraw extends java.applet.Applet {
        private Random rand;
        private Color[] colors;
        private int width;
        private int height;
    
    //  initialize the array of colors and the random number generator
    //  get the size of the applet
    
        public void init() {
            rand = new Random();
            colors = new Color[6];
            colors[0] = Color.black;
            colors[1] = Color.blue;
            colors[2] = Color.red;
            colors[3] = Color.yellow;
            colors[4] = Color.green;
            colors[5] = Color.magenta;
            width = getSize().width;
            height = getSize().height;
        }
    
        public void paint(Graphics g) {
            int colnum;
            int x,y;
    
    //  each iteration chooses a random number for the color, and a random
    //  position for the shape (on even iterations draw a square, on odd 
    //  iterations draw a circle
    
            for (int i = 0 ; i < 15 ; i++) {
                colnum = (int)(rand.nextDouble() * 6);
                if (colnum == 6)
                    colnum = 5;
                g.setColor(colors[colnum]);
                x = (int) (rand.nextDouble() * (width-15));
                y = (int) (rand.nextDouble() * (height-15));
                if (i % 2 == 0)
                    g.fillRect(x,y,15,15);
                else
                    g.fillOval(x,y,15,15);
            }
    
    //  draw the name in black in the lower right corner
    
            g.setColor(Color.black);
            FontMetrics fm = g.getFontMetrics();
            int messWidth = fm.stringWidth("your name here");
            g.drawString("your name here",width-messWidth-10,height-10);
        }
    }
    
  2. //  this applet sets up two TextAreas where the user can type text.
    //  when the user types a character in either TextArea the character
    //  is copied into the other TextArea
    
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
    
    public class NumGuess11 extends java.applet.Applet
                                    implements ActionListener
    {
        private TextField guess;
        private TextField message;
        private Panel guessPanel;
        private Panel msgPanel;
        private Panel buttonPanel;
        private Random rand;
        private int theNum;
        private Button newNum;
        private int width;
        private int height;
    
    //  create two Panels and the two text areas, add the text areas to the 
    //  Panels and add the Panels to the applet the TextAreas are placed 
    //  inside Panels so that their size can be specified
    
        public void init()
        {
            width = getSize().width;
            height = getSize().height;
            rand = new Random();
            theNum = (int) (rand.nextDouble() * 100);
            setLayout( new GridLayout(3,1) );
            guessPanel = new Panel();
            msgPanel = new Panel();
            buttonPanel = new Panel();
            guessPanel.add( guess = new TextField(4) );
            msgPanel.add( message = new TextField(45) );
            buttonPanel.add( newNum = new Button("New Number") );
            add(guessPanel);
            add(msgPanel);
            add(buttonPanel);
            message.setText("Type a number between 0 and 100, then press Enter");
            guess.addActionListener(this);
            newNum.addActionListener(this);
        }
    
    //  this method will receive an event when the user presses the enter key.
    
        public void actionPerformed(ActionEvent e)
        {
            if (e.getActionCommand().equals("New Number"))
            {
                theNum = (int) (rand.nextDouble() * 100);
                guess.setText("");
                message.setText("Type a number between 0 and 100, then press Enter");
            }
            else
            {
                String numstr = guess.getText();
                try {
                    int num = Integer.parseInt(numstr);
                    guess.selectAll();
                    if (num == theNum)
                        message.setText("You guessed it!");
                    else if (num < theNum)
                        message.setText("Try a bigger number...");
                    else
                        message.setText("Try a smaller number...");
                    }
                catch(NumberFormatException ex) {
                    message.setText("Enter a number between 0 and 100");
                    guess.selectAll();
                }
            }
        }
    }
    

Email Me | Office Hours | My Home Page | Department Home | MCC Home Page