定义一个Button 在actionPerformed中获取ee.getSource
在paint(Graphics g) 中判断再设置颜色并画。

解决方案 »

  1.   

    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;public class Drawing_Ovel extends Applet implements ActionListener
    {
    Button br=new Button("红"),bg=new Button("绿"),bb=new Button("蓝");
    Canvas c=new Canvas();
    public void init() 
    {
    this.setLayout(null);
    this.setSize(400,400);
    this.setBackground(Color.black);
    br.setSize(35,20);br.setLocation(10,10);
    bg.setSize(35,20);bg.setLocation(50,10);
    bb.setSize(35,20);bb.setLocation(100,10);
    c.setSize(300,300);
    c.setBackground(Color.yellow);
    add(br);add(bg);add(bb);
    add(c);
    br.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ee)
    {
    c.setBackground(Color.red);
    repaint();
    }
    });
    bg.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ee)
    {
    c.setBackground(Color.green);
    repaint();
    }
    });
    bb.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ee)
    {
    c.setBackground(Color.blue);
    repaint();
    }
    });
    }
    public void actionPerformed(ActionEvent ee)
    {
    repaint();
    }
    }
      

  2.   

    if((Button)ee.getSource()==br)gg.setColor(Color.red);
    else
    if((Button)ee.getSource()==bg)gg.setColor(Color.green);
    else
    if((Button)ee.getSource()==bb)gg.setColor(Color.blue);
    repaint();
      

  3.   

    gg.fillOval(50,50,50,50);
    换成一个fill(圆的引用好了)
      

  4.   

    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;public class Drawing_Ovel extends Applet implements ActionListener
    {
    Button br=new Button("红"),bg=new Button("绿"),bb=new Button("蓝");
    Canvas c=new Canvas();
             int GraphicsModel;///////////////////////////////////////////////////////////
    public void init() 
    {
    this.setLayout(null);
    this.setSize(400,400);
    this.setBackground(Color.black);
    br.setSize(35,20);br.setLocation(10,10);
    bg.setSize(35,20);bg.setLocation(50,10);
    bb.setSize(35,20);bb.setLocation(100,10);
    c.setSize(300,300);
    c.setBackground(Color.yellow);
    add(br);add(bg);add(bb);
    add(c);
    br.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ee)
    {
                                                  GraphicsModel=0;
    }
    });
    bg.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ee)
    {
                                                  GraphicsModel=1;
    }
    });
    bb.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ee)
    {
                                                  GraphicsModel=2;
    }
    });
    }
    public void paint(Graphics g)
    {
    if(GraphicsModel==0)
                       {         g.fill()};/////////////////////////////////////////////
    if(GraphicsModel==1)
                       {         g.fill()};////////////////////////////////////////////
    if(GraphicsModel==2)
                       {         g.fill()};///////////////////////////////////////////////
    }
      

  5.   

    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;public class Drawing_Ovel extends Applet implements ActionListener
    {
    Button br=new Button("红"),bg=new Button("绿"),bb=new Button("蓝");
    Canvas c=new MyCanvas();
    Graphics g=c.getGraphics();
    public void init() 
    {
    this.setLayout(null);
    this.setSize(400,400);
    this.setBackground(Color.black);
    br.setSize(35,20);br.setLocation(10,10);
    bg.setSize(35,20);bg.setLocation(50,10);
    bb.setSize(35,20);bb.setLocation(100,10);
    c.setSize(300,300);
    c.setBackground(Color.yellow);
    add(br);add(bg);add(bb);
    add(c);
    br.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ee)
    {
    Graphics g=c.getGraphics();
    g.setColor(Color.red);
    g.fillOval(50,50,50,50);
    repaint();
    }
    });
    bg.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ee)
    {
    Graphics g=c.getGraphics();
    g.setColor(Color.green);
    g.fillOval(50,50,50,50);
    repaint();
    }
    });
    bb.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ee)
    {
    Graphics g=c.getGraphics();
    g.setColor(Color.blue);
    g.fillOval(50,50,50,50);
    repaint();
    }
    });
    }
    public void actionPerformed(ActionEvent ee)
    {
    repaint();
    }
    }
    class MyCanvas extends Canvas
    { public void paint(Graphics gg)
    {
    gg.setColor(Color.red);
    gg.fillOval(50,50,50,50);
    }
    }
      

  6.   

    不可能啊!
    <HTML>
    <applet code="Drawing_Ovel.class" width=400 height=200>
    </applet>
    </HTML>不要用IE,可能plug-in有问题.用appletviewer ?.html
      

  7.   

    我是用JCreator 建立的工程 运行的啊 
    你也运行一下看看可以不?
      

  8.   

    帮你改了改,看这个行不
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;public class Cir extends Applet implements ActionListener
    {
    Button br=new Button("红");
    Button bg=new Button("绿");
    Button bb=new Button("蓝");
    int color=0; public void init() 
    {
    this.setLayout(null);
    this.setSize(400,400);
    this.setBackground(Color.black);
    br.setSize(35,20);br.setLocation(10,10);
    bg.setSize(35,20);bg.setLocation(50,10);
    bb.setSize(35,20);bb.setLocation(100,10);
    add(br);add(bg);add(bb);
    br.addActionListener(this);
    bg.addActionListener(this);
    bb.addActionListener(this);
    } public void actionPerformed(ActionEvent ee)
    {
    if(ee.getSource()==br)color=1;
    else
    if(ee.getSource()==bg)color=2;
    else
    if(ee.getSource()==bb)color=3;
    repaint(); } public void paint(Graphics g) 
    {
    switch (color)
    {
    case 1: g.setColor(Color.red);break;
    case 2: g.setColor(Color.green);break;
    case 3: g.setColor(Color.blue);break;
    default: g.setColor(Color.black);
    }
    g.fillOval(50,50,50,50);
    }

    }
      

  9.   

    两个问题:
    1、要获取canvas的graphics
    2、applet没有paint()方法,只有paint(Graphics g)
    两者不一样,建议区分使用,否则会画错地方的。
    楼主的程序相当于是自定义一个paint方法,所以要显示调用才行。import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;public class Drawing_Ovel extends Applet implements ActionListener
    {
    Graphics gg;
    Button br=new Button("&ordm;ì"),bg=new Button("&Acirc;&Igrave;"),bb=new Button("&Agrave;&para;");
    Canvas c=new Canvas();
    public void init() 
    {
    this.setLayout(null);
    this.setSize(400,400);
    this.setBackground(Color.black);
    br.setSize(35,20);br.setLocation(10,10);
    bg.setSize(35,20);bg.setLocation(50,10);
    bb.setSize(35,20);bb.setLocation(100,10);
    c.setSize(300,300);
    c.setBackground(Color.yellow);
    add(br);add(bg);add(bb);
    add(c);
    br.addActionListener(this);
    bg.addActionListener(this);
    bb.addActionListener(this);
    }
    public void actionPerformed(ActionEvent ee)
    {
    gg=c.getGraphics();
    if(ee.getSource()==br)gg.setColor(Color.red);
    else
    if(ee.getSource()==bg)gg.setColor(Color.green);
    else
    if(ee.getSource()==bb)gg.setColor(Color.blue);
    paint();}public void paint() 
    {
    gg.fillOval(50,50,50,50);
    }
    }
      

  10.   

    恩 楼上的2位改的可以用 谢谢
    可我还有个程序有问题就
    可以在帮我看看吗 
    这个程序可以运行 但按按钮没反映 也没有debug显示 可以看看那里有问题吗?
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;public class Drawing_Ovel extends Applet implements ActionListener
    {
    Button br=new Button("红"),bg=new Button("绿"),bb=new Button("蓝");
    Canvas c=new Canvas();
             int y=0;
    public void init() 
    {
    this.setLayout(null);
    this.setSize(400,400);
    this.setBackground(Color.black);
    br.setSize(35,20);br.setLocation(10,10);
    bg.setSize(35,20);bg.setLocation(50,10);
    bb.setSize(35,20);bb.setLocation(100,10);
    c.setSize(300,300);
    c.setBackground(Color.white);
    add(br);add(bg);add(bb);
    add(c);
    br.addActionListener(this);
    bg.addActionListener(this);
    bb.addActionListener(this);

    }
    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource()==br)y=0;
    if(e.getSource()==bg)y=1;
    if(e.getSource()==bb)y=2;
    }

    public void paint(Graphics g)
    {
    if(y==0)
                       {   g.setColor(Color.red);
                             g.fillOval(50,50,50,50);}
    if(y==1)
                       {  g.setColor(Color.green);    
                        g.fillOval(50,50,50,50);}
    if(y==2)
                       {g.setColor(Color.blue);         
                        g.fillOval(50,50,50,50);}
    }
    }
      

  11.   

    还是2个问题:
    1、现在用的是applet的grahpics,所以是在applet上画图,而canvas是加在applet上的,所以会覆盖applet所画的图像,所以不要加canvas(即将add(c)去掉)2、每次改变需要用repaint刷新一下
    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource()==br)y=0;
    if(e.getSource()==bg)y=1;
    if(e.getSource()==bb)y=2;                  repaint();  ////////////////////
    }