Java 绘制网格问题
我想在Java中创建一个简单的网格,作为使用分幅的地图编辑器。在我的渲染方法中,我将Java 绘制网格问题,java,java-2d,Java,Java 2d,我想在Java中创建一个简单的网格,作为使用分幅的地图编辑器。在我的渲染方法中,我将 public void render(Graphics g){ renderGrid(g,96,64); } private void renderGrid(Graphics g,int width, int height) { g.setColor(Color.white); for (int y = 0; y < height; y += 32){ for
public void render(Graphics g){
renderGrid(g,96,64);
}
private void renderGrid(Graphics g,int width, int height) {
g.setColor(Color.white);
for (int y = 0; y < height; y += 32){
for (int x = 0;x<width; x+= 32){
g.drawRect(x, y, x+32, y+32);
System.out.println("x =" + x + " y= " + y + " width= " + (x + 32 - x) + " height=" + (y + 32 - y)+ " column No= " + x/32 + " row No= " + y / 32);
}
}
}
有什么建议吗?
为什么单元格不是正方形?您的问题是
drawRect
开始位置和大小,而不是开始位置和结束位置。因此,它应该是:-
//drawRect(int x, int y, int width, int height)
g.drawRect(x, y, 32, 32);
另外
这个
(x+32-x)
和这个(y+32-y)
与(32)
相同,问题就在这里:
g.drawRect(x, y, x+32, y+32);
应该是:
g.drawRect(x, y, 32, 32);
第三和第四个参数是宽度和高度,而不是“绘制到该点”。可视信息:
import java.awt.*;
import javax.swing.*;
class GridCells {
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
JOptionPane.showMessageDialog(null, new GridCellPanel());
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency
SwingUtilities.invokeLater(r);
}
}
class GridCellPanel extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(Color.BLACK);
render(g);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 100);
}
public void render(Graphics g) {
renderGrid(g, 96, 64);
}
private void renderGrid(Graphics g, int width, int height) {
g.setColor(Color.white);
for (int y = 0; y < height; y += 32) {
for (int x = 0; x < width; x += 32) {
g.drawRect(x, y, 32, 32);
System.out.println("x =" + x + " y= " + y + " width= " +
(x + 32 - x) + " height=" + (y + 32 - y) +
" column No= " + x / 32 + " row No= " + y / 32);
}
}
}
}
import java.awt.*;
导入javax.swing.*;
类网格单元{
公共静态void main(字符串[]args){
Runnable r=新的Runnable(){
@凌驾
公开募捐{
showMessageDialog(null,新的GridCellPanel());
}
};
//应在EDT上创建和更新Swing GUI
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency
SwingUtilities.invokeLater(r);
}
}
类GridCellPanel扩展了JPanel{
@凌驾
公共组件(图形g){
超级组件(g);
挫折背景(颜色:黑色);
渲染(g);
}
@凌驾
公共维度getPreferredSize(){
返回新维度(200100);
}
公共空间渲染(图形g){
renderGrid(g,96,64);
}
私有void renderGrid(图形g、整型宽度、整型高度){
g、 setColor(Color.white);
对于(int y=0;y
在接受答案后我不确定您是否希望填写整个屏幕,所以在看到接受答案之前,我正在处理这个问题。我会坚持下去的,以防这就是你要找的
不完全确定您的代码逻辑,但我提出了自己的逻辑,似乎更容易遵循
private static final int SCREEN_SIZE = 300;
private static final int G_W = 30;
private static final int G_H = 30;
屏幕大小
除以正方形的宽度/高度,得到行数和列数
int columns = SCREEN_WIDTH / G_W;
int rows = SCREEN_WIDTH / G_H;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
g.drawRect(x, y, G_W, G_H);
x += G_W;
}
y += G_H;
x = 0;
}
for(int i=0;i
完整代码
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Grid extends JPanel{
private static final int SCREEN_SIZE = 300;
private static final int G_W = 30;
private static final int G_H = 30;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int x = 0;
int y = 0;
int columns = SCREEN_SIZE / G_W;
int rows = SCREEN_SIZE / G_W;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
g.drawRect(x, y, G_W, G_H);
x += G_W;
}
y += G_H;
x = 0;
}
}
public Dimension getPreferredSize() {
return new Dimension(SCREEN_SIZE, SCREEN_SIZE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.add(new Grid());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
导入java.awt.Dimension;
导入java.awt.Graphics;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.SwingUtilities;
公共类网格扩展JPanel{
专用静态最终整型屏幕尺寸=300;
专用静态最终整数G_W=30;
专用静态最终整数G_H=30;
受保护组件(图形g){
超级组件(g);
int x=0;
int y=0;
int columns=屏幕大小/G\u W;
int rows=屏幕大小/G\U W;
对于(int i=0;i
结果
我假设有一个由正方形组成的正常网格“有什么建议吗?”1)在问题中不包括特定于时间的称呼。2) 为了更快地获得更好的帮助,请发布一个。3) 请在句子开头加一个大写字母。也可以用大写字母表示I,缩写和首字母缩略词如JEE或WAR。这使得人们更容易理解和帮助。4) “谢谢”是噪音。别提了。5) 解释你预期会发生什么。“我想有一个由正方形组成的标准网格”,这是最好的信息。。事实上,我刚刚做到了。请注意其余的建议。您在代码中添加了调试消息;我们可以看一下输出吗?。但是请以后发布一个SSCCE(就像我回答的代码一样),我可以在你问我是否有SSCCE后一分钟左右回答这个问题。@mKorbel谢谢。)
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Grid extends JPanel{
private static final int SCREEN_SIZE = 300;
private static final int G_W = 30;
private static final int G_H = 30;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int x = 0;
int y = 0;
int columns = SCREEN_SIZE / G_W;
int rows = SCREEN_SIZE / G_W;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
g.drawRect(x, y, G_W, G_H);
x += G_W;
}
y += G_H;
x = 0;
}
}
public Dimension getPreferredSize() {
return new Dimension(SCREEN_SIZE, SCREEN_SIZE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.add(new Grid());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}