Java为什么单元格颜色不断变化?

huangapple go评论110阅读模式
英文:

Java why does cell keep changing colors

问题

  1. import javax.swing.*;
  2. import java.awt.*;
  3. class Main extends JFrame {
  4. class App extends JPanel {
  5. Grid grid;
  6. public App() {
  7. setPreferredSize(new Dimension(900, 720));
  8. grid = new Grid();
  9. }
  10. @Override
  11. public void paint(Graphics g) {
  12. grid.paint(g, getMousePosition());
  13. }
  14. }
  15. public static void main(String[] args) throws Exception {
  16. Main window = new Main();
  17. window.run();
  18. }
  19. private Main() {
  20. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  21. App canvas = new App();
  22. this.setContentPane(canvas);
  23. this.pack();
  24. this.setVisible(true);
  25. }
  26. public void run() {
  27. while (true) {
  28. this.repaint();
  29. }
  30. }
  31. }
  32. import java.awt.*;
  33. class Grid {
  34. // fields
  35. Cell[][] cells = new Cell[20][20];
  36. // constructor
  37. public Grid() {
  38. for (int i = 0; i < cells.length; i++) {
  39. for (int j = 0; j < cells[i].length; j++) {
  40. cells[i][j] = new Cell(10 + 35 * i, 10 + 35 * j);
  41. }
  42. }
  43. }
  44. // methods
  45. public void paint(Graphics g, Point mousePos) {
  46. for (int i = 0; i < cells.length; i++) {
  47. for (int j = 0; j < cells[i].length; j++) {
  48. cells[i][j].paint(g, mousePos);
  49. }
  50. }
  51. }
  52. }
  53. import java.awt.*;
  54. import java.util.Random;
  55. class Cell extends Rectangle {
  56. // fields
  57. int random_int;
  58. int minyellow = 50;
  59. int maxyellow = 100;
  60. static int size = 35;
  61. // constructors
  62. public Cell(int x, int y) {
  63. super(x, y, size, size);
  64. }
  65. // methods
  66. void paint(Graphics g, Point mousePos) {
  67. int random_int = (int) (Math.random() * (maxyellow - minyellow + 1) + minyellow);
  68. this.random_int = random_int;
  69. if (contains(mousePos)) {
  70. g.setColor(Color.GRAY);
  71. } else {
  72. g.setColor(Color.getHSBColor(255, random_int, 60));
  73. }
  74. g.fillRect(x, y, size, size);
  75. g.setColor(Color.BLACK);
  76. g.drawRect(x, y, size, size);
  77. }
  78. public boolean contains(Point p) {
  79. if (p != null) {
  80. return super.contains(p);
  81. } else {
  82. return false;
  83. }
  84. }
  85. }
英文:

Hello sorry I'm very weak in programming.
I'm trying to make my Box each a different shade of green. However, when I implement my code it keeps changing colors even when my box loop is finished.
The finished product is suppose to be a tetris with boxes that have different color of green.
So tetris class creates a lot of boxes in a array .
The method for the tetris is to create each box having its unique position of i and j

  1. import javax.swing.*;
  2. import java.awt.*;
  3. class Main extends JFrame {
  4. class App extends JPanel {
  5. Grid grid;
  6. public App() {
  7. setPreferredSize(new Dimension(900, 720));
  8. grid = new Grid();
  9. }
  10. @Override
  11. public void paint(Graphics g) {
  12. grid.paint(g, getMousePosition());
  13. }
  14. }
  15. public static void main(String[] args) throws Exception {
  16. Main window = new Main();
  17. window.run();
  18. }
  19. private Main() {
  20. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  21. App canvas = new App();
  22. this.setContentPane(canvas);
  23. this.pack();
  24. this.setVisible(true);
  25. }
  26. public void run() {
  27. while (true) {
  28. this.repaint();
  29. }
  30. }
  31. }
  32. import java.awt.*;
  33. class Grid {
  34. //fields
  35. Cell[][] cells = new Cell[20][20];
  36. // constructor
  37. public Grid(){
  38. for(int i = 0; i &lt; cells.length; i++){
  39. for(int j = 0; j &lt; cells[i].length; j++){
  40. cells[i][j] = new Cell(10+35*i,10+35*j);
  41. }
  42. }
  43. }
  44. // methods
  45. public void paint(Graphics g, Point mousePos){
  46. for(int i = 0; i &lt; cells.length; i++){
  47. for(int j = 0; j &lt; cells[i].length; j++){
  48. cells[i][j].paint(g, mousePos);
  49. }
  50. }
  51. }
  52. }
  53. import java.awt.*;
  54. import java.util.Random;
  55. class Cell extends Rectangle {
  56. // fields
  57. int random_int;
  58. int minyellow = 50;
  59. int maxyellow = 100;
  60. static int size = 35;
  61. //constructors
  62. public Cell(int x, int y){
  63. super(x,y,size,size);
  64. }
  65. //methods
  66. void paint(Graphics g, Point mousePos){
  67. int random_int = (int)(Math.random() * (maxyellow - minyellow + 1) + minyellow);
  68. this.random_int=random_int;
  69. if(contains(mousePos)){
  70. g.setColor(Color.GRAY);
  71. }
  72. else {
  73. g.setColor(Color.getHSBColor(255, random_int, 60));
  74. }
  75. g.fillRect(x,y,size,size);
  76. g.setColor(Color.BLACK);
  77. g.drawRect(x,y,size,size);
  78. }
  79. public boolean contains(Point p){
  80. if (p != null){
  81. return super.contains(p);
  82. } else {
  83. return false;
  84. }
  85. }
  86. }

答案1

得分: 0

在你的代码中存在一个问题,就是你在Cellpaint方法内部为颜色选择随机数,因此每次绘制时都会得到一个新的随机颜色。

为了使单元格具有稳定的颜色,最简单的修复方法是在构造函数中一次性设置random_int,而不进行更新。

  1. public Cell(int x, int y) {
  2. super(x, y, size, size);
  3. random_int = (int) (Math.random() * (maxyellow - minyellow + 1) + minyellow);
  4. }
  5. void paint(Graphics g, Point mousePos) {
  6. if (contains(mousePos)) {
  7. g.setColor(Color.GRAY);
  8. }
  9. else {
  10. g.setColor(Color.getHSBColor(255, random_int, 60));
  11. }
  12. g.fillRect(x,y,size,size);
  13. g.setColor(Color.BLACK);
  14. g.drawRect(x,y,size,size);
  15. }
英文:

A problem in your code is that you pick the random number for your colour inside Cell's paint method, so every time it is painted, it gets a new random colour.

For a cell to have a stable colour, the simplest fix is to set random_int once, in the constructor, and not update it.

  1. public Cell(int x, int y) {
  2. super(x, y, size, size);
  3. random_int = (int) (Math.random() * (maxyellow - minyellow + 1) + minyellow);
  4. }
  5. void paint(Graphics g, Point mousePos) {
  6. if (contains(mousePos)) {
  7. g.setColor(Color.GRAY);
  8. }
  9. else {
  10. g.setColor(Color.getHSBColor(255, random_int, 60));
  11. }
  12. g.fillRect(x,y,size,size);
  13. g.setColor(Color.BLACK);
  14. g.drawRect(x,y,size,size);
  15. }

huangapple
  • 本文由 发表于 2020年8月19日 07:41:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63478113.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定