Using the IDE
Objectives
- Editing and compiling Java source code
- Recognizing and correcting compilation errors
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.
Exercises
- Create a project with name of "CIS180lab0";
- Create a new class with name "Colors"; the IDE will create a
"Colors.java" file for you with some automaticially generated contents.
- Copy and paste the program given below in the "Colors.java" file, to replace the original content.
/ 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[] txtColors = { Color.blue, Color.magenta, Color.red };
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);
}
}
class WindowCloser extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
e.getWindow().dispose();
System.exit(0);
}
}
- Type (not copy and paste) the code below inside the Colors.java file after the "paint" method. Try to
follow the same coding conventions. Enter the method 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 method, 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.
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.setVisible(true);
}
- 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:
- Some of the text is highlighted in color. What do you
suppose is the significance of each of the colors you see?
- How is the behavior of the "return" or "enter" key different
than in an ordinary editor?
- What do you observe when you type a closing bracket or
parenthesis, "), }, or ]"?
- When you have finished entering the method, save the file, compile and run it.
- If you made any typing mistakes, compiler will give you an error
message with the offending line marked. Make any necessary corrections.
- Repeat steps 6 and 7 until your program compiles and runs
sucessfully.
- 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 and gray. Compile and run
your program to be sure it behaves as expected.
- Modify the program so that it displays your name instead of the
text "Goodbye World! Hello Java!!". Compile and test.
- Submit your Color.java file in myCourse lab0.