gpt4 book ai didi

java - 使用绘制方法时的嵌套循环,无法弄清楚如何正确设置它

转载 作者:行者123 更新时间:2023-11-30 07:46:14 26 4
gpt4 key购买 nike

我需要帮助弄清楚如何使用java中的draw方法在嵌套for循环中绘制棋盘/被子状图案。我的实验室作业如下。您必须创建 QuiltPattern 类并声明该类的两个对象。此类的构造函数将接受一个参数,该参数设置对象的特征(如颜色),在绘制被子时交替这些对象。确保您的被子横向至少有 5 个 block ,向下至少有 7 个 block 。 QuiltPattern 类应包含一个名为 Draw() 的方法,用于在屏幕上的特定位置绘制图案。您必须使用嵌套循环来绘制被子。我已经写好了所有内容,但是我在绘制像被子/棋盘一样的对象时遇到了困难。我似乎无法让它对齐,到目前为止我只有两行偏移。任何帮助或建议让它在嵌套循环中工作将不胜感激。我一直在尝试寻找与此类似的东西,但运气不佳。我究竟做错了什么?

这是我的 QuiltPanel:

import javax.swing.*;
import java.awt.*;

public class QuiltPanel extends JPanel
{
int x = 0, y = 0, count = 0;

private Quilt squareOne, squareTwo;

public QuiltPanel()
{
squareOne = new Quilt(25, Color.blue, x+50, y);
squareTwo = new Quilt(25, Color.green, x+25, y);

setPreferredSize(new Dimension(750, 500));
setBackground(Color.black);

}
public void paintComponent(Graphics page)
{
super.paintComponent(page);
for ( count = 0; count <= 10; count = count+1)
{ if ( count % 2 == 0)
{ if ( count <= 5)
{ squareOne.draw(page);
squareOne = new Quilt(25, Color.blue, x, y);

}
else
{ squareOne.draw(page);
squareOne = new Quilt(25, Color.blue, x, y+25);

}

}
else
{ if ( count <= 5)
{ squareTwo.draw(page);
squareTwo = new Quilt(25, Color.green, x, y);

}
else
{ squareTwo.draw(page);
squareTwo = new Quilt(25, Color.green, x, y+25);

}

}
x=x+25;
}
}
}

这是我的被子类:

import java.awt.*;

public class Quilt

{
private int height, width, x, y;
private Color color;

public Quilt(int size, Color newColor, int upperX, int upperY)
{
width = size;
height = size;
color = newColor;
x = upperX;
y = upperY;

}

public void draw(Graphics page)
{
page.setColor(color);
page.fillRect(x, y, width, height);
}

public void setHeight(int size)
{
height = size;
}

public void setWidth(int size)
{
width = size;
}

public void setColor(Color newColor)
{
color = newColor;
}

public void setX(int upperX)
{
x = upperX;
}

public void setY(int upperY)
{
y = upperY;
}

public int getHeight()
{
return height;
}

public int getWidth()
{
return width;
}

public Color getColor()
{
return color;
}

public int getX()
{
return x;
}

public int getY()
{
return y;
}
}

如果您需要我的 QuiltPattern (JFrame):

import javax.swing.*;
import java.awt.*;

public class QuiltPattern
{
public static void main(String[] args)
{
JFrame frame = new JFrame("QuiltPattern");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add(new QuiltPanel());
frame.getContentPane().add(new QuiltPanel());

frame.pack();
frame.setVisible(true);
}
}

最佳答案

您的 Quilt 类应该是 QuiltPattern 类。所以我只是将该代码从 Quilt 复制并粘贴到 QuiltPattern,正如程序中所说,QuiltPattern 将执行绘图,并且它将有 2 个具有不同绘图属性的对象。

最好将主类分开。我创建了一个新类 QuiltMain 来初始化框架和面板。 (这都是您的代码)。

更改了 QuiltPattern(以前称为 Quilt)的构造函数以接受 2 个参数颜色和大小,因为正方形的位置在逻辑上应传递给 draw() 方法本身。

最后是创建棋盘图案的循环。一般有3种选择。

选项1。

int initX = getWidth() / 2 - (columnCount * squareSide) / 2;
int initY = getHeight() / 2 - (rowCount * squareSide) / 2;
int squareCount = rowCount * columnCount;

for(int i = 0; i < squareCount; i++) {
int rowI = squareCount / rowCount;
int colJ = squareCount % columnCount;

//draw at (initX + colJ * squareSide, initY + colI * squareSide)
}

选项 2。

int centerX = getWidth() / 2;
int centerY = getHeight() / 2;

for(int colJ = -rowCount/2; colJ < rowCount/2; colJ++) { //condition will include <= when rowCount is odd
for(int rowI = -columnCount/2; rowI < columnCount/2; rowI++) {//condition will include <= when columnCount is odd

//draw at (centerX + colJ * squareSide, centerY + colI * squareSide)
}

选项 3:

int initX = getWidth() / 2 - (columnCount * squareSide) / 2;
int initY = getHeight() / 2 - (rowCount * squareSide) / 2;

for(int colJ = 0; colJ < rowCount; colJ++) {
for(int rowI = 0; rowI < columnCount; rowI++) {

//draw at (initX + colJ * squareSide, initY + colI * squareSide)
}

我选择了选项 3,因为它看起来最简单、最合适。

工作代码:

被子主

import javax.swing.JFrame;


public class QuiltMain {
public static void main(String[] args) {
JFrame frame = new JFrame("QuiltPattern");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add(new QuiltPanel());
frame.getContentPane().add(new QuiltPanel());

frame.pack();
frame.setVisible(true);
}
}

被子图案

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class QuiltPattern {
private int height, width, x, y;
private Color color;

public QuiltPattern(int size, Color newColor) {
width = size;
height = size;
color = newColor;
}

public void draw(Graphics page, int x, int y) {
page.setColor(color);
page.fillRect(x, y, width, height);
}

public void setHeight(int size) {
height = size;
}

public void setWidth(int size) {
width = size;
}

public void setColor(Color newColor) {
color = newColor;
}

public void setX(int upperX) {
x = upperX;
}

public void setY(int upperY) {
y = upperY;
}

public int getHeight() {
return height;
}

public int getWidth() {
return width;
}

public Color getColor() {
return color;
}

public int getX() {
return x;
}

public int getY() {
return y;
}
}

被子面板

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JPanel;

class QuiltPanel extends JPanel {
int x = 0, y = 0;
int rowCount = 7;
int columnCount = 5;

private QuiltPattern squareOne, squareTwo;

public QuiltPanel() {
squareOne = new QuiltPattern(25, Color.blue);
squareTwo = new QuiltPattern(25, Color.green);

setPreferredSize(new Dimension(750, 500));
setBackground(Color.black);

}

public void paintComponent(Graphics page) {
super.paintComponent(page);
int count = 0;
int squareSide = squareOne.getWidth();
boolean firstOne = true;

int quiltWidth = columnCount * squareSide;
int quiltHeight = rowCount * squareSide;

int initX = (getWidth() - quiltWidth) / 2;
int initY = (getHeight() - quiltHeight) / 2;

for(int colJ = 0; colJ < columnCount; colJ++) {
for(int rowI = 0; rowI < rowCount; rowI++) {
int x = colJ * squareSide + initX;
int y = rowI * squareSide + initY;
if(firstOne) {
squareOne.draw(page, x, y);
} else {
squareTwo.draw(page, x, y);
}
firstOne = !firstOne;
}
}
}
}

关于java - 使用绘制方法时的嵌套循环,无法弄清楚如何正确设置它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33911978/

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