CS 145 Lab 4 – More Methods
Welcome to lab 4!
If you have checkpoints from the last lab to show your instructor or TA, do so immediately. No credit will be given if you have not already completed the work, nor will credit be given after the first 10 minutes of this lab.
Checkpoint 1
Person A types.
You’ve seen Practice-It!, which accompanies the textbook and is great for trying out the ideas you read about. There’s also Coding Bat. Go there now and make an account.
Complete the follow exercises:
- makeTags
- withoutEnd
- left2
- nTwice
- intMax (don’t use an
if
statement!)
If you feel like you could use extra practice with the concepts we discuss in this class, keep working away at the problems on Coding Bat and Practice-It! Don’t wait for them to be assigned.
Checkpoint 2
Person B types. Your next task is to use the Graphics2D
class to programmatically draw a scene.
Open your IntelliJ project and create a package named labs.lab04
. With the lab04
package selected, paste in the class Canvas
listed below. IntelliJ will automatically create the class for you.
package labs.lab04;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Canvas {
private JFrame frame;
private BufferedImage image;
public Canvas(int width, int height) {
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
SwingUtilities.invokeLater(() -> {
frame = new JFrame("Canvas");
frame.add(new CanvasPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, height);
});
}
public Graphics2D getGraphics() {
return image.createGraphics();
}
public void show() {
SwingUtilities.invokeLater(() -> {
frame.setVisible(true);
});
}
private class CanvasPanel extends JPanel {
@Override
public void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, null);
}
}
}
This class pops open a window to view an image. Do not modify it in any way; your other code will construct an instance of it.
Create a class named Scene
and add these lines to its main
method:
Canvas canvas = new Canvas(512, 512); // feel free to adjust the height and width
Graphics2D graphics = canvas.getGraphics();
// call drawing methods here
canvas.show();
Complete the following tasks:
- Consult the Graphics documentation for a list of methods that let you draw on a canvas.
- Use the
Graphics2D
object to construct a scene fitting some theme. For our discussion, suppose you choose winter. Use the variousdraw*
andfill*
methods to accomplish your task. Change the color with code like the following:Programmatically draw at least three unique objects related to your theme in various colors. For winter, you might draw a pile of snowballs, an evergreen tree, and a gift box tied with a ribbon under the tree.// Use a pre-defined color constant. Replace RED accordingly. graphics.setColor(Color.RED); // Use an RGB triplet. Replace R, G, B with ints in [0, 255]. graphics.setColor(new Color(R, G, B));
- Write a heavily-parameterized method that you call to draw some object in your scene several times. For winter, you might create
drawSnowperson
that accepts twoint
s representing the x and y coordinates of a snow person’s base or origin point (and any other parameters you might need). You write this method to draw the snow person relative to this base position, so that you can call it multiple times frommain
to create an army of snow people.