Lab 2: Due September 25, 2:29 p.m.

In this lab you will begin to learn how to draw in Java and to build graphical interfaces.   For starters we will look at the default Java swing project .  The course materials folder contains a modified version of the program file, in this case called Lab2.java   The modifications are just comments that I have added that explain what the program does in much more detail (as well as providing some initial guidelines on what I expect in your programs).  All the program does is create a window that displays "Hello World" in big blue letters.  Your job will be to allow for the creation of additional windows that can be used for rudimentary drawing.  In subsequent labs we will in turn modify the classes you develop to allow for more sophisticated drawing.

 

Part I - understand the default program.

 

You should carefully read the contents of the version that I have modified.

 

Part II - creating a drawing window.

 

The second thing you will do is allow for the creation of a new window that the user can use for simple drawing.  To do this you should modify the original program such that one of the menu items causes the creation of a new window.  This window will be of type "DrawingBoard", a class that you will define yourself.

 

The DrawingBoard class should, like the default class, extend the JFrame class.  However, instead of interacting with this class through menus, the user will interact with it through mouse clicks for drawing.  Therefore, instead of implementing an ActionListener this class will need to implement the Java interface "MouseListener."

 

The MouseListener interface calls for five methods that you will have to include in your class.  These are as follows:

 

public void mouseEntered(MouseEvent e)

    {

        // your code goes here - the mouse enters the window

    }

   

    public void mouseExited(MouseEvent e)

    {

        // your code goes here - the mouse leaves the window

    }

   

    public void mouseClicked(MouseEvent e)

    {

        // your code goes here - the mouse was clicked

    }

   

    public void mousePressed(MouseEvent e)

    {

        // your code goes here - the mouse was pressed

    }

   

   

    public void mouseReleased(MouseEvent e)

    {

        // your code goes here - the mouse was released

    }

 

Your program should behave as follows.  When the user presses the mouse they define the first endpoint of a line.  When they release the mouse they define the other endpoint and the line should be drawn.  When the mouse is clicked, the window should be reset (blanked).

The window should also be reset if the user moves it (see the "paint" method in the default program).

 

Some useful Java functions for implementing this task:

The MouseEvent  class contains methods getX() and getY() that return the X and Y position of the mouse respectively.

 

The Graphics class contains a drawLine(x,  y, x1, y1) method that draws a line from position x, y to position x1, y1 within  the graphics window.

 

The Graphics class contains a fillRect(x,  y,  width, height) method that fills in a rectangle from corner x, y within  the graphics window.  This can be used to blank a window.

 

JFrame windows can be sized with the setSize(x, y) method.  I recommend a size of 500x500 for starters.

 

To use these methods you will need access to the Java libraries javax.swing.event.MouseInputAdapter, and java.awt.event.

 

Part III - expanded capabilities

 

Once you get the window to draw lines it is time to expand the drawing capabilities.  We would also like to be able to draw rectangles and ovals.  The trick here is that you will need a way to know what type of thing the user wants to draw.  One simple way to do this is to provide the user with buttons that they can select - one to choose lines, one to choose rectangles, and one to choose ovals.  Once the user has pressed a button, it determines the next thing to be drawn.

 

Buttons are fairly simple to use in Java.  One way to accomplish this task would be to add buttons to our DrawingBoard class (you can do this if you like).  The difficulty is that it requires managing the layout of the window which adds additional complexity.  Instead we will just create another window that only contains our buttons.  To do this you will need to create another class.  Call this one "Choosing" or something similar.  This class can be very simple.  It merely sets up a window with 3 buttons and makes sure your main program knows which button has been selected most recently.

 

As part of our continuing lesson in Java graphics we will implement this window a bit differently than the other one.   This time, since we aren't drawing, we will simply add a component to our JFrame called a JPanel.  You can create a new panel in the obvious way: new JPanel("panel name").  Once you have created a panel then you can add buttons (called JButtons) to it.  These too are made in the obvious way: new JButton("button name").  So you'll have one button for lines, one for rectangles, and one for ovals.  Once you have created them you can use the JPanel "add" method to add them to the panel. 

 

Of course, you will also need to know when the button has been clicked.  To do this you can define an action listener local to the Choosing class.  You can define a class within a class to do this.  For instance, say I have defined a JButton called rect.  I can directly add a listener for it as follows:

 

       rect.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                     // whatever you want to do when clicked;

            }

        });

 

Finally, you'll need to add the JPanel to the frame (your base class).  You'll also want to do this in a way that formats the buttons appropriately.  The following will work assuming a JPanel name "pane":

 

this.getContentPane().add(pane, BorderLayout.CENTER);

 

Yes, creating a graphical interface means you have to do a lot of technical junk.  Ultimately this will become easy to you though.  And it will make programming much more fun.  In any case the real trick of the buttons is using them to communicate with your other window.  This is a classic problem in object oriented program - how do I make different components communicate with each other?  There are at least two reasonable solutions to this problem.  One is to give the components access to each other.  A better solution in this case is to communicate through the main class (the class where "main" is defined).  In this case when a button is pressed the result should be to call a method in the main class that in turn calls a method in the DrawingBoard class.  The method in the DrawingBoard class should then set some sort of variable such that it knows to draw the appropriate type of item.  Remember, to call a method in the main class means that you will need an instance of the main class, which in turn means that you have to pass it in to the constructor of your Choosing class.

 

Back in the DrawingBoard class you need to account for the fact that you won't always want to draw lines anymore.  You also need to be able to draw rectangles and ovals.  To draw an oval use drawOval(x, y,  width,  height);   Make sure that your method does not rely on the user moving the mouse in a fixed direction.

 

What to hand in.

 

A working project folder with your name (e.g. ChownLab2).  Of course the java files should be commented to include your name and other information as indicated in the marked up version.  Drag a copy of the folder to the course drop box.

 

Grading issues.

 

As an early lab I will be tough on lots of issues that have nothing to do with whether your program runs properly - commenting, readability, use of constants, etc.  A good rule of thumb is to overdo it if in doubt at this stage.  I will also look favorably on people who explore a bit in their labs.  E.g. not only adding buttons to choose drawing types, but also buttons to select the color, etc.  Nonetheless, the main goal of this lab is to get you comfortable with some graphics programming and that will constitute the bulk of the points given.