Download notes

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CPSC 224: “Paint” Lab
For this lab, you should work in groups of 2–3. Work on the following steps in order. For many
parts you’ll need to look at the Java API. At any time if you get stuck, raise your hand and I’ll
come and help you. An example of the final product is shown at the end of the document.
STEP 1. Create a JPanel subclass called PaintLab that contains the following:
(a). The imports:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
(b). A private enum type: private enum Mode {CIRCLE, SQUARE}
(c). A main method that creates a frame object containing an instance of PaintLab, sets the
frame’s title to “Paint Lab”, sets the default close operation to exit, and makes the frame
object visible.
(d). A default constructor that does the following:
public PaintLab() {
// creates a JButton named "Draw Circle"
// creates a JButton named "Draw Square"
// creates a buttonPanel (JPanel) using BoxLayout, X_AXIS
// adds the two buttons to buttonPanel
// sets the current panel to BoxLayout, Y_AXIS
// adds the buttonPanel
}
Make sure you can compile and run PaintLab.
STEP 2. Create a private inner class of PaintLab called DrawingPanel of type JPanel. Define
the following for DrawingPanel:
(a). Private instance variables:
private
private
private
private
boolean paintFlag = false;
Mode theMode = Mode.CIRCLE;
int width;
int height;
(b). A constructor:
private DrawingPanel(int width, int height) {
this.width = width;
this.height = height;
// set the preferred size to width by height
}
(c). A public method setPaintFlag that sets the paintFlag variable to true and theMode to
the given Mode argument. The method does not return anything.
(d). Override the paintComponent method with the following.
public void paintComponent(Graphics g) {
// return if paint flag is false
super.paintComponent(g); // clears screen
// get the fancier 2D graphics object
Graphics2D g2 = (Graphics2D)g;
// create a random color
int red = (int)(Math.random() * 255) + 1;
int green = (int)(Math.random() * 255) + 1;
int blue = (int)(Math.random() * 255) + 1;
// set the color
g2.setColor(new Color(red, green, blue));
// create a random width
int w = (int)(Math.random() * (width/2));
// create a random location
int x = (int)(Math.random() * width) + 1;
int y = (int)(Math.random() * height) + 1;
// draw the shape:
// if the mode is set to CIRCLE, call: g2.fillOval(x, y, w, w)
// else if mode is set to SQUARE, call: g2.fillRect(x, y, w, w)
// turn off the paint flag
g.dispose();
}
Make sure you can compile and run PaintLab (although it won’t look any different from before at
this point).
STEP 3. Go back to the PaintLab constructor and fill in the remaining parts.
(a). Define a local drawingPanel variable of type DrawingPanel. You should set the width and
height to a reasonable value.
(b). Add the two button listeners. Each listener should call the setPaintFlag method on the
drawingPanel (with the appropriate Mode passed in), and then call drawingPanel.repaint().
(c). Add the drawingPanel to the PaintLab panel. The buttons should be below the drawingPanel.
Compile and run PaintLab. It should work now.
STEP 4. Extend PaintLab to support a raised squared in addition to circles and squares. Look
at the fill3DRect method in the Java API under the Graphics2D class to see how to create a
raised square. You will need to add a button labeled “Draw Raised Square”.
2
STEP 5. Extend PaintLab to support adding the string “Hello World!” to the panel. Again, look
in the Graphics2D class at the method drawString(String, int, int). Add a button labeled
“Draw Hello Word” to the applet.
STEP 6. If you have time, add an additional shape to PaintLab (e.g., a Polygon, an Arc, an Oval,
etc.).
STEP 7. Show me your PaintLab class before you leave.
3