编写一个Applet程序:画图
要求:界面上有2个按钮,一个名称为“椭圆”,另一个为“矩形”,点击后分别画蓝色的椭圆和红色的矩形,图形大小和位置自己确定。运行环境:JCreator
速度 在线等啊~~!!

解决方案 »

  1.   

    定义个描述矩形的类Rectangle,类中有两个整形的成员变量,length,width分别表示矩形的长和宽。定义构造函数,初始化这两个变量;定义方法求矩形的周长,并返回整形结果;定义方法求矩形的面积,并返回整形结果;定义方法把矩形的长、宽,以及周长和面积转化为字符串并返回字符串。
    编写应用程序,测试Rectangle类,使用类中的各个方法,并将结果输出。
    快点啊~~~
    马上就要结束考试了
    我帮一朋友问的~~~
    各位大虾帮忙了
    分不够了还可以再给
      

  2.   

    真后悔走开了,还要啊????!!!!
    哈哈,接点分吧
     import java.applet.Applet; 
    import java.awt.*; 
    import java.awt.event.*; 
    import java.awt.geom.*; 
    public class Test extends Applet implements ActionListener 

    private Button bt1=new Button("椭圆"), 
    bt2=new Button("矩形"); public void init() 

    add(bt1); 
    add(bt2); 
     
    bt1.addActionListener(this); 
    bt2.addActionListener(this); 

    public void actionPerformed(ActionEvent e) 

    if(e.getSource()==bt1)  picture1(); 
    else if(e.getSource()==bt2)  picture2();  repaint(); 

     
    boolean choose; 
     
    public void picture1() 

    choose=true; 

    public void picture2() 

    choose=false; 

    public void paint(Graphics g) 

    Graphics2D g2=(Graphics2D)g; 
    int x=90,y=50,n,m,i; 
    double d; 
    if(choose) 

    g2.setColor(Color.blue); 
     g2.drawOval(x,y,50,30); } 
    else 

    g2.setColor(Color.red); g2.drawRect(x,y,35,30);  


    }
      

  3.   

    第二个:
    import java.util.*;
    class Rectangle
    {
    int length,width;
        Rectangle()
    {}
    Rectangle(int a,int b)
    {
    length=a;
    width=b;
    }
    public int round()
    {
    int round=(int)(length+width)*2;

    return round; }
    public int  mianji()
    {
    int mianji=length*width;

    return mianji;
    }
    public String tostring(int i)
    {
    String str;
    str=String.valueOf(i);
       
    return str;
    }}
    public class Test
    {
    public static void main(String args[])
    {
    Rectangle r1=new Rectangle(15,6);
    System.out.println("周长为: "+r1.round());
    System.out.println("面积是:"+r1.mianji());
    System.out.println ("周长转换成字符串:"+r1.tostring(r1.round()));
            System.out.println ("面积转换成字符串:"+r1.tostring(r1.mianji()));
    }
    }
      

  4.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;public class PainterPanel extends JPanel implements MouseListener{
    int shape=-1; //图案类型
    Point[] point=new Point[2]; //记录鼠标拖动的起始点和终点 public PainterPanel(){
    super(); //调用父类构造函数
    this.setBackground(Color.white); //设置背景颜色
       point[0]=new Point(-1,-1); //初始化变量
       point[1]=new Point(-1,-1);
    addMouseListener(this); //增加鼠标事件
    }
    public void mouseReleased(MouseEvent e){ //鼠标释放事件
    point[1]=new Point(e.getX(),e.getY());  //设置终点位置
    repaint(); //重绘屏幕
    }
    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
    public void mouseClicked(MouseEvent e){}
    public void mousePressed(MouseEvent e){  //鼠标按下时事件
    point[0]=new Point(e.getX(),e.getY());  //设置起始点位置
    }   
    public void paint(Graphics g){
    super.paint(g);
    switch (shape){  //根据shape值绘制图形
    case 0:
    g.drawLine(point[0].x,point[0].y,point[1].x,point[1].y); //绘线
    break;
    case 1:
    int width=point[1].x-point[0].x;
    int height=point[1].y-point[0].y;
    g.drawOval(point[0].x,point[0].y,width,height); //绘椭圆
    break;
    case 2:
    width=point[1].x-point[0].x;
    height=point[1].y-point[0].y;
    g.drawRect(point[0].x,point[0].y,width,height);  //绘矩形
    break;
    }
    }

    public void drawShape(int shape){
    this.shape=shape;
    }
    }
    -----------------------
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;public class PainterDemo extends JFrame{

    JToggleButton[] button=new JToggleButton[3];  //按钮组
    PainterPanel painter=new PainterPanel(); //绘图面板

    public PainterDemo(){
    super("Java画图程序"); //调用父类构造函数

    String[] buttonName={"直线","椭圆","矩形"}; //按钮文字
    DrawShapeListener buttonListener=new DrawShapeListener(); //按钮事件 JToolBar toolBar=new JToolBar(); //实例化工具栏
    ButtonGroup buttonGroup=new ButtonGroup(); //实例化按钮组
    for (int i=0;i<button.length;i++){
    button[i]=new JToggleButton(buttonName[i]); //实例化按钮
    button[i].addActionListener(buttonListener); //增加按钮事件处理
    buttonGroup.add(button[i]); //增加按钮到按钮组
    toolBar.add(button[i]);  //增加按钮到工具栏
    } Container container=getContentPane(); //得到窗口容器
    container.add(toolBar,BorderLayout.NORTH); //增加组件到容器上
    container.add(painter,BorderLayout.CENTER);

    setSize(300,200);  //设置窗口尺寸
    setVisible(true);  //设置窗口为可视
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //关闭窗口时退出程序
    }

    class DrawShapeListener implements ActionListener{  //按钮事件处理
    public void actionPerformed(ActionEvent e){
    for (int i=0;i<button.length;i++){ 
    if (e.getSource()==button[i]){  //判断来自于哪个按钮
    painter.drawShape(i); //绘制图形
    }
    }
    }
    }

    public static void main(String[] args){
    new PainterDemo();
    }
    }