⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 displayshapes.java

📁 一个大学本科java课程的大作业。要求利用GUI以及APPLET分别完成一个任务:随机产生三角形
💻 JAVA
字号:

/**
 *Title:DisplayShapes.java
 *Descriprion:This applet class prints out rectangles and triangles,
 *and also outputs their area and perimeter.
 *@author: Peng yu
 */



import javax.swing.*;
import java.awt.*;

public class DisplayShapes extends JApplet 
{
	
	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 window as well as some 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.setSize(400, 400);  //draw a window of 400 by 400 and a default white color
	
	}





	/**
	 * This method paint the shapes out.It calls two methods
	 * @param g is an object of graphics
	 */
	public void paint(Graphics g) 
	{	
	
                init();                    //call the init to reset(in case minimize the window)
    	        g.setColor(Color.white);   //repaint the background

		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 + -