📄 printandmeasureshapes.java
字号:
/**
* Title: PrintAndMeasureShapes.java
* Description:This java file can create a java applet for whole program.
* And it print shapes and measure shapes
*
* Copyright: Copyright (c) 2006
* @author Q Jin
* @version 1.0
*/
import java.awt.*;
import javax.swing.*;
/**
* This is a PrintAnd MeasureShapes java file
* It draw the shapes for whole program
*
* @param rectNumber
* @param ovalNumber
*/
public class PrintAndMeasureShapes extends JApplet{
/**
* This is a paint java file
* It draws shapes.
*
* @param x width of the shapes
* @param y height of the shapes
*/
public void paint( Graphics g ) //throws IllegalRectangleException
{
// access the method of the superclass
super.paint( g );
// declare and initialize variables
int rectNumber = ( int )(Math.random()*3);
int ovalNumber = ( int )(Math.random()*4);
// access the method of subclass and set red color
g.setColor( Color.red );
// draw the shapes use random width and height
for( int i=1; i<=rectNumber; i++ ){
int x = ( int )(Math.random()*10+1);
int y = ( int )(Math.random()*10+1);
// If the determined width and height numbers are the same, then this is not a valid
// rectangle. Create the IllegalRectangleException class to signal when
// an illegal rectangle has been created.
if( x == y ) throw new IllegalRectangleException();
Rectangle rect = new Rectangle( x, y );
// draw the shape with changing color
g.fillRect( 100*i-90, 10, x, y );
g.drawString( "Area:"+rect.calculateArea(),100*i-90, 40 );
g.drawString( "Perimeter:"+rect.calculatePerimeter(), 100*i-90, 60);
}
g.setColor( Color.GREEN );
// draw the shapes using random width and height
for( int i=1; i<=ovalNumber; i++ ){
int x = ( int )(Math.random()*15+1);
int y = ( int )(Math.random()*15+1);
// create a new oval object
Oval oval = new Oval( x, y );
// draw the oval with changing color
g.fillOval(100*i-90, 70, x, y );
// If the determined width and height numbers are the same (i.e., the oval is a circle)
// then determine and print their area and perimeter
// assume that the oval’s height and width are given in centimetres.
if( x == y ){
g.drawString( "Area:"+oval.calculateArea(), 100*i-90, 110);
g.drawString("Perimeter:"+oval.calculatePerimeter(), 100*i-90, 130);
}
}
g.setColor( Color.YELLOW );
g.drawString( "Total "+(rectNumber+ovalNumber)+" shape(s)", 10,150);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -