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

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

Java why does cell keep changing colors

问题

import javax.swing.*;
import java.awt.*;

class Main extends JFrame {

    class App extends JPanel {

        Grid grid;

        public App() {
            setPreferredSize(new Dimension(900, 720));
            grid = new Grid();
        }

        @Override
        public void paint(Graphics g) {
            grid.paint(g, getMousePosition());
        }

    }

    public static void main(String[] args) throws Exception {
        Main window = new Main();
        window.run();
    }

    private Main() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        App canvas = new App();
        this.setContentPane(canvas);
        this.pack();
        this.setVisible(true);
    }

    public void run() {
        while (true) {
            this.repaint();
        }
    }
}

import java.awt.*;

class Grid {
    // fields
    Cell[][] cells = new Cell[20][20];

    // constructor
    public Grid() {
        for (int i = 0; i < cells.length; i++) {
            for (int j = 0; j < cells[i].length; j++) {
                cells[i][j] = new Cell(10 + 35 * i, 10 + 35 * j);
            }
        }
    }

    // methods
    public void paint(Graphics g, Point mousePos) {
        for (int i = 0; i < cells.length; i++) {
            for (int j = 0; j < cells[i].length; j++) {
                cells[i][j].paint(g, mousePos);
            }
        }
    }
}

import java.awt.*;
import java.util.Random;

class Cell extends Rectangle {
    // fields
    int random_int;
    int minyellow = 50;
    int maxyellow = 100;
    static int size = 35;

    // constructors
    public Cell(int x, int y) {
        super(x, y, size, size);
    }

    // methods

    void paint(Graphics g, Point mousePos) {
        int random_int = (int) (Math.random() * (maxyellow - minyellow + 1) + minyellow);
        this.random_int = random_int;

        if (contains(mousePos)) {
            g.setColor(Color.GRAY);
        } else {
            g.setColor(Color.getHSBColor(255, random_int, 60));
        }
        g.fillRect(x, y, size, size);
        g.setColor(Color.BLACK);
        g.drawRect(x, y, size, size);
    }

    public boolean contains(Point p) {
        if (p != null) {
            return super.contains(p);
        } else {
            return false;
        }
    }
}
英文:

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

import javax.swing.*;
import java.awt.*;
class Main extends JFrame {
class App extends JPanel {
Grid grid;
public App() {
setPreferredSize(new Dimension(900, 720));
grid = new Grid();
}
@Override
public void paint(Graphics g) {
grid.paint(g, getMousePosition());
}
}
public static void main(String[] args) throws Exception {
Main window = new Main();
window.run();
}
private Main() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
App canvas = new App();
this.setContentPane(canvas);
this.pack();
this.setVisible(true);
}
public void run() {
while (true) {
this.repaint();
}
}
}
import java.awt.*;
class Grid {
//fields
Cell[][] cells = new Cell[20][20];
// constructor
public Grid(){
for(int i = 0; i &lt; cells.length; i++){
for(int j = 0; j &lt; cells[i].length; j++){
cells[i][j] = new Cell(10+35*i,10+35*j);
}
}
}
// methods
public void paint(Graphics g, Point mousePos){
for(int i = 0; i &lt; cells.length; i++){
for(int j = 0; j &lt; cells[i].length; j++){
cells[i][j].paint(g, mousePos);
}
}
}
}
import java.awt.*;
import java.util.Random;
class Cell extends Rectangle {
// fields
int random_int;
int minyellow = 50;
int maxyellow = 100;
static int size = 35;
//constructors
public Cell(int x, int y){
super(x,y,size,size);
}
//methods
void paint(Graphics g, Point mousePos){      
int random_int = (int)(Math.random() * (maxyellow - minyellow + 1) + minyellow);
this.random_int=random_int;
if(contains(mousePos)){
g.setColor(Color.GRAY);
} 
else {
g.setColor(Color.getHSBColor(255, random_int, 60));
}
g.fillRect(x,y,size,size);
g.setColor(Color.BLACK);
g.drawRect(x,y,size,size);
}
public boolean contains(Point p){
if (p != null){
return super.contains(p);
} else {
return false;
}
}
}

答案1

得分: 0

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

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

public Cell(int x, int y) {
    super(x, y, size, size);
    random_int = (int) (Math.random() * (maxyellow - minyellow + 1) + minyellow);
}

void paint(Graphics g, Point mousePos) {      
    if (contains(mousePos)) {
        g.setColor(Color.GRAY);
    } 
    else {
        g.setColor(Color.getHSBColor(255, random_int, 60));
    }
    g.fillRect(x,y,size,size);
    g.setColor(Color.BLACK);
    g.drawRect(x,y,size,size);
}
英文:

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.

public Cell(int x, int y) {
super(x, y, size, size);
random_int = (int) (Math.random() * (maxyellow - minyellow + 1) + minyellow);
}
void paint(Graphics g, Point mousePos) {      
if (contains(mousePos)) {
g.setColor(Color.GRAY);
} 
else {
g.setColor(Color.getHSBColor(255, random_int, 60));
}
g.fillRect(x,y,size,size);
g.setColor(Color.BLACK);
g.drawRect(x,y,size,size);
}

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:

确定