嵌套for循环以创建砖墙

时间:2014-07-24 07:24:47

标签: java nested-loops

我是编程新手,这是我第一次发布到这个网站。我目前正在进行一项任务,允许用户输入1到20之间的数字,然后根据用户输入JTextField的砖层数创建一个砖墙。我能够创建一行,但我并不真正理解嵌套语句,我在网上看到的所有例子都让我更加困惑,所以我想知道是否有人可以帮助我更好地理解嵌套for循环。

//Package List
import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;

public class Wall extends JApplet implements KeyListener {



//Component declaration
    JLabel directions;
    JTextField input;
    //Variable declaration
    int startX = 50;
    int startY = 650;
    int width = 50;
    int height = 20;
    int spacing = 2;


    //Method declaration
    public void init() 
    {
        getContentPane().setBackground(new Color (128, 128, 128));//Changes backround of JApplet to black
        //Set JTextField and JLabel
        setLayout (new FlowLayout( ));
        directions = new JLabel("Enter in any number between 1 and 20 and then press Enter on your keyboard.");
        input = new JTextField ( 10 );
        add (directions );
        add (input);

        //Key listener
        addKeyListener( this );
        setFocusable( true );   
    }

    //Method declaration 
    public void paint(Graphics g) 
    {
        super.paint (g);

        for (int col=1; col<= 8; col++)
         {
         // for (int row; row<=20; row++)
            {   g.setColor (Color.RED);
                g.fillRect (startX, startY, 50, 20);
                startX =  startX + spacing + width;
            }

         }
    }

    //Key event methods
    public void keyReleased( KeyEvent ke ){}
    public void keyTyped (KeyEvent ke ) {}
    public void keyPressed ( KeyEvent ke )
    {
        int key = ke.getKeyCode ( );    
        if (key == KeyEvent.VK_ENTER)
        {
        }

    }

2 个答案:

答案 0 :(得分:1)

您需要根据当前x / y计算砖块的row / col位置。现在,您可以在每个循环开始时简单地初始化这些值并根据需要增加,或者您可以使用一些数学...

Bricks

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class AnotherBrickInTheWall extends JApplet {

//Component declaration
    JLabel directions;
    JTextField input;
    //Variable declaration
    int startX = 0;
    int startY = 50;
    int width = 50;
    int height = 20;
    int spacing = 2;

    //Method declaration
    public void init() {
        getContentPane().setBackground(new Color(128, 128, 128));//Changes backround of JApplet to black
        //Set JTextField and JLabel
        setLayout(new FlowLayout());
        directions = new JLabel("Enter in any number between 1 and 20 and then press Enter on your keyboard.");
        input = new JTextField(10);
        add(directions);
        add(input);
    }

    //Method declaration 
    @Override
    public void paint(Graphics g) {
        super.paint(g);

        for (int row = 0; row < 8; row++) {
            int y = startY + (row * (height + spacing));
            for (int col = 0; col < 8; col++) {
                int x = startX + (col * (width + spacing));
                System.out.println(x + "x" + y);
                g.setColor(Color.RED);
                g.fillRect(x, y, width, height);
            }
        }
    }
}

现在,你永远不应该在添加了组件的组件上绘画,这会导致绘画出现在组件的顶部,最终会出现各种绘画问题。

相反,创建一个自定义组件,从JPanel扩展并覆盖它的paintComponent方法,然后将自定义绘图放在此处。

然后,您可以在applet上添加另一个JPanel控件并使用BorderLayout,添加这两个面板,NORTH中的字段和CENTER中的字段位置

<强>更新

如果你想“非常”喜欢,你甚至可以根据当前的x调整row位置,例如......

Bricks

int xOffset = 0;
for (int row = 0; row < 8; row++) {
    int y = startY + (row * (height + spacing));
    if (row % 2 == 0) {
        xOffset = width / 2;
    } else {
        xOffset = 0;
    }
    for (int col = 0; col < 8; col++) {
        int x = xOffset + (startX + (col * (width + spacing)));
        System.out.println(x + "x" + y);
        g.setColor(Color.RED);
        g.fillRect(x, y, width, height);
    }
}

此外,JTextField有一个ActionListener,当字段被执行时会触发该KeyListener,通常是用户按 Enter 。这意味着您不需要{{1}}。

有关详细信息,请参阅How to Use Text Fields

答案 1 :(得分:0)

int x=num //1-20
int step=num/8;
int last_row=num%8;
for(int j=1;j<=step;j++){
    int nos=8;
    if(j == step && last_step ! = 0)
        nos = last_step;
    for (int col=1; col<= nos; col++)
    {
        g.setColor (Color.RED);
        g.fillRect (startX, startY, 50, 20);
        startX =  startX + spacing + width;
        startY =  startY*j;
    }
}

试试这个。