To gain some understanding of arrays in Java.
To see a first while loop.
Go over the notes: topic 5.
In this exercise, you will practice initializing and working with an array of integers.
In the Interactions pane, type the following:
int[] nums = {1,2,3,4,5}; System.out.println(nums[0]);
How would you now print the rest of the items in the array nums?
Type the following statement: System.out.println(nums[nums.length]);
What happens? Why?
In the Interactions pane, make a variable pictureObj refer to a picture that contains the image in the file caterpillar.jpg that is in your mediaSources folder.
We have seen the following methods:
getPixel(x,y)
of the Picture
class, which allows you to access one of the pixels in a picture
setColor(color)
of the
We will use these methods to change the color of the following pixels of the caterpillar picture to black:
One way to do this is to type the following sequence of Java statements in the Interactions pane (note that you can repeat the previous line in the Interactions pane by using the up-arrow key, and then edit the line before hitting Enter for that line - this saves a lot of typing)
import java.awt.Color; pictureObj.getPixel(100,100).setColor(Color.BLACK); pictureObj.getPixel(101,101).setColor(Color.BLACK); pictureObj.getPixel(102,102).setColor(Color.BLACK); pictureObj.getPixel(103,103).setColor(Color.BLACK); pictureObj.getPixel(104,104).setColor(Color.BLACK); pictureObj.getPixel(105,105).setColor(Color.BLACK); pictureObj.getPixel(106,106).setColor(Color.BLACK); pictureObj.repaint();
In the Definitions pane, create a new class Drawing
and a static method drawLine
that takes as
input a Picture
, returns nothing, and performs the
same drawing as above. Compile it, and test it in the Interactions
pane on a new picture.
Modify your drawLine
method, so that it takes
two extra int
parameters, that specify the starting
point for the line (here, that was (100,100)).
We have seen the method getPixels
that returns an
array containing all pixels from an image, arranged row after row. It is
possible to use it to draw the same line as in the previous
exercise, without using getPixel
(note the difference: getPixels
vs. getPixel
; the latter gives access to a
single pixel).
In this exercise, you are asked to write (and compile, and test) a
static method drawLineArray
, that takes the same
arguments as drawLine
, and draws the same line, but using
the array given by getPixels
.
You will need the object method getWidth()
of the
Picture
class, which gives you the width of a
picture. Suppose that the variable width
holds this
value, and that arrayPixels
is the array you obtained
from getPixels
, then arrayPixels[100+50*width]
is the pixel at position (100,50).
Loops will be introduced in topic 6. For the moment, we will
practice with a simple construction,
the while
loop.
Type the following in the Interactions pane:
int counter = 1; while (counter <= 10) { System.out.print("*"); counter = counter + 1; } System.out.println();The
while
construction ensures that the body
of the loop (the two statements in the middle) are executed
while the condition counter <= 10
holds.
What happens if you remove the statement counter = counter + 1;
? Why?
To stop it, click on the Reset button.
Write a static method printAsterisks
that takes
an int n
as input, return nothing, and prints n
asterisks. This method should be in a new class Asterisks
.
Compile; test.
Modify the drawLine
method, by adding a new int
parameter to it, say n
, so that it draws a line made of n
pixels.