Drawing Shapes The following is a simple applet that draws a blue rectangle on a
ID: 3788492 • Letter: D
Question
Drawing Shapes
The following is a simple applet that draws a blue rectangle on a yellow background.
// ************************************************************
// Shapes.java
//
// The program will draw two filled rectangles and a
// filled oval.
// ************************************************************
import javax.swing.JApplet;
import java.awt.*;
public class Shapes extends JApplet
{
public void paint (Graphics page)
{
// Declare size constants
final int MAX_SIZE = 300;
final int PAGE_WIDTH = 600;
final int PAGE_HEIGHT = 400;
// Declare variables
int x, y; // x and y coordinates of upper left-corner of each shape
int width, height; // width and height of each shape
// Set the background color
setBackground (Color.yellow);
// Set the color for the next shape to be drawn
page.setColor (Color.blue);
// Assign the corner point and width and height
x = 200;
y = 150;
width = 100;
height = 70;
// Draw the rectangle
page.fillRect(x, y, width, height);
}
}
Study the code, noting the following:
<html>
<applet code="Shapes.class" width=600 height=400>
</applet>
</html>
Save the files Shapes.java and Shapes.html to your directory. Now do the following:
1. Compile Shapes.java, but don't run it—this is an applet, so it is run through a browser or a special program called the Applet Viewer.
2. Run the program through your browser. You should see a blue rectangle on a yellow background.
3. Now run the program through the Applet Viewer by typing the command appletviewer Shapes.html You should see a new window open displaying the rectangle.
4. Now open the program in your text editor and change the x and y variables both to 0. Save and recompile the program, then view it in the Applet Viewer (this is generally less tr ouble when making lots of changes than using the browser). What happened to the rectangle?
5. Now change the width to 200 and the height to 300. Save, recompile and run to see how this affects the rectangle.
6. Change x to 400, y to 40, width to 50 and height to 200. Test the program to see the effect.
7. Modify the program so that it draws four rectangles in all, as follows:
One rectangle should be entirely contained in another rectangle
One rectangle should overlap one of the first two but not be entirely inside of it
The fourth rectangle should not overlap any of the others.
One last touch to the program... Change the colors for at least three of the shapes so the background and each of the three shapes are different colors (a list of colors is in Figure 2.10 of the text). Also change two of the fillRect methods to fillOval so the final program draws two rectangles and two ov als. Be sure that the overlap rules are still met.
Explanation / Answer
//Modified code as per given changes with output image.
// ************************************************************
// Shapes.java
//
// The program will draw two filled rectangles and a
// filled oval.
// ************************************************************
import javax.swing.JApplet;
import java.awt.*;
public class Shapes extends JApplet
{
public void paint (Graphics page)
{
// Declare size constants
final int MAX_SIZE = 300;
final int PAGE_WIDTH = 600;
final int PAGE_HEIGHT = 400;
// Declare variables
int x, y; // x and y coordinates of upper left-corner of each shape
int width, height; // width and height of each shape
// Set the background color
setBackground (Color.yellow);
// Set the color for the next shape to be drawn
// Assign the corner point and width and height
x=200;
y=150;
width=100;
height=70;
// Draw the rectangle
//First rectangle which is a Oval having blue color
page.setColor (Color.blue);
page.fillOval(100,100,300,300);
//Second rectangle inside the first rectangle
page.setColor(Color.red);
page.fillRect(x, y, width, height);
//Third overlaping rectangle with first rectangle
page.setColor(Color.cyan);
page.fillRect(60,60,130,110);
//Fourth rectangle which is seperate from all above rectangle
page.setColor(Color.green);
page.fillOval(400,40,100,60);
}
}
Output :
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.