Practice Quiz 5    Section Leader _____________ Name ____________________________

 

1. Complete the code in paintComponent to draw the three shape shown . The bigger circle has the upper left corner at Point(30, 30).  The border with a 5 pixel gutter should stay within 5 pixels even if the JFrame is resized. 10pts

 

public class MainDrawOnPanel extends JFrame {

  public static void main(String[] args) {

    new MainDrawOnPanel().setVisible(true);

  }

 

  public MainDrawOnPanel() {

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setTitle("Match 3 shapes");

    setSize(280, 200);

    getContentPane().add(new QuizPanel());

  }

}

 

 

public class QuizPanel extends JPanel {

 

  @Override

  public void paintComponent(Graphics g) {

    super.paintComponent(g);

    Graphics2D g2 = (Graphics2D) g;

    g2.drawString("Outer rectangle has 5 pixel gutter", 10, 20);

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  }

}

In this problem, you will use the object-oriented design pattern Strategy to begin a Background manager strategy consisting of just two different (and rather simple, and silly) BackGround managers (these were made to be intentionally simple). One background manager draws many ovals in a specified width and height (that code is actually given). The other background manager draws a rectangle with a 10 pixel border in a specified color. Here is a main method that uses the two Background strategies you will implement along with the graphical output. You will be implementing several classes described after this page. The call to JOptionPane.showMessageDialog is here simply to cause a pause. This program must generate the frames and dialog shown below.

 

public class TestStrategy {

 

  public static void main( String[] args )  {

    // Default is to have a white background

    BackgroundFrame f = new BackgroundFrame();

 

    f.setBackgroundShape(new SquareBackground(15.0));

    JOptionPane.showMessageDialog(null, "Blue squares");

 

    f.setBackgroundShape(new OvalBackground(40.0, 20.0));

    JOptionPane.showMessageDialog(null, "Red ovals" );

  }

 

}

    

            

 

 

          

                                                            

 

Use the UML diagram on the next page to answer the questions that follow


Strategy Pattern made specific for Silly Background

 

 


2. Write Background as if it were in its own file. (4 pts)

 

 

 

 

 

 

 

 

 

3. Write BackGroundFrame as if it were in its own file. Make it 200 x 200 pixels. By default, there BackgroundFrames use nothing drawn to the JFrame. The program shown above must terminate on the WindowClosing event (6 pts)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

4. Complete  OvalBackground around the method given as if it were in its own file. The single drawBackground method is begun to show that you need to erase the old background first (we are not replacing panels).  (8pts)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  public void drawBackground(JFrame f) {

    // Get the drawing context

    Graphics g = f.getContentPane().getGraphics();

    Graphics2D g2 = (Graphics2D)g;

    // Clear old background

    g2.setPaint(f.getBackground());

    g2.fill(new Rectangle(f.getHeight(), f.getHeight() ) ); 

 

    // Draw background using a algorithm for drawing as many ovals as possible

 

 

 

 

 

 

  }

} // end class OvalBackground

 

5. Completely write SquareBackground as if it were in its own .java file.  (12pts)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

6. Describe what you would have to do to add a third background strategy to your system. Do NOT write the new strategy. (4pts)