/*Shape03.java 12/12/99
Copyright 1999, R.G.Baldwin
Illustrates use of the Shape interface.
Draws a 4-inch by 4-inch Frame on the screen.
Translates the origin to the center of the Frame.
Draws a pair of X and Y-axes centered on the new origin.
Draws a black circle on the screen with a diameter of
one inch centered at the origin.
Gets the bounding box for the circle and draws it
in red.
Draws a one-half inch green square completely inside
of the circle.
Draws a one-half inch blue square partial inside and
partially outside the circle.
Draws a one-half magenta square completely outside the
circle.
Tests the bounding box and the three one-half inch
squares to determine if they are contained in the circle.
Displays the results on the command-line screen.
Tests the bounding box and the three one-half inch
squares to determine if they intersect the circle.
Displays the results on the command-line screen.
The program produces the following output:
theCircle contains theBoundingBox: false
theCircle contains theInsideBox: true
theCircle contains theIntersectingBox: false
theCircle contains theOutsideBox: false
theCircle intersects theBoundingBox: true
theCircle intersects theInsideBox: true
theCircle intersects theIntersectingBox: true
theCircle intersects theOutsideBox: false
Tested using JDK 1.2.2 under WinNT Workstation 4.0
**************************************/
import java.awt.geom.*;
import java.awt.*;
import java.awt.event.*;
class Shape03{
publicstaticvoid main(String[] args){
GUI guiObj = new GUI();
}//end main
}//end controlling class Shape03
class GUI extends Frame{
int res;//store screen resolution here
staticfinalint ds = 72;//default scale, 72 units/inch
staticfinalint hSize = 4;//horizonal size = 4 inches
staticfinalint vSize = 4;//vertical size = 4 inches
GUI(){//constructor
//Get screen resolution
res = Toolkit.getDefaultToolkit().
getScreenResolution();
//Set Frame size
this.setSize(hSize*res,vSize*res);
this.setVisible(true);
this.setTitle("Copyright 1999, R.G.Baldwin");
//Window listener to terminate program.
this.addWindowListener(new WindowAdapter(){
publicvoid windowClosing(WindowEvent e){
System.exit(0);}});
}//end constructor
//Override the paint() method
publicvoid paint(Graphics g){
//Downcast the Graphics object to a Graphics2D object
Graphics2D g2 = (Graphics2D)g;
//Scale device space to produce inches on the screen
// based on actual screen resolution.
g2.scale((double)res/72,(double)res/72);
//Translate origin to center of Frame
g2.translate((hSize/2)*ds,(vSize/2)*ds);
//Draw x-axis
g2.draw(new Line2D.Double(-1.5*ds,0.0,1.5*ds,0.0));
//Draw y-axis
g2.draw(new Line2D.Double(0.0,-1.5*ds,0.0,1.5*ds));
//Define a one-inch diameter circle centered about
// its origin. Note that Ellipse2D implements Shape
Ellipse2D.Double theCircle = new
Ellipse2D.Double(-0.5*ds, -0.5*ds, 1.0*ds, 1.0*ds);
//Draw theCircle in the Frame in the default
// drawing color, black
g2.draw(theCircle);
//Get bounding box of theCircle
Rectangle2D theBoundingBox =
theCircle.getBounds2D();
g2.setColor(Color.red);//change the drawing color
//Draw the bounding box in the new color
g2.draw(theBoundingBox);
//Create boxes to test for contains and intersects
Rectangle2D.Double theInsideBox = new
Rectangle2D.Double(-0.25*ds, -0.25*ds,
0.5*ds, 0.5*ds);
Rectangle2D.Double theIntersectingBox = new
Rectangle2D.Double(0.3*ds, 0.3*ds,
0.5*ds, 0.5*ds);
Rectangle2D.Double theOutsideBox = new
Rectangle2D.Double(-1.25*ds, -1.25*ds,
0.5*ds, 0.5*ds);
//Draw the test boxes in new colors
g2.setColor(Color.green);
g2.draw(theInsideBox);//theInsideBox is green
g2.setColor(Color.blue);
g2.draw(theIntersectingBox);//theIntersectingBox blue
g2.setColor(Color.magenta);
g2.draw(theOutsideBox);//theOutsideBox is magenta
//Now perform the tests and display the results
// on the command-line screen.
System.out.println(
"theCircle contains theBoundingBox: "
+ theCircle.contains(theBoundingBox));
System.out.println("theCircle contains theInsideBox: "
+ theCircle.contains(theInsideBox));
System.out.println(
"theCircle contains theIntersectingBox: "
+ theCircle.contains(theIntersectingBox));
System.out.println("theCircle contains theOutsideBox: "
+ theCircle.contains(theOutsideBox));
System.out.println();//blank line
System.out.println(
"theCircle intersects theBoundingBox: "
+ theCircle.intersects(theBoundingBox));
System.out.println(
"theCircle intersects theInsideBox: "
+ theCircle.intersects(theInsideBox));
System.out.println(
"theCircle intersects theIntersectingBox: "
+ theCircle.intersects(theIntersectingBox));
System.out.println(
"theCircle intersects theOutsideBox: "
+ theCircle.intersects(theOutsideBox));
}//end overridden paint()
}//end class GUI
//=================================//
Figure 12
|