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

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

问题描述:

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

例如:当较大的矩形尺寸是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; 
    } 
} 
+0

*“我想知道如何从程序的角度来处理这个问题”*您如何使用铅笔和纸张来处理这个问题? – 2012-07-25 04:43:59

所以你要拼贴的大矩形与一些小的矩形。首先定义一个类来表示小矩形,并创建一个数据结构(可能是ArrayList)来保存它们。使用嵌套for循环遍历大型矩形的区域,步骤为S_width/S_height,并创建尽可能多的小矩形。将它们添加到创建时的ArrayList。在Google上搜索ArrayList,以便在需要时查找Java文档。

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

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

+1

您可能想阅读http://docs.oracle.com/javase/tutorial/2d/index.html获取有关Graphics-ps的帮助。就我个人而言,我从一个JFrame开始,applets只是添加了很多额外的开销,你真的不想处理蝙蝠权利 – MadProgrammer 2012-07-25 04:26:30

+0

在这种情况下,可能甚至不需要存储小矩形(他们可以在绘制过程中以简单的循环进行计算)。随着更复杂的形状,你确实想分开绘图和计算。 – Thilo 2012-07-25 04:28:35

+0

@Thilo:你是对的 - 但由于OP显然只是在学习编程,我认为这样做可以帮助把任务分解成更小,更易于管理的单元。如果你认为另一种方式更好,发表一个答案,如果OP决定这样做,他可以选择你的答案。 – 2012-07-25 04:30:56