2012-07-25 58 views
0

我偶然發現了一個我想用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

回答

1

所以你要拼貼的大矩形與一些小的矩形。首先定義一個類來表示小矩形,並創建一個數據結構(可能是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

相關問題