Java 使用绘图方法时出现嵌套循环,无法正确设置
我需要帮助了解如何使用java中的draw方法在嵌套for循环中绘制棋盘格/棉被状图案。我的实验作业如下。 您必须创建一个QuitPattern类并声明该类的两个对象。此类的构造函数将接受一个参数,该参数设置对象的特征(如颜色),在绘制缝合线时替换这些对象。确保你的被子至少有5块宽,7块下。QuiltPattern类应该包括一个名为Draw()的方法,该方法在屏幕上的特定位置绘制图案。必须使用嵌套循环来绘制棉被。 我几乎写了所有东西,但我很难画出像羽毛笔/棋盘一样的对象。我似乎无法使它对齐,到目前为止,我只有两行是偏移的。如果您能提供任何帮助或建议,使其在嵌套循环中工作,我们将不胜感激。我一直在努力寻找类似的东西,但运气不太好。我做错了什么 这是我的被单:Java 使用绘图方法时出现嵌套循环,无法正确设置,java,swing,paint,Java,Swing,Paint,我需要帮助了解如何使用java中的draw方法在嵌套for循环中绘制棋盘格/棉被状图案。我的实验作业如下。 您必须创建一个QuitPattern类并声明该类的两个对象。此类的构造函数将接受一个参数,该参数设置对象的特征(如颜色),在绘制缝合线时替换这些对象。确保你的被子至少有5块宽,7块下。QuiltPattern类应该包括一个名为Draw()的方法,该方法在屏幕上的特定位置绘制图案。必须使用嵌套循环来绘制棉被。 我几乎写了所有东西,但我很难画出像羽毛笔/棋盘一样的对象。我似乎无法使它对齐,到目
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;
}
}
}
如果您需要我的绗缝图案(JFrame):
您的“棉被”类应为“棉被”类。所以我只是将代码从QuiltPattern复制粘贴到QuiltPattern,正如程序中所说的那样,QuiltPattern将进行绘图,它将有两个具有不同绘图属性的对象 最好将主类分开。我创建了一个新类QuiltMain来初始化框架和面板。(这都是你的代码) 更改了QuiltPattern的构造函数(以前的QuiltPattern)以接受2个参数color和size作为正方形的位置,并在逻辑上传递给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)
}
绗缝机
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.awt.Color;
导入java.awt.Dimension;
导入java.awt.Graphics;
导入javax.swing.JPanel;
类QuiltPanel扩展了JPanel{
int x=0,y=0;
int rowCount=7;
int columnCount=5;
私人绗缝机平方一、平方二;
公共绗缝布(){
squareOne=新绗缝图案(25,颜色为蓝色);
squareTwo=新绗缝图案(25,颜色为绿色);
setPreferredSize(新尺寸(750500));
挫折背景(颜色:黑色);
}
公共组件(图形页){
super.paintComponent(第页);
整数计数=0;
int squareSide=squareOne.getWidth();
布尔值firstOne=true;
int quiltWidth=列计数*平方边;
int quiltHeight=行数*平方边;
int initX=(getWidth()-quiltWidth)/2;
int initY=(getHeight()-quiltHeight)/2;
for(int colJ=0;colJ
是否必须使用正方形绘制棋盘?你不能用直线吗,那应该是直截了当的。还有,“交替那些物体”是什么意思?假设对draw方法的一次调用将绘制整个棉被,下一次alternate对象将只是覆盖它。它只是说它需要至少5个块宽和7个向下交替不同的设计。它需要看起来像一床被子,但棋盘已经足够接近我想要实现的目标了。我只需要弄清楚如何在循环中逐列重复它们。@11thdimension此项目基于Java软件解决方案第8版中的一个项目。书中的项目要求我们编写一个程序,绘制一条被子,其中一个简单的图案在正方形网格中重复。然后我们必须修改它,使其具有交替的颜色或任何我们想要的特征。所以最终的结果和被子相似或者可以识别。我想我已经知道了,很快就会发布一些东西。谢谢!我肯定有一点不对劲。非常感谢你的帮助。
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)
}
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)
}
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;
}
}
}
}