Using the IDE

Objectives

Description

For each lab and homework assignment throughout the semester, you will be asked to use the IDE (integrated development environment) to edit, compile, and run Java applications. For each assignment, you will be provided with a project folder containing the files used by the IDE. You only need to be concerned with the one file with a ".java" extension.

Depending on the particular IDE you are using, there could be some surprising quirks that you should be aware of. Your laboratory instructor will give you additional details about how to use the IDE.

Exercises

  1. Type the program given below in the "Colors.java" file. Try to follow the same coding conventions. Enter the program precisely as it appears below. Note that Java is a case sensitive language. That is, upper case and lower case letters are not considered to be the same. As you enter the program, pay attention to the spacing and indentation of the source code. The compiler is not sensitive to variations in format, but it is important to develop a good style for human readability.
  2. Notice how the program editor behaves somewhat differently than an ordinary text editor. As you enter your program, answer each of the following questions on a sheet of paper:

    1. Some of the text is highlighted in color. What do you suppose is the significance of each of the colors you see?
    2. How is the behavior of the "return" or "enter" key different than in an ordinary editor?
    3. What do you observe when you type a closing bracket or parenthesis, "), }, or ]"?
  3. When you have finished entering the program, compile and run it.
  4. If you made any typing mistakes, compiler will give you an error message with the offending line marked. Make any necessary corrections.
  5. Repeat steps 3 and 4 until your program compiles and runs sucessfully.
  6. Modify the two lines near the beginning of the file that start with "Color[ ]" to add the additional background colors green and white, and the additional text color black. Compile and run your program to be sure it behaves as expected.
  7. Modify the program so that it displays your name instead of the text "Goodbye World! Hello Java!!". Compile and test.

The source code to enter in step one is:



// My First Application
// Watch the colors change as you click the buttons

import java.awt.*;
import java.awt.event.*;

public class Colors extends Panel implements ActionListener
{
    Button backButton, textButton;
    Font f = new Font("Helvetica", Font.BOLD, 18);
    Color[] bgColors = { Color.cyan, Color.orange, Color.pink, Color.green };
    Color[] txtColors = { Color.blue, Color.magenta, Color.red, Color.black };
    int bgCode = 0, txtCode = 0;
    
    public Colors()
    {
        backButton = new Button("Background Color");
        backButton.addActionListener(this);
        add(backButton);
        
        textButton = new Button("Text Color");
        textButton.addActionListener(this);
        add(textButton);
        
        setBackground(bgColors[bgCode]);
    }
        


    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();      // Who generated the event?
        
        if (source == backButton) {         // Was it the Background Color button?
            bgCode = ++bgCode % bgColors.length;
            setBackground(bgColors[bgCode]);
        }
        
        else if (source == textButton) {    // Or was it the Text Color button?
            txtCode = ++txtCode % txtColors.length;
        }
        
        repaint();
    }
    
    
    public void paint(Graphics g)
    {
        g.setColor(txtColors[txtCode]);
        g.setFont(f);
        g.drawString("Goodbye world!  Hello Java!", 30, 120);
    }
    
            
    public static void main(String args[])
    {
        Frame f = new Frame("Colors");
        f.add(BorderLayout.CENTER, new Colors());
        f.setSize(325, 200);
        f.addWindowListener(new WindowCloser());
        f.show();
    }
}

class WindowCloser extends WindowAdapter
{
	public void windowClosing(WindowEvent e)
	{
		e.getWindow().dispose();
		System.exit(0);
	}
}