<legend id='YwhE2'><style id='YwhE2'><dir id='YwhE2'><q id='YwhE2'></q></dir></style></legend>

<small id='YwhE2'></small><noframes id='YwhE2'>

  • <tfoot id='YwhE2'></tfoot>

    <i id='YwhE2'><tr id='YwhE2'><dt id='YwhE2'><q id='YwhE2'><span id='YwhE2'><b id='YwhE2'><form id='YwhE2'><ins id='YwhE2'></ins><ul id='YwhE2'></ul><sub id='YwhE2'></sub></form><legend id='YwhE2'></legend><bdo id='YwhE2'><pre id='YwhE2'><center id='YwhE2'></center></pre></bdo></b><th id='YwhE2'></th></span></q></dt></tr></i><div id='YwhE2'><tfoot id='YwhE2'></tfoot><dl id='YwhE2'><fieldset id='YwhE2'></fieldset></dl></div>

      • <bdo id='YwhE2'></bdo><ul id='YwhE2'></ul>
      1. 创建一个多彩多姿的板

        时间:2024-08-24

          <tbody id='T3qTT'></tbody>

                <bdo id='T3qTT'></bdo><ul id='T3qTT'></ul>
              • <legend id='T3qTT'><style id='T3qTT'><dir id='T3qTT'><q id='T3qTT'></q></dir></style></legend>

                  <small id='T3qTT'></small><noframes id='T3qTT'>

                  <tfoot id='T3qTT'></tfoot>
                  <i id='T3qTT'><tr id='T3qTT'><dt id='T3qTT'><q id='T3qTT'><span id='T3qTT'><b id='T3qTT'><form id='T3qTT'><ins id='T3qTT'></ins><ul id='T3qTT'></ul><sub id='T3qTT'></sub></form><legend id='T3qTT'></legend><bdo id='T3qTT'><pre id='T3qTT'><center id='T3qTT'></center></pre></bdo></b><th id='T3qTT'></th></span></q></dt></tr></i><div id='T3qTT'><tfoot id='T3qTT'></tfoot><dl id='T3qTT'><fieldset id='T3qTT'></fieldset></dl></div>

                  本文介绍了创建一个多彩多姿的板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我要创建一个彩色板,从第一个黑色方块开始,然后是蓝色、红色和黄色,这些方块是对角线填充的,没有空的彩色方块.我知道我的算法是错误的,但我不知道如何修复它.目前,我的代码打印出来是这样的

                  I am to create a multicolored board, starting with the first square as black, then blue, red, and yellow, the squares are being filled diagonally and there are no empty colored squares. I know my algorithm is wrong, but I have not a clue as how to fix it. Currently, my code prints out like this

                  import javax.swing.JFrame;
                  import javax.swing.JPanel;
                  import java.awt.Color;
                  import java.awt.Dimension;
                  import java.awt.Graphics;
                  import java.awt.Graphics2D;
                  import java.awt.Insets;
                  
                  public class Grid extends JPanel {
                  private static final long serialVersionUID = 1L;
                  public static final int GRID_COUNT = 8;
                  private Color[] colors = { Color.black, Color.yellow, Color.red,
                          Color.blue };
                  private int colorIndex = 0;
                  
                  public void paintComponent(Graphics g) {
                      super.paintComponent(g);
                      Graphics2D graphics = (Graphics2D) g;
                      graphics.setColor(Color.black);
                  
                      Dimension size = getSize();
                      Insets insets = getInsets();
                      int w = size.width - insets.left - insets.right;
                      int h = size.height - insets.top - insets.bottom;
                  
                      int sqrWidth = (int)((double)w / GRID_COUNT);
                      int sqrHeight = (int)((double)h / GRID_COUNT);
                      for (int row = 0; row < GRID_COUNT; row++) {
                          for (int col = 0; col < GRID_COUNT; col++) {
                                  int x = (int) (row * (double) w / GRID_COUNT);
                                  int y = (int) (col * (double) h / GRID_COUNT);
                                  if ((row + col) % 2 == 0) {
                                      int colorIndex = (row + col) % 4;
                                      graphics.fillRect(x, y, sqrWidth, sqrHeight);
                                      graphics.setColor(colors[colorIndex]);
                                      colorIndex = (colorIndex + 1) % colors.length;
                      }
                  
                  }
                  
                  public static void main(String[] args) {
                      Grid grid = new Grid();
                      grid.setPreferredSize(new Dimension(400, 400));
                      JFrame frame = new JFrame("Grid");
                      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                      frame.add(grid);
                      frame.pack();
                      frame.setLocationRelativeTo(null);
                      frame.setVisible(true);
                  
                  }
                  

                  }

                  推荐答案

                  我快速浏览了代码,有很多小东西让我感到困惑..

                  I've run through the code quickly and there are number of little things which seem confusing to me..

                  您的颜色计算被抛出,因为您的原始代码每隔一个单元格跳过...

                  Your color calculation is been thrown off because your original code was skipping every second cell...

                  if ((row + col) % 2 == 0) {

                  这意味着当您尝试确定颜色时,您没有得到您期望的颜色.

                  Which meant that when you tried to determine the color, you weren't getting the color you were expecting.

                  您还在行循环中而不是在列循环中设置单元格的颜色,这对我来说似乎很奇怪......

                  You were also setting the color of the cell within the row loop and not in the column loop, which seems weird to me...

                  (我在其中添加了文本以进行调试,请随意摆脱它)

                  (I added text in to for debugging, feel free to get rid of it)

                  import java.awt.Color;
                  import java.awt.Dimension;
                  import java.awt.EventQueue;
                  import java.awt.FontMetrics;
                  import java.awt.Graphics;
                  import java.awt.Graphics2D;
                  import java.awt.Insets;
                  import javax.swing.JFrame;
                  import javax.swing.JPanel;
                  import javax.swing.UIManager;
                  import javax.swing.UnsupportedLookAndFeelException;
                  
                  public class Grid {
                  
                      private static final long serialVersionUID = 1L;
                      public static final int GRID_COUNT = 8;
                      private Color[] colors = {Color.black, Color.yellow, Color.red,
                          Color.blue};
                  
                      public static void main(String[] args) {
                          new Grid();
                      }
                  
                      public Grid() {
                          EventQueue.invokeLater(new Runnable() {
                              @Override
                              public void run() {
                                  try {
                                      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                                  } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                                  }
                  
                                  JFrame frame = new JFrame("Grid");
                                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                  frame.add(new GridPane());
                                  frame.pack();
                                  frame.setLocationRelativeTo(null);
                                  frame.setVisible(true);
                  
                              }
                          });
                      }
                  
                      public class GridPane extends JPanel {
                  
                          @Override
                          public Dimension getPreferredSize() {
                              return new Dimension(200, 200);
                          }
                  
                          @Override
                          public void paintComponent(Graphics g) {
                              super.paintComponent(g);
                              Graphics2D graphics = (Graphics2D) g;
                              graphics.setColor(Color.black);
                  
                              Dimension size = getSize();
                              Insets insets = getInsets();
                              int w = size.width - insets.left - insets.right;
                              int h = size.height - insets.top - insets.bottom;
                  
                              FontMetrics fm = graphics.getFontMetrics();
                  
                              int sqrWidth = (int) ((double) w / GRID_COUNT);
                              int sqrHeight = (int) ((double) h / GRID_COUNT);
                              int colorIndex = 0;
                              for (int row = 0; row < GRID_COUNT; row++) {
                                  for (int col = 0; col < GRID_COUNT; col++) {
                                      int x = (int) (col * sqrWidth);
                                      int y = (int) (row * sqrHeight);
                                      colorIndex = (row + col) % 4;
                                      graphics.setColor(colors[colorIndex]);
                                      graphics.fillRect(x, y, sqrWidth, sqrHeight);
                                      String text = row + "/" + col;
                                      graphics.setColor(Color.WHITE);
                                      graphics.drawString(
                                              text,
                                              x + ((sqrWidth - fm.stringWidth(text)) / 2),
                                              y + ((sqrHeight - fm.getHeight()) / 2) + fm.getAscent());
                                  }
                              }
                  
                          }
                      }
                  }
                  

                  这篇关于创建一个多彩多姿的板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:自定义 gxt Cell 可能需要 Widget 下一篇:GridView 在触摸时获取项目

                  相关文章

                  <small id='lZ0ni'></small><noframes id='lZ0ni'>

                      <bdo id='lZ0ni'></bdo><ul id='lZ0ni'></ul>
                  1. <legend id='lZ0ni'><style id='lZ0ni'><dir id='lZ0ni'><q id='lZ0ni'></q></dir></style></legend>

                    <tfoot id='lZ0ni'></tfoot>
                  2. <i id='lZ0ni'><tr id='lZ0ni'><dt id='lZ0ni'><q id='lZ0ni'><span id='lZ0ni'><b id='lZ0ni'><form id='lZ0ni'><ins id='lZ0ni'></ins><ul id='lZ0ni'></ul><sub id='lZ0ni'></sub></form><legend id='lZ0ni'></legend><bdo id='lZ0ni'><pre id='lZ0ni'><center id='lZ0ni'></center></pre></bdo></b><th id='lZ0ni'></th></span></q></dt></tr></i><div id='lZ0ni'><tfoot id='lZ0ni'></tfoot><dl id='lZ0ni'><fieldset id='lZ0ni'></fieldset></dl></div>