Title: JH4 Drawing part Help
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

JH4 Drawing part Help

- I want to first of all demo the final product that you should have when you complete JH4.
- In my published answer for JH3, I left in the optional part to do the polygon drawing. In my published answer to this one, you will also see the Polygon drawing code. I would like to demo how it works. Note that Polygon drawing works quite differently from the other drawing primitives. Remember, you don't have to do the Polygon drawing unless you have time for an additional challenge.

Steps to completing JH4

1. You will want to take your solution from JH3, or the published answer for JH3.
2. The Drawing.java part can stay the same.
3. The old DrawingProg.java will be renamed to DrawingPanel.java. In this code you will change the class to extend JPanel instead of JFrame. In this process, you will discover a few things that need to be changed. In most cases it will be things to eliminate.
4. You will now create a new DrawingProg2.java class that uses your DrawingPanel class in the Center of a BorderLayout. The picture below shows the overall structure.
5. Instead of having a main which reads in commands from a Scanner class, you will add Components that will live in the North and West region of your overall BorderLayout.
6. I will show you a template of the new approach for DrawingProg2.java (end of these notes) which will outline the steps described in this set of notes.

Once you have added your components to the North and West regions of the BorderLayout, your screen can look like the following:

Note that when you pick "line" or "scribble", the "filled" checkbox is meaningless. It is nice to make this CheckBox invisible when you are in "line" or "scribble" mode.

So in scribble mode or line mode, your screen would look like the following:

North Panel

1. The North Region is filled with a JPanel which uses a FlowLayout.
2. We have one CheckBox class for the "Filled" part.
3. We have a Radio Button Group containing the RadioButtons for rectangle, oval, line, and scribble
4. Using the setVisible method, we make the "Filled" CheckBox visible when rectangle, or oval are selected. The CheckBox is invisible when line or scribble are selected.
5. Note that RadioButtons, and CheckBoxes respond to the ActionListener interface the same way that JButton does.

West Panel

1. The West Region is filled with a JPanel using a GridLayout.
2. GridLayout can be constructed with either:
new GridLayout(3,1) or
new GridLayout(0,1)
3. If you use GridLayout and set the number of rows to zero (note you need to specify the number of columns for this to work), then GridLayout adds rows on an as needed basis. So adding 3 components generates 3 rows in our case.

DrawingProg2.java

template for DrawingProg2:


//appropriate imports

public class DrawingProg2 extends JFrame implements ActionListener{

    DrawingPanel drawingPanel = new DrawingPanel();
    JCheckBox filled = new JCheckBox("filled");

    DrawingProg2()
    {
        super("My Drawing Program");

        String[] colors = {"red", "green", "blue"};
        String[] shapes1 = {"rectangle", "oval","line","scribble"};

        setSize(800, 400);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        layout(shapes1, colors);

        setVisible(true);
    }

    private void layout(String[] shapes, String[] colors )
    {
       // set defaults
        drawingPanel.drawing.setColor(Color.red);
        drawingPanel.drawing.setDrawType(DrawType.rectangle);

        setLayout(new BorderLayout());

/*
        drawingPanel goes in the CENTER
        Create a JPanel with a FlowLayout for the NORTH.
        This JPanel will get the filled JCheckBox and all of the necessary radio buttons

        Create a JPanel with a GridLayout(0,1) or GridLayout(3,1) for the WEST.
        This JPanel will get the radio buttons for the colors.

        All RadioButtons and the CheckBox will have ActionListeners associated with them.
*/


    }

    public static void main(String[] args) {
        DrawingProg2 dp = new DrawingProg2();

    }

    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        String action = actionEvent.getActionCommand();
        System.out.println(action);

        switch(action)
        {
        case "red":
            drawingPanel.drawing.setColor(Color.red);
            break;
/*
  Handle all of the other actions, and make sure you set the visibility of
  the filled CheckBox appropriately.
  The other methods to be called are:
         drawingPanel.drawing.setDrawType(....);
         drawingPanel.drawing.setFilled(.....);
*/

        }


    }

}


Last Modified: July 28, 2014 11:23 AM