📄 printandmeasureshapes.java
字号:
import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
import java.util.ArrayList;
/**
* A class to print and mesure the shapes. It will creat a windows and show
* random number of rantangles and ovals with different colors.If the rectangle
* is square rectangle,throw exception.Otherwise, show the area and perimeter of
* rectangle. For oval,only show the area and perimeter when it is a circle.
*
* @author Ruixiao Wu.
* @version 1.0 26/06/2006.
*/
public class PrintAndMeasureShapes extends JApplet {
/**
* A class extends JApplet.
*
* @author Ruixiao Wu.
* @version 1.0 26/06/2006.
*/
private static final long serialVersionUID = 1L;
public void init() {
Container container = getContentPane();
DrawPanel a = new DrawPanel();
container.add(a);
}
class DrawPanel extends JPanel {
/**
* A class extends JPanel.
*
* @author Ruixiao Wu
* @version 1.0 26/06/2006.
*/
private static final long serialVersionUID = 1L;
/**
* A method to paint the shapes.
*/
public void paintComponent(Graphics g) {
super.paintComponents(g);
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(Color.BLUE);
g2.fillRect(0, 0, 300, 300);
double leftX = 10;// the x coordinate.
double topY = 10;// the y coordinate.
Rectangle rect = new Rectangle();// creat a new object.
Oval o = new Oval();// creat a new object.
ArrayList<Rectangle2D> rectList = new ArrayList<Rectangle2D>();// creat
// and
// store
// the
// retangle
// into
// an
// ArrayList.
Rectangle2D j;// creat a new rectangle2D object.
int numOfRec = rect.numOfRec();// initial the number of rectangle.
for (int i = 0; i < numOfRec; ++i) {// a for loop to initial the
// rectangle and draw them.
rect.initList();
rect.calculateArea();
rect.calculatePerimeter();
double rectWeigth = rect.RecList.get(0);
double rectLength = rect.RecList.get(1);
j = new Rectangle2D.Double(leftX, (topY + i * 35), rectWeigth,
rectLength);
rectList.add(j);
g2.setPaint(Color.RED);//set color.
try {// the exception.
if (rect.checkParam() == true) {
g2.fill(rectList.get(i));
g2.drawString("The area is:" + rect.getArea()
+ " square cm." + " The Perimeter is:"
+ rect.getPerimeter() + "cm", (int) (leftX),
(int) (topY + 28 + i * 30));
} else
throw new IllegalRectangleException();
} catch (IllegalRectangleException e) {
g2.drawString(
"This is not a valid rectangle! I do not print!",
(int) (leftX), (int) (topY + 28 + i * 30));
rectList.remove(i);
}
}
ArrayList<Ellipse2D> ovalList1 = new ArrayList<Ellipse2D>();// creat and
// store the
// oval into
// an
// ArrayList.
Ellipse2D k;// creat a new rectangle2D object.
int numOfOval = o.numOfOval();// initial the number of rectangle.
for (int i = 0; i < numOfOval; ++i) {// a for loop to initial the
// rectangle and draw them.
o.initList();
o.calculateArea();
o.calculatePerimeter();
int ovalWeigth = o.OvalList.get(0);
int ovalLength = o.OvalList.get(1);
k = new Ellipse2D.Double();
k
.setFrame(leftX, (topY + 100 + i * 35), ovalWeigth,
ovalLength);
ovalList1.add(k);
g2.setPaint(Color.green);//set color.
g2.fill(ovalList1.get(i));
if (o.getArea() == 0)
g2.drawString("This is a Oval!", (int) leftX,
(int) (topY + 130 + i * 30));
else
g2.drawString("The area is:" + Math.round(o.getArea())
+ "square cm." + " The Perimeter is:"
+ Math.round(o.getPerimeter()) + "cm.",
(int) leftX, (int) (topY + 125 + i * 32));
}
g2.setPaint(Color.yellow);// print the result.
g2.drawString("I print " + rectList.size() + " Rectangle, and "
+ ovalList1.size() + " Oval.", (int) leftX,
(int) (topY + 250));
}// End method paintComponent.
}// End class DrawPanel.
}// End class PrintAndMeasureShapes.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -