gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 15:15:38 25 4
gpt4 key购买 nike

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

例如:当较大矩形尺寸为 4 x 5,较小矩形尺寸为 2 x 2 时,我能够将其放置在较大矩形内的较小矩形的最大数量为 4。我想以图形方式显示它们。

由于我是java新手,我想知道如何从编程的角度解决这个问题,以及我必须使用什么概念来实现相同的目标。

<小时/>

用于计算最大矩形数量的初始代码。任何人都可以帮我用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;
}
}

最佳答案

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

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

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

关于java - 如何使用Java将较小的矩形放置在较大的矩形内?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11642631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com