嵌套for循环以绘制矩形 [英] nested for loops to draw rectangles

查看:153
本文介绍了嵌套for循环以绘制矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Graphics;
@SuppressWarnings("serial")

public class RedAndBlackSteps extends JFrame
{
   public int startX, startY;
   public static int brickWidth, brickHeight, brickSpace;
   public static int numberOfRows;
   public RedAndBlackSteps()
   {
      super ("Pyramid");
      startX = 400;
      startY = 50;
      brickWidth = 50;
      brickHeight = 50;
      brickSpace = 2;
      numberOfRows = 5;
   }

public void paint(Graphics g)
{
   super.paint(g);
   int currentX = startX;
   int currentY = startY;
   for (int i = 0; i < numberOfRows; i++) //for loop used to draw the bricks      building the steps
   {
      for (int j = 0; j <= i; j++)
      {
         g.setColor(Color.red);
      }
      g.fillRect(currentX, currentY, brickWidth, brickHeight);
      currentY = currentY + 50 + brickSpace;
      currentX = currentX - 50 + brickSpace;
   }  
}
public static void main(String[] args) // main used to display GUI dialogue box
{
   RedAndBlackSteps app = new RedAndBlackSteps();
   app.setSize(800, 400);
   app.setVisible(true);
   app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //when user presses "X" the    dialogue box will close
}



我想这样画
第一排顶部的红色块
红色黑色第二行红色
红色黑色黑色黑色黑色红色用于第三行,等等,用于5行

到现在为止,我只能画红色方块

谁能帮我吗



I want to draw like this
red block on the top first row
red black red for the second row
red black black black red for the third row etc for 5 rows

until now I am able to draw only red blocks

can anybody please help me

推荐答案

您只能将颜色设置为红色.将其设置为黑色,它将为黑色
You only set the color to red. Set it to black and it will be black


这篇关于嵌套for循环以绘制矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆