Lab 01: Hello Graphics!

an introduction to graphical programs in Java


DUE: Friday, February 19, 23:59 AOE


Prerequisites

Look at the graphics!

To get started download the file HelloGraphics.java, or copy and paste the code below into a new file.

click to view code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Dimension;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    
    public class HelloGraphics extends JPanel{
        public static final int BOX_WIDTH = 1024;
        public static final int BOX_HEIGHT = 768;
        public static final Color MAMMOTH_PURPLE = new Color(63, 31, 105);
        public static final Color SPRING_LEAF = new Color(91, 161, 81);
    
        public HelloGraphics(){
            this.setPreferredSize(new Dimension(BOX_WIDTH, BOX_HEIGHT));
        }
    
        //Your code here, if you want to define additional methods.
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            //Your code here: feel free to remove what is below
            
            g.setColor(MAMMOTH_PURPLE);
            g.fillRect(0, 0, BOX_WIDTH, BOX_HEIGHT);
    
            g.setColor(Color.WHITE);
            g.setFont(new Font("Times New Roman", Font.BOLD, 48));
            g.drawString("Protect the herd!", 50, 50);
    
            g.setColor(SPRING_LEAF);
            g.drawLine(50, 55, 500, 55);
    
            g.setColor(Color.GREEN);
            g.fillOval(50, 100, 30, 20);
    
            g.setColor(Color.RED);
            g.fillRect(100, 100, 20, 30);
    
            g.setColor(Color.BLUE);
            g.drawRoundRect(300, 200, 100, 200, 50, 100);
    
            g.setColor(Color.ORANGE);
            g.drawArc(200, 100, 200, 300, 90, 140);
        }
        
        public static void main(String args[]){
            JFrame frame = new JFrame("Hello, Graphics!");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setContentPane(new HelloGraphics());
            frame.pack();
            frame.setVisible(true);
        }
    }


You should get output that looks something like this:

Check out the documentation

Now that we’ve seen what the program does, it is time to figure out how. Look at the code, and reference it against the documentation for the the various objects we’ve used. In particular, you’ll want to look at

since these classes will be especially helpful in your assignment. You can glance at the documentation for the other classes used in HelloGraphics.java

but you don’t need to worry too much about these for now.

IYI: the colors MAMMOTH_PURPLE and SPRING_LEAF come from Amherst College’s Visual Identity Toolkit. There you can find resources to make things look Amherst-y.

Your assignment

To complete this lab you should:

  1. Write a method that draws some object in a specified location. This can be a stick figure, a building, a mammoth, or whatever you want, but it needs to contain at least 5 base shapes (ovals, rectangles, etc). Your method should take as arguments the location of where to draw the object (in addition to any other information you want to pass to the method). If your figure is complicated, it might even make sense to have multiple methods that draw the different parts of the figure.

  2. Draw 25 copies of your object in a 5 x 5 grid.

Note. Making 25 copies in a grid naively is a painful process of busywork. I suggest using (nested) loops to draw the grid of objects.

Extension. To challenge yourself (and get some extra credit), make it so that the images in the grid are not identical, for example by distorting the image or changing its color based on its position in the grid. Also, you can make a cool background for your grid of objects. Below is an example that I put together while procrastinating from doing more productive work. Note that there is a grid of 25 dogs of varying shapes and sizes!

What to submit

Please turn in your version of HelloGraphics.java to the Moodle submission site. Be sure to fill out the comments at the beginning of the file with your name and a description of your program. Additionally, complete this survey/self-assessment for the assignment.

Grading

Your basic assignment will be graded on a 3 point scale as follows:

  • 3 points. Submission produces the desired output in the manner specified by the assignment. Code is clearly organized with comments describing what different components of the code (e.g., methods) do.

  • 2 points. Submission produces more-or-less the desired output, but may diverge from the program description in the assignment. Code is not clearly organized and/or lacking helpful comments.

  • 1 point. Submission compiles, but does not produce the desired output. Code is confusingly written without useful comments.

  • 0 points. Submission does not compile.

In addition to the points above, you will receive credit for any extenion(s) as specified in the course syllabus.