gpt4 book ai didi

Java,对象网格

转载 作者:行者123 更新时间:2023-11-29 05:47:51 25 4
gpt4 key购买 nike

我需要帮助将网格绘制到 GUI 以及稍后让我更改绘制的框的颜色的程序。我知道我将不得不使用 paintComponent(Graphics g),但我不知道如何使用或在何处使用。

所以这里是我目前得到的代码的副本(尽管我被告知仅仅获得代码可能会让人望而生畏,但我认为这是人们提供帮助的最佳方式,而不仅仅是为我做这件事).它从顶部设置值,创建 GUI,调用 GUI,用框填充二维数组(我认为)。然后在 Boxes 类中设置 boxes 类需要的值,然后开始如何绘制它们(不知道如何解决),然后是 x 和 y 坐标的一些 seta 方法。

我希望您做的是展示如何将框绘制到 Jpanel,制作网格,然后展示如何根据外部值将颜色更改为不同深浅的蓝色。

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

public class NewGrid {

Boxes[][] Boxs;
int BoxesX;
int BoxesY;

NewGrid() {
buildtheGUI();

}
JFrame frame = new JFrame();
JPanel panel = new JPanel();

public void buildtheGUI() {
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.setVisible(true);
}

public static void main(String[] args) {
new NewGrid();
}

public void addboxes() {
Boxs = new Boxes[panel.getWidth() / 10][panel.getHeight() / 10];
for (int i = 0; i < panel.getWidth() / 10; i++) {
for (int j = 0; j < panel.getHeight() / 10; j++) {
Boxs[i][j] = new Boxes();
Boxs[i][j].setx(i * (panel.getWidth() / 10));
Boxs[i][j].sety(j * (panel.getHeight() / 10));
Boxs[i][j].draw(null);
}

}
}
}


public class Boxes extends JPanel {
int x;
int y;
int width = 10;
int hieight = 10;
Color colour = Color.BLACK;




public void draw(Graphics g) {
g.setColor(colour);
g.fillRect(x, y, width, hieight);
}
public void setx(int i ){
x = i;
}
public void sety(int i ){
y = i;
}


}

最佳答案

.

JComponent comp = event.getComponent();
String strRow = (String) comp.getClientProperty("row");
String strColumn = (String) comp.getClientProperty("column");

简单的代码

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

public class NewGrid {

private int row = 10;
private int column = 10;
private JFrame frame = new JFrame();
private JPanel panel = new JPanel();

private NewGrid() {
addboxes();
panel.setLayout(new GridLayout(row, column));
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

private void addboxes() {
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
Boxes box = new Boxes();
box.putClientProperty("row", row);
box.putClientProperty("column", column);
panel.add(box);
}
}
}

public static void main(String[] args) {
Runnable doRun = new Runnable() {
@Override
public void run() {
new NewGrid();
}
};
SwingUtilities.invokeLater(doRun);
}
}

class Boxes extends JPanel {

private static final long serialVersionUID = 1L;

@Override
public Dimension getMinimumSize() {
return new Dimension(20, 20);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(20, 20);
}

@Override
public Dimension getMaximumSize() {
return new Dimension(20, 20);
}

@Override
public void paintComponent(Graphics g) {
int margin = 2;
Dimension dim = getSize();
super.paintComponent(g);
g.setColor(Color.red);
g.fillRect(margin, margin, dim.width - margin * 2,
dim.height - margin * 2);
}
}

关于Java,对象网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15145061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com