Pfeffer 2017-11-06 21:21 采纳率: 50%
浏览 869
已结题

关于成员变量在paint里面使用的问题

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

punct()
    {

    System.out.println("("+x+","+y+")");
    }



punct (int x , int y)
    {
        x1 =x;
        y1 =y;
    System.out.println("Die Stelle von dem Anfangspunkt:"+"("+x1+","+ y1+")");

    }

punct(punct p)
{
    p.x = 10;
    p.y = 20;
System.out.println("Mit diesem Konstruktor kann der Punkt p3 auch installiert werden");
System.out.println("("+p.x+","+p.y+")");
}

public int getX() {
return x;
}



public void setX(int x) {
this.x = x;
}

public int getY() {
return y;
}



public void setY(int y) {
this.y = y;
}



    public int getX1() {
    return x1;
}



public void setX1(int x1) {
    this.x1 = x1;
}



public int getY1() {
    return y1;
}



public void setY1(int y1) {
    this.y1 = y1;
}



public int getX2() {
    return x2;
}



public void setX2(int x2) {
    this.x2 = x2;
}



public int getY2() {
    return y2;
}



public void setY2(int y2) {
    this.y2 = y2;
}



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

    }




    void setLocatio(int x, int y)
    {
        x1 =x;

        y1 =y;



    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(")");  



    }
    // 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(x2, y2);
    }

    void Xtranslate(int dx)
    {
       x2=x1 + dx;
       y2=y1;
       this.print(x2, y2);
    }

    void Ytranslate(int dy)
    {
       x2=x1;
       y2=y1 + dy;
       this.print(x2, y2);
    }


    static void Abstand(double a, double b, double c, double d)
    {
        //System.out.println("Punkt a,  den Abstand eines Punktes vom Koordinatenursprung:"+Math.sqrt(Math.pow(p.getX1(), 2)+Math.pow(p.getX2(), 2)));
        System.out.println("der Abstand zwischen zwei Punkten:"+Math.sqrt(Math.pow(a-c, 2)+Math.pow(b-d,2)));   
    }

}



public class Punkt {
/*
    public static void main(String[] args) {
        // TODO Auto-generated method stu
        punct p1 = new punct(10,20);
        punct p2 = new punct(10,30);

        if((p1.getX1()==p2.getX1())&&(p1.getY1()==p2.getY2()))
        {
        System.out.println("p1 ist ungleich zu p2");
        }
        else
        {
            System.out.println("p1 ist gleich zu p2");
        }
        //-----------------------------------------------------------------------------------------
        punct p3 = new punct();
        System.out.println("der Punkt p3 ist momentan nicht definiert");
        new punct(p3);
        //-----------------------------------------------------------------------------------------
        punct p = new punct();
        /*constructor*/


/*  
        p.setX(50);
        p.setY(100);

        System.out.print("vor dem Verschieben: ");
        p.setLocatio(50,150); 

        System.out.println();
        System.out.print("nach dem Verschieben: ");
        p.translate(20,100);
        if((p.getX1()==p.getX2())&&(p.getY1()==p.getY2()))
        {
        System.out.println("p vor dem Verschieben ist ungleich zu p nach dem Verschieben");
        }
        else
        {
            System.out.println("p vor dem Verschieben ist ungleich zu p nach dem Verschieben");
        }
        //------------------------------------------------------------------------------------------------------
        System.out.println("die Koordinate von x-Verschieben"+"("+p.getX1()+","+p.getY1()+")");
        System.out.print("die Koordinate name x-Verschieben:"  );
        p.Xtranslate(100);
        System.out.println("");
        System.out.print("die Koordinate name Y-Verschieben:"  );
        p.Ytranslate(100);
//------------------------------------------------------------------------------------------

        System.out.println("Punkt a, die Koordinate von x-Verschieben"+"("+p.getX1()+","+p.getY1()+")");
        System.out.println("Punkt b,  Koordinate von x-Verschieben"+"("+p.getX2()+","+p.getY2()+")");
        System.out.println("Der Abstand zwischen punkt a und Koodiantensprung:");
        punct.Abstand(p.getX1(),p.getY1(),0,0);
        System.out.println("Der Abstand zwischen punkt a und punkt b:");
        punct.Abstand(p.getX1(),p.getY1(),p.getX2(),p.getY2());
//-------------------------------------------------------------------------------------------
        punct[] Array = new punct[5];
        for(int i = 0; i<5; i++)
        {
            double a = 100*Math.random();
            int b = (int) a;
            double c = 100*Math.random();
            int d = (int) a;


            Array[i] =  new punct(b,d);
        }






    }
    */

}

 import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.BasicStroke;

//import java.awt.Panel;




class Farbepunkt extends Frame
{



   static  int r1 ;

   int[] arr = new int[6];
  /*  int g1 ;
    int b1 ;
    int r2 ;
    int g2 ;
    int b2 ;*/


      Color  c1 = new Color(0,0,0);
     Color  c2= new Color(0,0,0);


    public punct p;




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

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




    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setStroke(new BasicStroke(3f)); 

    g.setColor(Color.red);

    //------------------
    c1 = g.getColor();
    arr[0] = c1.getRed();
    arr[1] = c1.getGreen();
    arr[2]= c1.getBlue();
    System.out.println(arr[0]);
    System.out.println(arr[1]);
    System.out.println(arr[2]);

   //--------------------------------------

    g.drawLine(p.getX1(),p.getY1()-3, p.getX1()-3, p.getY1()-3); 
    String A = "("+p.getX1()+","+ p.getY1()+")" ;
    g.drawString(A, p.getX1()+10, p.getY1()+10);





        g.setColor(Color.blue);
    String B = "("+p.getX2()+","+ p.getY2()+")" ;
    g.drawString(B, p.getX2()+20, p.getY2()+20);
    g.drawLine(p.getX2()+2,p.getY2()+2, p.getX2()+2, p.getY2()+2); 
    /*
       c2 = g.getColor();
         this.r2 = c2.getRed();
         this.g2 = c2.getGreen();
         this.b2 = c2.getBlue();
        System.out.println(r2);
        System.out.println(g2);
        System.out.println(b2);*/

    g.setColor(Color.white);
    g.drawLine(p.getX2(),p.getY2(), p.getX1(), p.getY1()); 


}


    public String toString()
    {
        System.out.println(arr[0]+"ha");
        System.out.println(arr[1]+"ha");
        System.out.println(arr[2]+"ha");
        /*System.out.println(this.r2+"ha");
        System.out.println(this.g2+"ha");
        System.out.println(this.b2+"ha");*/

    return this.r1+"";
    }

}




public class TestFarbpubkt {

    public static void main(String[] args) {

        // TODO Auto-generated method stub
         Farbepunkt Fp = new  Farbepunkt("WindowsDemo");

            punct p = new punct();
            Fp.p = p; 
            p.setLocatio(100, 200);
            p.translate(20, 50);

            Fp.init();
            System.out.println(Fp.r1);
            String a = Fp.toString();







    }

}
![图片说明](https://img-ask.csdn.net/upload/201711/07/1510003314_815224.png)

我从方法中获取了rgb值附给了数组,但是每次除了Paint方法之后,就会立刻被初始化零。

  • 写回答

3条回答

  • threenewbee 2017-11-07 00:37
    关注

    你的程序太乱,请贴代码以前,把不必要的空行删除。
    然后,你的程序怎么有两个main函数,到底是一个程序还是两个程序。
    最后,你的图我看不到。

    评论

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗