-1

我试图在这里学习一些Java,但遇到了一个问题。程序应该生成随机的字段形状,但它只生成随机的空形状。

最后一个参数是否填充但似乎没有效果。

这是代码。

  public DrawPanel()
    {
        setBackground( Color.WHITE );

        lines = new MyLine[1 + (int)(Math.random() * ((5 - 1) + 1))];
        rectangles = new MyRectangle[1 + (int)(Math.random() * ((5 - 1) + 1))];
        ovals = new MyOval[1 + (int)(Math.random() * ((5 - 1) + 1))];

        // create lines
        for ( int count = 0; count < lines.length; count++ )
        {
            // generate random coordinates
            int x1 = randomNumbers.nextInt( 300 );
            int y1 = randomNumbers.nextInt( 300 );
            int x2 = randomNumbers.nextInt( 300 );
            int y2 = randomNumbers.nextInt( 300 );

            // generate a random color
            Color color = new Color( randomNumbers.nextInt( 256 ), 
                    randomNumbers.nextInt( 256 ), randomNumbers.nextInt( 256 ) );

            // add the line to the list of lines to be displayed
            lines[count] = new MyLine( x1, y1, x2, y2, color );


            // create Rectangles
            for ( int countR = 0; countR < rectangles.length; countR++ )
            {
                // generate random coordinates
                int x1R = randomNumbers.nextInt( 300 );
                int y1R = randomNumbers.nextInt( 300 );
                int x2R = randomNumbers.nextInt( 300 );
                int y2R = randomNumbers.nextInt( 300 );

                // generate a random color
                Color colorR = new Color( randomNumbers.nextInt( 256 ), 
                        randomNumbers.nextInt( 256 ), randomNumbers.nextInt( 256 ) );

                // add the rectangles to the list of rectangles to be displayed
                rectangles[countR] = new MyRectangle( x1R, y1R, x2R, y2R, colorR, true );


                // create Ovals
                for ( int countO = 0; countO < ovals.length; countO++ )
                {
                    // generate random coordinates
                    int x1O = randomNumbers.nextInt( 300 );
                    int y1O = randomNumbers.nextInt( 300 );
                    int x2O = randomNumbers.nextInt( 300 );
                    int y2O = randomNumbers.nextInt( 300 );

                    // generate a random color
                    Color colorO = new Color( randomNumbers.nextInt( 256 ), 
                            randomNumbers.nextInt( 256 ), randomNumbers.nextInt( 256 ) );

                    // add the rectangles to the list of rectangles to be displayed
                    ovals[countO] = new MyOval( x1O, y1O, x2O, y2O, colorO, true );


                } // end Ovals for  


            } // end Rectangle for   

        } // end for
    } // end DrawPanel constructor



   // for each shape array, draw the individual shapes
   public void paintComponent( Graphics g )
   {
      super.paintComponent( g );



//      // draw the lines
//      for ( MyLine line : lines )
//         line.draw( g );

      for ( MyRectangle rectangle : rectangles )
          {rectangle.draw(g);}

      for( MyOval oval : ovals)
          {  oval.draw(g);   }

      for( MyLine line : lines)
      {  line.draw(g);   }




   }



public class MyRectangle {

    private int x1 = 0; // x-coordinate of first endpoint
    private int y1 = 0; // y-coordinate of first endpoint
    private int x2 = 0; // x-coordinate of second endpoint
    private int y2 = 0; // y-coordinate of second endpoint
    private Color myColor; // color of this shape
    boolean filled ; // Boolean flag if shape is filled

    public MyRectangle(int x1, int y1, int x2, int y2, Color myColor,
            boolean filled) {

        super();
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
        this.myColor = myColor;
        this.filled = filled;
    }

    public MyRectangle() {

        super();
        setUpperLeftX(0,0);
        setUpperLeftY(0,0);
        this.myColor = Color.BLACK;
        this.filled = false;
    }


    public int getUpperLeftX(int x1, int x2){

        // The upper-left x-coordinate is the smaller of the two x-coordinate values

        if(x1 <= x2){

            return x1;

        }
        else{

            return x2;
        }



    }

    public void setUpperLeftX(int x1, int x2){

        if(x1 >= 0){
        this.x1 = x1;
        }
        else{this.x1 = 0;}
        if(x2 >= 0){
        this.x2 = x2;   
        }
        else{this.x2 = 0;}



    }

    public int getUpperLeftY(int x1, int x2){

    // the upperleft y-coordinate is the smaller of the two y-coordinate values

        if(y1 <= y2){

            return y1;

        }
        else{

            return y2;
        }

    }  

    public void setUpperLeftY(int x1, int x2){

        if(y1 >= 0){
        this.y1 = x1;
        }
        else{this.y1 = 0;}
        if(y2 >= 0){
        this.y2 = x2;   
        }
        else{this.y2 = 0;}

    } 

    public int getWidth(int x1, int x2){

        int width = 0;


        // the width is the absolute value of the difference between the two x-coordinate values

        width = (x1 - x2);

        return Math.abs(width);

    }

    public int getHeight(){

        int height = 0;

        // the height is the absolute value of the difference between the two y-coordinate values.


        height = (y1 - y2);

        return Math.abs(height);

    }

    public void draw( Graphics g )
       {




          g.setColor( getMyColor() );
          g.drawRect(getX1(), getY1(), getX2(), getY2());
       } // end method draw

    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;
    }

    public Color getMyColor() {
        return myColor;
    }

    public void setMyColor(Color myColor) {
        this.myColor = myColor;
    }

    public boolean isFilled() {
        return filled;
    }

    public void setFilled(boolean filled) {
        this.filled = filled;
    }



public static void main( String args[] )
   {
      DrawPanel panel = new DrawPanel();      
      JFrame application = new JFrame();

      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      application.add( panel );
      application.setSize( 600, 600 );
      application.setVisible( true );
   } // end main
4

1 回答 1

2

你从不运行填充方法

尝试用这个替换你的draw方法:

public void draw( Graphics g )
   {          
      g.setColor( getMyColor() );
      if(filled)
         g.fillRect(getX1(), getY1(), getWidth(), getHeight());
      else
         g.drawRect(getX1(), getY1(), getWidth(), getHeight());
   } // end method draw

您似乎也使用第二个坐标而不是宽度和高度

于 2013-10-13T17:42:49.060 回答