簡體   English   中英

使用繪制方法時的嵌套循環,很難弄清楚如何正確設置它

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

我需要幫助弄清楚如何使用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,因為它似乎最簡單,最合適。

工作代碼:

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

QuiltPattern

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

QuiltPanel

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM