我需要帮助弄清楚如何使用Java中的draw方法在嵌套的for循环中绘制棋盘格/被子状图案。我的实验室作业内容如下。
您必须创建QuiltPattern类并声明该类的两个对象。此类的构造函数将接受一个参数,该参数设置对象的特征(如颜色),在绘制被子时替换这些对象。确保您的被子至少横过5块,向下7块。您的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);
}
}

最佳答案

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

最好总是分开Main类。我创建了一个新类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;
                }
            }
    }
}

10-05 17:51