日期:2014-05-20 浏览次数:20743 次
public class ContentPanel extends JPanel{ private static final long serialVersionUID = 1L; // private MyLine line; // private MyOval oval; // private MyRectangle rect; private int shapeNumber; private MyShape[] shape; public int numLine,numOval,numRect; private int i; public String strNum; //定义随机数组,该数组格式为{randomX1,randomY1,randomX2,randomY2,randomColor,randomFill,randomShape} public int[] randomAttr; public boolean fillStatus; public ContentPanel(int number){ shapeNumber=number; shape=new MyShape[shapeNumber]; } protected void paintComponent(Graphics g) { numLine=numOval=numRect=0; super.paintComponent(g); //g.drawString("yuzhengzhong", 10,570); for(i=0;i<shapeNumber;i++){ randomAttr=GetRandom.getRandom(); if(randomAttr[5]==0) fillStatus=false; else if(randomAttr[5]==1) fillStatus=true; switch(randomAttr[6]){ case Global.Line: shape[i]=new MyLine(randomAttr[0],randomAttr[1],randomAttr[2],randomAttr[3],randomAttr[4]); shape[i].draw(g); numLine++; break; case Global.OVAL: shape[i]=new MyOval(randomAttr[0],randomAttr[1],randomAttr[2],randomAttr[3],randomAttr[4],fillStatus); shape[i].draw(g); numOval++; break; case Global.RECTANGLE: shape[i]=new MyRectangle(randomAttr[0],randomAttr[1],randomAttr[2],randomAttr[3],randomAttr[4],fillStatus); shape[i].draw(g); numRect++; break; } } strNum="Lines:"+numLine+" Ovals:"+numOval+" Rectangles:"+numRect; g.drawString(strNum, 10,570); } public String retShapeNum(){ System.out.println(strNum); return strNum; } }
public class Main { public static void main(String[] args){ JFrame frame=new JFrame("yuzhengzhong 实验四!"); frame.setSize(600+10,600+35); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); String str=JOptionPane.showInputDialog("Enter the number of the shapes:"); int number=Integer.parseInt(str); //System.out.println(number); ContentPanel panel=new ContentPanel(number); frame.add(panel); String shapeNumbers=panel.retShapeNum(); System.out.print(shapeNumbers); JLabel label=new JLabel(shapeNumbers); frame.add(label,"South"); frame.setVisible(true); } }