University of Washington, CSE 142
Lab 3: Parameters, Graphics
Except where otherwise noted, the contents of this document are
Copyright 2013 Stuart Reges and Marty Stepp.
lab document created by Marty Stepp, Stuart Reges and Whitaker Brand
Basic lab instructions
-
Mouse over highlighted words if you're not sure what they mean!
-
Talk to your classmates for
help.
-
You may want to bring your textbook to future labs
to look up syntax and examples.
-
Stuck? Confused? Have a question? Ask a TA for
help, or look at the book or past .
-
Complete as much of the lab as you can within the allotted time. You don't need to keep working on these exercises after you leave.
-
Feel free to complete problems in any order.
-
Make sure you've signed in on the sign-in sheet before you leave!
Today's lab
Goals for today:
-
learn the basics of declaring and passing parameters to
methods
-
use
String
s to represent and manipulate text data
-
Learn to set a break with the jGRASP debugger to find out what is going
on inside a program
-
use the instructor-provided
DrawingPanel
and Java's Graphics
and Color
classes
Parameters
A parameter allows you to pass in a value (an expression or a variable!) to a method as you call it.
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public static void main(String[] args) {
squared(3);
squared(8);
int x = 5;
squared(x);
squared(2 + 2)
}
public static void squared(int num) {
System.out.println(num + " times " + num + " is " + (num * num));
}
|
[an error occurred while processing this directive]
Graphics
Now we'll explore several exercises related to drawing graphics. (none)
-
We'll use a provided class
DrawingPanel
that works with Java classes Graphics
(a "pen" for drawing) and Color
.
-
Download
DrawingPanel.java
by right-clicking the link and selecting "save as".
-
You can't open DrawingPanel by clicking on an icon. But if you make a DrawingPanel in Java (which we'll do soon!), a panel will open in a pop-up! You can interact with this window with your mouse, and can check the correctness of your drawings in
DrawingPanel
by clicking File, Compare to Web File....
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
Parameterized methods and Graphics
When you want to divide a graphical program into multiple drawing methods,
you must pass Graphics g as a parameter in addition to any other
parameters. Example:
1
2
3
4
5
6
7
8
9
10
11
|
public static void main(String[] args) {
DrawingPanel panel = new DrawingPanel(400, 300);
Graphics g = panel.getGraphics();
...
drawStuff(g, 13, 52, 7);
}
public static void drawStuff(Graphics g, int a, int b, int c) {
g.drawLine(a, 45, b, c);
...
}
|
[an error occurred while processing this directive]
[an error occurred while processing this directive]
If you finish them all...
If you finish all the exercises, try out
our Practice-It
web tool. It lets you solve Java problems from our Building Java
Programs textbook.
You can view an exercise, type a solution, and submit it to see if you
have solved it correctly.
Choose some problems from the book and try to solve them!