📄 drawingpanel.java
字号:
/**
*Title:DrawingPanel.java
*Descriprion:This class is a subclass of JPanel.
*It is modified by the class DisplayShapes and
*also prints out the shapes as well as their area and perimeter.
*@author: Peng yu
*/
import javax.swing.*;
import java.awt.*;
public class DrawingPanel extends JPanel
{
public static int numberOfShape; //the total number of drawn shapes
public static int countRect; //the number of generated rectangles
public static int countTri; //the number of generated triangles
public static int xPoint;
public static int yPoint; //the coordinary
/**
* This method just initialises the parameters
* and determines the number of rectangles and triangles by random
*/
public void init()
{
numberOfShape=0;
countRect = (int) (1 + Math.random() * 3);
countTri = (int) (1 + Math.random() * 3);
xPoint=15;
yPoint=15;
}
/**
* This method paint the shapes out.It calls three methods
* @param g is an object of graphics
*/
public void paintComponent(Graphics g)
{
init(); //call the init to reset the panel
g.setColor(Color.white); //repaint the background of the frame
for (int i = 1; i <= countRect; i++) //generate "countRect" rectangles
{
try
{
Rectangle rect = new Rectangle();
paintRect(g,rect); //call the paintRect method to paint rectangle
numberOfShape++;
yPoint =yPoint+rect.getHeight()+30;
} catch (IllegalRectangleException e) {
System.err.println(e.getMessage());
//Print the message of the IllegalRectangleException
}
}
for (int i = 1; i <= countTri; i++) //generate "countTri" triangles
{
try
{
Triangle tri = new Triangle();
paintTri(g,tri); //Call the paintTri method to paint triangles
numberOfShape++;
yPoint=yPoint+tri.getHeight()+30;
} catch (IllegalTriangleException e) {
System.err.println(e.getMessage());
//Print the message of the IllegalTriangleException
}
}
paintOutcomes(g); //Call the paintOutcomes method to print the outcomes
}
/**
* This method paint the rectangles
* and output the relative parameters such as area as well as perimeter
* @param g the object of graphics
* @param rect the object of rectangle
*/
public static void paintRect(Graphics g,Rectangle rect)
{
g.setColor(Color.blue);
g.fillRect(xPoint, yPoint, rect.getWidth(), rect.getHeight());
g.setFont(new Font("TimesRoman", Font.PLAIN, 12)); //set the font
g.setColor(Color.black);
g.drawString(String.format("Width= %d centimeters Height= %d centimeters",
rect.getWidth(),rect.getHeight()),
xPoint+rect.getWidth()+20, yPoint+rect.getHeight()/2);
g.drawString(String.format("Area= %,4.2f square centimeters. Perimeter= %,4.2f centimeters.",
rect.calcShapeArea(), rect.calcShapePerimeter()),
xPoint, yPoint+rect.getHeight()+10);
}
/**
* This method paint the triangles
* and output the relative parameters such as area as well as perimeter
* @param g the object of graphics
* @param tri the object of triangle
*/
public static void paintTri(Graphics g,Triangle tri)
{
g.setColor(Color.red);
int[] a = { xPoint + tri.getBase() / 2, xPoint, xPoint + tri.getBase() };
int[] b = { yPoint, yPoint+tri.getHeight(), yPoint + tri.getHeight() };
g.fillPolygon(a, b, 3); //print the triangle
g.setColor(Color.black);
g.drawString(String.format("Base=%d Height=%d",
tri.getBase(),tri.getHeight()),xPoint+tri.getBase(), yPoint+tri.getHeight()/2);
g.drawString(String.format("Area= %,4.2f square centimeters Perimeter= %,4.2f centimeters",
tri.calcShapeArea(), tri.calcShapePerimeter()),xPoint, yPoint+tri.getHeight()+10);
}
/**
* This method print out the outcomes such as total number of drawn shapes
* @param g the object of graphics
*/
public static void paintOutcomes(Graphics g)
{
g.setColor(Color.black);
g.drawString( String.format("The number of rectangles generated (contains illegal ones) is: %d",countRect),xPoint,yPoint+20);
g.drawString( String.format("The number of triangles generated (contains illegal ones) is: %d",countTri),xPoint,yPoint+35);
//print out the number of shapes generated (contains illegal ones)
g.setFont(new Font("TimesRoman", Font.BOLD, 12));
if (numberOfShape != 0)
{
g.drawString("The total number of drawn shapes is "+numberOfShape, xPoint, yPoint + 50);
}
else
{
g.drawString("No valid shapes were generated. Please try again!",xPoint, yPoint + 50);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -