我需要帮助改变网格中矩形的颜色。到目前为止,我有矩形和网格(这是绘制分隔矩形的线条),我想在单击时更改单个矩形的颜色。
我在颜色应该改变的循环中添加了“System.out.println(”Something“)”行,它总是返回“Something”。这就是为什么我很困惑。感谢您的帮助!
类网格:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.Timer;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Grid extends JPanel{
private int a = 50;
private int b = 50;
private Lifeform[][] Life;
private boolean[][]life = new boolean[a][b];
private Timer t;
Grid(){
//creates grid of rectangles
Life = new Lifeform[a][b];
int ypos = 0;
for(int i = 0; i < Life.length; i++){
int xpos = 0;
for(int j = 0; j < Life[0].length; j++){
Rectangle r = new Lifeform();
r.setBounds(xpos, ypos, 50, 50);
Life[i][j] = (Lifeform) r;
xpos += 50;
}
ypos += 50;
}
t = new Timer(64, new Movement());
this.addMouseListener(new mouse());
}
public void paintComponent(Graphics g){
for(Lifeform[] n : Life){
//makes rectangles white
g.fillRect(this.getX(), this.getY(), this.getWidth(), this.getHeight());
g.setColor(Color.white);
}
for (int i = 0; i <= 25; i++){
g.drawLine(0, 50*i, 1500, 50*i);
g.setColor(Color.black);
}
for (int i = 0; i <= 25; i++){
g.drawLine(50*i, 0, 50*i, 750);
g.setColor(Color.black);
}
}
private JFrame createGrid(){
JFrame frame = new JFrame("Alveolate");
frame.add(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700,700);
frame.setVisible(true);
return frame;
}
public class mouse implements MouseListener{
//colors rectangles (it doesn't work)
public void mouseClicked(MouseEvent e) {
Point p = new Point(e.getX(),e.getY());
for(int i = 0; i < Life.length; i++){
for(int j = 0; j < Life[i].length; j++){
Lifeform spot = Life[i][j];
if (spot.contains(p)){
spot.setColor(Color.red);
System.out.println("Something");
}
}
}
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
public class Movement implements ActionListener{
public void actionPerformed ( ActionEvent e ){
for (int i = 0; i < Life.length; i++){
for (int j = 0; j < Life[i].length; j++){
if(Life[i][j].getColor().equals(Color.black)){
life[i][j] = true;
}
else{
life[i][j] = false;
}
}
}
repaint();
}
}
public void startTimer(){
t.start();
}
public void stopTimer(){
t.stop();
}
public static void main(String[] args) {
Grid ABC = new Grid();
ABC.createGrid();
ABC.startTimer();
}
}
Class Lifeform:
import java.awt.Color; import java.awt.Rectangle;
public class Lifeform extends Rectangle {
private Color c;
public Lifeform() {
c = Color.WHITE;
}
public Color getColor() {
return c;
}
public boolean setColor( Color c ) {
boolean rtn = false;
if( c != null ) {
this.c = c;
rtn = true;
}
return rtn;
}
}
答案 0 :(得分:0)
试试这个:
for(int i = 0; i < 50; i++){
for(int j = 0; j < 50; j++){
....
}
}
答案 1 :(得分:0)
试
for(Lifeform[] n : Life){
for(Lifeform lf : n){
g.setColor(lf.getColor());
g.fillRect((int)lf.getX(), (int)lf.getY(), (int)lf.getWidth(), (int)lf.getHeight());
}
}
在paintComponent方法中
答案 2 :(得分:0)
没有阅读所有代码,但似乎你肯定将Lifeform的颜色设置为红色。这意味着您的渲染代码实际上从未使用此事实来显示Lifeform。看起来你唯一一次绘制矩形是在paintComponent中。每次想要重绘时都需要调用此画图。您可以在JFrame或JPanel上使用重绘来实现此目的。此外,你在那里的渲染代码似乎只显示白色矩形
url(r'^accounts/login/$')
这是在你的Lifeform [] []循环中。相反,你应该有一个双循环来获得每个Lifeform并使用x和y以及颜色来绘制。类似的东西:
g.fillRect(this.getX(), this.getY(), this.getWidth(), this.getHeight());
g.setColor(Color.white);
此外,生命应该用小写的'l'命名。看到没有惯例的名字会让人感到困惑。