Win2D and Axes


  Tutorial Home
 Install Eclipse You first need to have Eclipse installed on your machine. If you are in the CS labs then it will already be there. If you are working on your own machine the link to the Eclipse code is here.
Create an Eclipse Project

In Eclipse, all work is done in projects. You will need to create such a project. A video to show you how is here.

Adding the Phun Package

Before you can use any of the code that is part of the Phun package, you must first add that code to your project. If you do not know how to do this, there is a video here that will help you.

Creating a Java package Every program that you write in CS 142 should be in its own package. This will simplify many things for you. For this tutorial, create a package called "tutorial". If you do not know how to create a package, see the video here.
Creating a basic program

Every program begins with a class that contains the main(String [] args) method. The main method is where our program will start. We will call our starting class TutorialMain. If you do not know how to create a class using Eclipse, look at the video here.

The code for this class is as follows:

package tutorial;

import edu.byu.phun2d.*;      
		// Tells Java that you want to use the phun package
/**
 * Comment that describes what your program will do
 */
public class TutorialMain
{

    public static void main (String []args)
    {
        
    }
}
The above code is the starting point for any 2D program. When you create a new class in the tutorial package the "package" line, the basic comment and the "public class TutorialMain" lines will be created. You must add the "import edu.byu.phun.*;" line and you must add the main method as shown.
Creating a 2D window

All 2D graphics must be created inside of a Win2D window. This window knows how to display 2D graphics and also knows how to update itself whenever you add objects to the the window. To create new Win2D add code to your TutorialMain class so that it looks like this:

package tutorial;

import edu.byu.phun.*;      
    
/**
 * Comment that describes what your program will do
 */
public class TutorialMain
{

    public static void main (String []args)
    {
        Win2D win = new Win2D("Tutorial",10,20,400,300);
    }
}

This code will cause an empty window to appear on the screen as shown below:

EmptyWindow

The statement "Win2D win = new Win2D("Tutorial",10,20,400,300);" tells Java to create a new Win2D object and place it in a variable named "win". This object represents the empty window. The first parameter, "Tutorial", gives the title of the window. The values 10 and 20 are the coordinates of the upper left window on the screen. 10 says that the window is to be 10 pixels from the left edge of the screen and 20 says that the window is to be 20 pixesl from the top of the screen. The statement also says that the window is to be 400 pixels wide and 300 pixels tall.
Adding axes

We have an empty window, but there is nothing in it, which is not very interesting. The easiest thing to add is a set of coordinate axes. These are very helpful in understanding where we are in 2D. For most of our programs we will start by adding coordinate axes to help us with our work. We can always comment them out later so that they do not clutter up our picture.

To add the axes modify your program as shown below:

package tutorial;

import edu.byu.phun.*;      
    // Tells Java that you want to use the phun package
/**
 * Comment that describes what your program will do
 */
public class TutorialMain
{

    public static void main (String []args)
    {
        Win2D win = new Win2D("Tutorial",10,20,400,300);
        
        win.add(new Axes());
    }
}

The line "win.add(new Axes());" will create a new Axes object and will add it to the window that we named "win". The window will change to that shown below.

Axes

There are 2 arrows shown, one for each axis: X (red pointing to the right), Y (green pointing up). The colors are important because they identify which axis is which.