Pfeffer 2017-11-04 23:27 采纳率: 50%
浏览 1161
已采纳

在paint之前定义变量,然后用drawline,就是画不出来图像,如何才能往paint里引入变量

 import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
//import java.awt.Panel;




class WindowsDemo extends Frame
{
    int size=5;
     int gridLen=30;
     int c = 10;
     int d = 30;


    public WindowsDemo(String title)
    {
    super(title);
    }

    public void init() {
        super.setSize(1000,1000);
        super.setBackground(Color.GREEN);
        super.setBounds(100,100,300,300);
        super.setLayout(null);
        super.setVisible(true);




    }




    public void paint(Graphics g) {
        //super.paint(g);
        //g.setColor(Color.BLACK);

        int b = 10;
         //g.drawLine(40+b*gridLen, 40, 40+b*gridLen, 40+b*gridLen); 


        //System.out.println(size);
        //System.out.println(c);
        //System.out.println(d);
           g.drawLine(c, c, d, d); 
           g.drawLine(40, 40+b*gridLen, 40+size*gridLen, 40+b*gridLen); 
          for(int i=0;i<=size;i++){ 
           g.drawLine(40+i*gridLen, 40, 40+i*gridLen, 40+size*gridLen); 
           g.drawLine(40, 40+i*gridLen, 40+size*gridLen, 40+i*gridLen); 
          } 





}

}

class punct
{
private int x,y,x1,y1,x2,y2;  

punct()
    {
    }



punct (int x , int y)
    {
        this.x =x;
        this.y =y;

    }




    void print(int x, int y)
    {
        System.out.println();
        System.out.println("die Koordinate ist (" +x+","+y+ ")");

    }




    int[] setLocatio(int x0, int y0)
    {
        x1 = x0;
        y1 = y0;


    String a = this.toString(x1 ,y1 );
    System.out.print("Die Koordinate ist (");
    for( int i=0;i<a.length();i++ )
    {
        System.out.print( a.charAt(i) );
    }


    System.out.print(")");  

    int[] arr1 = {x1,x2};

    return arr1;

    }
    // String toString

    public String toString(int A, int B)
    {

        String a = String.valueOf(A);
        String b = String.valueOf(B);
        String s = a+","+b;


        return s;
    }

    // void translate(int dx, int dy)
    void translate(int dx, int dy)
    {
       x2=x1 + dx;
       y2=y1 + dy;
       this.print(this.x2, this.y2);



    }



}



public class DrawWindows {

    public static void main(String[] args) {
        // TODO Auto-generated method stu
        punct p = new punct();



        p.setLocatio(100,100); 
        p.translate(100,100);
        new WindowsDemo("WindowsDemo").init();

    }

}

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-11-05 01:31
    关注

    class WindowsDemo extends Frame
    在这个里面加上
    public punct p;
    main里面
    punct p = new punct();
    p.setLocatio(100,100);
    p.translate(100,100);
    WindowsDemo wd = new WindowsDemo("WindowsDemo");
    wd.p = p;
    wd.init();

    paint里面通过p获得参数

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题