class myline {  public void draw(Graphics g){
    g.drawLine(x, y, width, height);
  }}调用就是在你写的applet中的paint(Graphics g)中调用
myline.draw(g)不知道你想问的是不是这?

解决方案 »

  1.   

    //这是我编的一个Applet,或许对你有帮助,happy
    import java.awt.*;
    import javax.swing.*;
    public class drawpic extends JApplet{
    int choice;int a=50,b=50,c=180,d=180; public void init(){resize(1000,1000);
    String input;
    input=JOptionPane.showInputDialog("1-画线\n"+"2-画圆\n"+"3-画矩形\n"+"4-画椭圆\n");
    choice=Integer.parseInt(input);
    switch(choice){
    case 1: {inputLine();
    break;}
    case 2: {inputCircle();
    break;}
    case 3: {inputLine();
    break;}
    case 4: {inputOval();
    break;}
    default: JOptionPane.showMessageDialog(null,"输入无效","title",JOptionPane.PLAIN_MESSAGE);//System.exit(0);

    }//switch
    }//init
    public void paint(Graphics g){

    switch(choice){
    case 1: {g.setColor(Color.red);g.drawLine(a,b,c,d);break;}
    case 2: {g.setColor(Color.orange);g.drawOval(a,b,c,c);break;}
    case 3: {g.setColor(Color.cyan);g.drawRect(a,b,c,d);break;}
    case 4: {g.setColor(Color.blue);g.drawOval(a,b,c,d);break;}
    default: ;

    }//switch

    }//paint
    public void inputLine(){
    String input=JOptionPane.showInputDialog("请输入起点的X坐标");
    a=Integer.parseInt(input);
    input=JOptionPane.showInputDialog("请输入起点的Y坐标");
    b=Integer.parseInt(input);
    input=JOptionPane.showInputDialog("请输入终点的X坐标");
    c=Integer.parseInt(input);
    input=JOptionPane.showInputDialog("请输入终点的Y坐标");
    d=Integer.parseInt(input);
    }//Line
    public void inputCircle(){
    String inputa=JOptionPane.showInputDialog("请输入圆心的X坐标");
    a=Integer.parseInt(inputa);
    String inputb=JOptionPane.showInputDialog("请输入圆心的Y坐标");
    b=Integer.parseInt(inputb);
    String inputc=JOptionPane.showInputDialog("请输入圆的半径");
    c=Integer.parseInt(inputc);
    }//circle
    public void inputOval(){
    String inputa=JOptionPane.showInputDialog("请输入椭圆中心的X坐标");
    a=Integer.parseInt(inputa);
    String inputb=JOptionPane.showInputDialog("请输入椭圆中心的Y坐标");
    b=Integer.parseInt(inputb);
    String inputc=JOptionPane.showInputDialog("请输入椭圆的长轴");
    c=Integer.parseInt(inputc);
    String inputd=JOptionPane.showInputDialog("请输入椭圆的短轴");
    d=Integer.parseInt(inputd);
    }//Oval
    }//applet
      

  2.   

    先谢谢你们了,呵呵,看看我编的哪里出错了吧。
    public class myline extends Object
    { private double x1;
      private double y1;
      private double x2;
      private double y2;
      public myline()
      { setx1(0);
        sety1(0);
        setx2(0);
        sety2(0);
      }
      public myline(double x1,double y1,double x2,double y2)
      { setx1(x1);
        sety1(y1);
        setx2(x2);
        sety2(y2);
      }
      public void setx1(double x1)
      { this.x1=(x1>=0?x1:0);
      }
      public void sety1(double y1)
      { this.y1=(y1>=0?y1:0);
      }
      public void setx2(double x2)
      { this.x2=(x2>=0?x2:0);
      }
      public void sety2(double y2)
      { this.y2=(y2>=0?y2:0);
      }
      public double getx1(double x1)
      { return x1;
      }
      public double gety1(double y1)
      { return y1;
      }
      public double getx2(double x2)
      { return x2;
      }
      public double gety2(double y2)
      { return y2;
      }
      public void draw(Graphics g)
      { g.drawLine(x1,y1,x2,y2);
      }
    }
    编译出错,错误提示为:
    myline.java:42: cannot resolve symbol
    symbol  : class Graphics
    location: class myline
      public void draw(Graphics g)
                       ^
    1 error
    高手们给我看看吧,辛苦你们一下了啊,再谢谢先。
      

  3.   

    public class myline extends Object
                                ^^^^^^ 改为JApplet
    然后添加
    public void paint(Graphics g) {
      draw(g);
    }
      

  4.   

    g.drawLine(x1,y1,x2,y2); 这个方法只可以接受int类型的参数可以改为
    /////////////////////////////////////////////////////////////////////////////////
    import java.awt.*;
    public class myline extends Panel
    { private int x1;
      private int y1;
      private int x2;
      private int y2;
      public myline()
      { setx1(0);
        sety1(0);
        setx2(0);
        sety2(0);
        repaint();
      }
      public myline(int x1,int y1,int x2,int y2)
      { setx1(x1);
        sety1(y1);
        setx2(x2);
        sety2(y2);
        repaint();
      }
      public void setx1(int x1)
      { this.x1=(x1>=0?x1:0);
      }
      public void sety1(int y1)
      { this.y1=(y1>=0?y1:0);
      }
      public void setx2(int x2)
      { this.x2=(x2>=0?x2:0);
      }
      public void sety2(int y2)
      { this.y2=(y2>=0?y2:0);
      }
      public int getx1(int x1)
      { return x1;
      }
      public int gety1(int y1)
      { return y1;
      }
      public int getx2(int x2)
      { return x2;
      }
      public int gety2(int y2)
      { return y2;
      }
      public void paint(Graphics g)
      { g.drawLine(x1,y1,x2,y2);
       System.out.print("fdsa");
      }
      public static void main(String args[])
      {
       Frame f=new Frame();
      
       myline my=new myline (10,10,100,100);
       f.add(my);
       f.setSize(200,200);
       f.setVisible(true);
      }
    }
      

  5.   

    各位可能题目理解错了,怪我没说清楚,我把他完整的抄下来,大家再次辛苦一下。
    题目:创建一个画图的applet,能随机的画直线、矩形和椭圆。为此,创建几个“聪明”的图形类:这些类的对象知道如何画它自己,只要提供了一个Graphics对象告诉他们在哪里画(即applet的Graphics类的数据对象允许在applet的背景上画一个图形)。这些类的名字应该为myline,myrect和myoval。myline类的数据应该包括坐标x1,y1和x2,y2,Graphics类的drawLine方法将用一条直线连接给定的两点。myrect和myoval类的数据应该包括左上角x和y的坐标值,宽度和高度(必须非负)。每个类中的所有数据必须为private。除了这些数据,每个类至少应该定义如下的public方法:
    1、一个无参数的构造函数,将所有坐标值置为0。
    2、一个带参数的构造函数,将坐标置为所提供的值。
    3、为每个数据提供一个设置值的方法,是编程人员可以独立的设置一个图形的任何数据值(如:如果你有一个实例变量x1,你应有一个方法setx1)。
    4、为每个数据提供一个读取方法,使编程人员可以独立的读取一个图形中的任何数据(如:如果你有一个实例变量x1,你应有一个getx1方法)。
    5、一个draw方法,其定义的首行应为:
    public void draw(Graphics g)
    applet的paint方法将调用它在屏幕上画出一个图形。
    前面的方法是必须的,那你还可以提供更多灵活的方法。
    首先,定义myline类和一个检测你的类的applet。这个applet应该有一个myline实例变量line,它指向一个myline对象(由applet的init方法以随机坐标创建)。applet的paint方法应该用如下的语句画出图形:
    line.draw(g);
    其中,line是myline引用,而g是Graphics对象,图形将用该对象在applet上画自己。然后,将单个myline引用改为myline引用数组,并再将几个myline对象加到程序中来画图。applet的paint方法应该遍历myline对象数组,并画出每个图形。
    上述部分完成之后,你应该定义myoval和myrect类,并把这些类的对象加到myrect和myoval数组中。applet的paint方法应遍历每个数组,并画出每个图形。为每种类型创建5个图形。
    当applet运行时,从appletviewer的Applet菜单中选择重新载入applet,这将会使applet为图形选择新的随机数再次画出图形。
      

  6.   

    import javax.swing.JApplet;
    import java.awt.*;public class DrawShape
        extends JApplet {
      int width, height;
      MyLine line = new MyLine(100, 100, 150, 150);
      MyOval oval = new MyOval(50, 50, 100, 100);
      MyRect rect = new MyRect(75, 75, 150, 150);  public DrawShape() {
        width = getWidth();
        height = getHeight();
      }  public void paint(Graphics g) {
        line.draw(g);
        oval.draw(g);
        rect.draw(g);
      }}abstract class Shape {
      protected int x1, x2, y1, y2;  Shape() {
        x1 = 0;
        x2 = 0;
        y1 = 0;
        y2 = 0;
      }  Shape(int x1, int y1, int x2, int y2) {
        this.x1 = x1;
        this.x2 = x2;
        this.y1 = y1;
        this.y2 = y2;
      }  public void setX1(int x1) {
        this.x1 = x1;
      }  public void setX2(int x2) {
        this.x2 = x2;
      }  public void setY1(int y1) {
        this.y1 = y1;
      }  public void setY2(int x1) {
        this.y1 = y2;
      }}class MyLine
        extends Shape {  MyLine() {
        super();
      }  MyLine(int x1, int y1, int x2, int y2) {
        super(x1, y1, x2, y2);
      }  public void draw(Graphics g) {
        g.drawLine(x1, y1, x2, y2);
      }}class MyOval
        extends Shape {
      MyOval() {
        super();
      }  MyOval(int x1, int y1, int x2, int y2) {
        super(x1, y1, x2, y2);
      }  public void draw(Graphics g) {
        g.drawOval(x1, y1, x2, y2);
      }
    }class MyRect
        extends Shape {
      MyRect() {
        super();
      }  MyRect(int x1, int y1, int x2, int y2) {
        super(x1, y1, x2, y2);
      }  public void draw(Graphics g) {
        g.drawRect(x1, y1, x2, y2);
      }
    }============================================================
    编译后,建一个网页,将applet放入到网页中如果你再完成不了,我也帮不了你了。
      

  7.   

    我的程序是:
    请问我的错在哪里?下面是myline类,我不想采取你的这种包访问的方式编写,我的思路是分开写myline类,myrect类和myoval类,然后搞一个applet调用,这样的话我觉得程序清晰些,也方便修改些。再辛苦你一下啊,呵呵。
    public class myline
    { private int x1;
      private int  y1;
      private int  x2;
      private int  y2;
      public myline()
      { setx1(0);
        sety1(0);
        setx2(0);
        sety2(0);
      }
      public myline(int  x1,int  y1,int  x2,int  y2)
      { setx1(x1);
        sety1(y1);
        setx2(x2);
        sety2(y2);
      }
      public void setx1(int  x1)
      { this.x1=x1;
      }
      public void sety1(int  y1)
      { this.y1=y1;
      }
      public void setx2(int  x2)
      { this.x2=x2;
      }
      public void sety2(int  y2)
      { this.y2=y2;
      }
      public int  getx1(int  x1)
      { return x1;
      }
      public int  gety1(int  y1)
      { return y1;
      }
      public int  getx2(int  x2)
      { return x2;
      }
      public int  gety2(int  y2)
      { return y2;
      }
      public void draw(Graphics g)
      {
        g.drawLine(x1, y1, x2, y2);
      }
    }
    但是错误提示为:
    myline.java:42: cannot resolve symbol
    symbol  : class Graphics
    location: class myline
      public void draw(Graphics g)
                       ^
      

  8.   

    Graphics就是这个编译器不认识。
      

  9.   

    Graphics就是这个编译器不认识。