Computer Lab #1In this lab, we practice writing programs with the java IDE
Eclipse.
Our programs may require using the UW graphics library. The
documentation is here.
I) Writing a class
Writing, compiling and executing a program in Eclipse. The lectures slides are available here.
- Start Eclipse and select File>New>New JavaProject. A project dialog box appears. In the textfield at the top, give a name to your
project (your choice, e.g. lab1). Leave all of the options as they are and click Finish.
- You should see your project in the Package Explorer window. Expand its folder by clicking on the + symbol next to the folder. Right click on the src folder (below the project folder) and select New > Class. A class dialog box appears. Enter the name Person and click Finish.
- Write the Person class as described in the lecture
slides. If you notice any red squiggly lines as you type, it means that your program has some syntax errors (Eclipse compiles your code as you type it). Correct the errors by comparing your code to what is written on the class slides. Remember that Java is case sensitive.
- Write also the client class PersonUser (also described in the slides)
- To run your program, just click on the Run button (the green round button with a right white arrow).
- Add more methods to the class, e.g. another constructor (default) or a method to change the age.
- Close Eclipse. On your computer, navigate to the folder of the
Eclipse project that you just used. It should contains an src folder that contains a
Person.java file (the source of your program). This is the only file that you need to save (e.g. on your thumb drive). All of the other files are automatically recreated when Eclipse is restarted. For instance, notice
the bin folder that contains the Person.class file. It is the result the
compilation of the source code in byte code.
- If you want to work again on your program, restart Eclipse and
create another project. Instead of creating a new class, drag and drop your class file into the src folder of your newly created project.
Of course, there is more to Eclipse than what is described above.
Consult this help page to learn more about the IDE.
II) Doing graphics
- Start Eclipse and select File>New>New JavaProject. A project dialog box appears. In the textfield at the top, give a name to your
project (your choice, e.g. lab1). Leave all of the options as they are and click Next. Select the Libraries tab, and click on "Add External JARs...". Navigate to the location of the UW library file, select the file, and click Open. Close the Project dialog box by clicking Finish.
- You should see your project in the Package Explorer window. Expand its folder by clicking on the + symbol next to the folder. Right click on the src folder (below the project folder) and select New > Class. A class dialog box appears. Enter the name WindowWithCircle and click Finish.
- Write the program described in the lecture
slides. If you notice any red squiggly lines as you type, it means that your program has some syntax errors (Eclipse compiles your code as you type it). Correct the errors by comparing your code to what is written on the class slides. Remember that Java is case sensitive.
- To run your program, just click on the Run button (the green round button with a right white arrow). A (not so fancy) window with a circle
should appear in the upper left corner of your screen.
- Try to improve the class by answering some of the questions
listed in the lecture slides.
- How to change the color of the circle, the location of the
circle, the size of the circle? How to fill the circle?
- Consult the UW Library documentation and use some of the
methods available for Oval objects. Here is a solution.
- Close Eclipse. On your computer, navigate to the folder of the
Eclipse project that you just used. It should contains an src folder that contains a
WindowWithCircle.java file (the source of your program). This is the only file that you need to save (e.g. on your thumb drive). All of the other files are automatically recreated when Eclipse is restarted. For instance, notice
the bin folder that contains the WindowWithCircle.class file. It is the result the
compilation of the source code in byte code.
- If you want to work again on your program, restart Eclipse and
create another project. Instead of creating a new class, drag and drop your class file into the src folder of your newly created project
III) More graphics programs
- Create a class SomeGraphics to display 2 circles (one red and
one blue) side by side. This is another program from the lecture
slides. Here is a solution.
- More challenging: create a class to display a traffic light.
The class should include method to turn the light to green, red or
yellow. Here is a solution.
- We write a program to display shapes made of squares. Use the
Pen class from the UW library. Start from this file.
Here is a solution.