如何使用Java将较小的矩形放在较大的矩形内?

时间:2012-07-25 04:16:49

标签: java applet java-2d

我偶然发现了一个问题,我想用Java解决它。用户输入较大的矩形尺寸(即L_width和L_height)和较小的矩形尺寸(即S_width和S_height)。我想在较大的矩形内放置尽可能多的较小矩形并以图形方式显示。

例如:当较大的矩形大小为4 x 5且较小的矩形大小为2 x 2时,那么我可以将它放在较大的矩形内的较小矩形的最大数量是4.我想要以图形方式显示它们。

作为java的新手,我想知道如何从程序化的角度来看待这个问题,以及我必须用什么概念来实现同样的目标。


计算最大矩形数的初始代码。 any1可以帮我用java

以图形方式显示这个结果
// Code Starts
import java.awt.Graphics;
import java.util.Scanner; 

import javax.swing.JComponent;
import javax.swing.JFrame;

//Class to store the output of layout
class layout{
    private int Cnt_BW_CW=0; // BoardWidth and CardWidth are arranged together
    private int Cnt_BW_CH=0;
    private int option=0;    // Option 1: width-width Option 2: width-height

    public int getCnt_BW_CW (){
        return Cnt_BW_CW;
    }
    public int getCnt_BW_CH (){
        return Cnt_BW_CH;
    }
    public int getoption (){
        return option;
    }

    public void setCnt_BW_CW (int newValue){
        Cnt_BW_CW = newValue;
    }
    public void setCnt_BW_CH (int newValue){
        Cnt_BW_CH = newValue;
    }
    public void setoption (int newValue){
        option = newValue;
    }
}

// Stores the Dimension
class Dimension{
    private float w,h;
    Scanner input = new Scanner( System.in );

    public Dimension(){
        System.out.print( "Enter Width: " ); 
        w = input.nextInt(); 
        System.out.print( "Enter Height: " ); 
        h = input.nextInt(); 
    }
    public Dimension(float width, float height){
        w = width;
        h = height;
    }
    public float getWidth (){
        return w;
    }
    public float getHeight (){
        return h;
    }
    public void setWidth (float newWidth){
        w = newWidth;
    }
    public void setHeight (float newHeight){
        h = newHeight;
    }
}

class MyCanvas extends JComponent {
    private static final long serialVersionUID = 1L;

    public void paint(Graphics g) {
        g.drawRect (10, 10, 200, 200);  
      }
}

public class boundedRect {
  @SuppressWarnings("unused")

  public static void main(String[] a) {
    Dimension Board = new Dimension();
    Dimension Card = new Dimension();
    int Cnt =0;

    Cnt = NumOfRect(Board, Card);
    System.out.printf( "Number of Cards:%d",Cnt );

    JFrame window = new JFrame();
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setBounds(30, 30, 300,300);
    window.getContentPane().add(new MyCanvas());
    window.setVisible(true);
  }

  public static int NumOfRect(Dimension b,Dimension c){
      float bw,bh,cw,ch;
      int bw_cw,bh_ch,bw_ch,bh_cw;
      int SameDimensionCnt,DiffDimensionCnt;
      int count;
      layout Result = new layout();

      bw =b.getWidth(); bh = b.getHeight();
      cw =c.getWidth(); ch = c.getHeight();

      if (bw < cw || bh < ch){
          System.out.println( "Board and Card Dimension mismatch" );
          System.exit(0);
      }

      bw_cw = (int)Math.floor(bw/cw);
      bh_ch = (int)Math.floor(bh/ch);
      SameDimensionCnt = bw_cw * bh_ch; 
      Result.setCnt_BW_CW(SameDimensionCnt);

      bw_ch = (int)Math.floor(bw/ch);
      bh_cw = (int)Math.floor(bh/cw);
      DiffDimensionCnt = bw_ch * bh_cw;
      Result.setCnt_BW_CH(DiffDimensionCnt);

      System.out.printf( "Matching BW x CW: %d\n",SameDimensionCnt );
      System.out.printf( "Matching BW x CH: %d\n",DiffDimensionCnt );

      if (SameDimensionCnt < DiffDimensionCnt ){
          count = DiffDimensionCnt;
          System.out.println( "Align Board Width and Card Height" );
          Result.setoption(2);
      }else {
          count = SameDimensionCnt;
          System.out.println( "Align Board Width and Card Width" );
          Result.setoption(1);
      }
      return count;
  }
}

1 个答案:

答案 0 :(得分:1)

因此,您希望平铺一个包含许多较小矩形的大矩形。首先定义一个类来表示小矩形,并创建一个数据结构(可能是ArrayList)来保存它们。使用嵌套的for循环以S_width / S_height步长遍历大矩形区域,并创建尽可能多的小矩形。创建时将它们添加到ArrayList。如果您需要,请在Google上搜索ArrayList以查找Java文档。

然后你需要编写代码在屏幕上绘制它们。为此,请查看Google上的官方Java教程并阅读有关图形的部分。

首先尝试编写代码,如果遇到问题,请在此处发布代码(您可以编辑问题)。