使用 draw 方法时出现嵌套循环,无法弄清楚如何正确设置它

Nested loops when using draw methods, having trouble figuring out how to set it up right

我需要帮助弄清楚如何使用 java 中的 draw 方法在嵌套 for 循环中绘制类似 checkerboard/quilt 的模式。我的实验室作业如下。 您必须创建一个 QuiltPattern Class 并声明该 class 的两个对象。此 class 的构造函数将接受一个参数,该参数设置对象的特征(如颜色),在绘制被子时交替使用这些对象。确保你的被子至少包含 5 个方块和 7 个方块。您的 QuiltPattern class 应该包含一个名为 Draw() 的方法,该方法在屏幕上的特定位置绘制图案。您必须使用嵌套循环来绘制被子。 我已经把大部分内容都写好了,但是我无法绘制 quiltlike/checkerboard 之类的对象。我似乎无法让它排成一行,到目前为止我只有两行是偏移的。任何使它在嵌套循环中工作的帮助或建议将不胜感激。我一直在努力寻找与此类似的东西,但运气不佳。我究竟做错了什么?

这是我的被子面板:

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;
        }
   }
}

这是我的被子Class:

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);
}
}

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

始终最好将 Main class 分开。我创建了一个新的 class 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,因为它看起来最简单也最合适。

工作代码:

QuiltMain

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;
                }
            }
    }
}