Using the IDE

Objectives

Description

For many of your lab and homework assignments throughout the semester, you will be asked to use the IDE (integrated development environment) to edit, compile, and run Java applications. 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 or underlined 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 an opening bracket or parenthesis, "(, {, or ["?
    4. 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.*;
import javax.swing.*;

public class Colors extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;
	private JButton backgroundButton = new JButton("Background Color");
	private JButton textButton = new JButton("Text Color");
	private JLabel greeting = new JLabel("Goodbye world!  Hello Java!");
	private Color[] bgColors = { Color.cyan, Color.orange, Color.pink };
	private Color[] txtColors = { Color.blue, Color.magenta, Color.red };
	private JPanel backgroundPane = new JPanel();
	private int bgCode = 0;
	private int txtCode = 0;

	public Colors() {
		super("Colors");
		
		backgroundPane.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 30));
		this.add(backgroundPane);
		
		backgroundButton.addActionListener(this);
		backgroundPane.add(backgroundButton);

		textButton.addActionListener(this);
		backgroundPane.add(textButton);

		Font font = new Font("Helvetica", Font.BOLD, 18);
		greeting.setFont(font);
		backgroundPane.add(greeting);
		
		greeting.setForeground(txtColors[txtCode]);
		backgroundPane.setBackground(bgColors[bgCode]);
		
		this.setSize(325, 200);
		this.setResizable(false);
		this.setLocationByPlatform(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}

	
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource(); // Who generated the event?

		if (source == backgroundButton) { // Was it the Background Color button?
			bgCode = ++bgCode % bgColors.length;
			Color color = bgColors[bgCode];
			backgroundPane.setBackground(color);
		}

		else if (source == textButton) { // Or was it the Text Color button?
			txtCode = ++txtCode % txtColors.length;
			greeting.setForeground(txtColors[txtCode]);
		}
	}


	public static void main(String[] args) {
		new Colors();
	}
}