gpt4 book ai didi

java - 打破网格线

转载 作者:行者123 更新时间:2023-12-01 15:26:36 25 4
gpt4 key购买 nike

网格看起来像这样。 http://a4.sphotos.ak.fbcdn.net/hphotos-ak-prn1/s320x320/532063_426018057411895_100000111130260_1879195_1266764275_n.jpg

我在循环中插入了 if 语句来绘制线条以进行换行。然而,程序的运行就好像 if 语句不存在一样。if 语句还不够吗?

import java.awt.*;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import javax.swing.*;

public class Grid extends Canvas{

Cell[][] maze;
int rows;
int cols;
int pathSize;
double width, height;


public Grid(int rows, int cols, int h, int w) {
this.rows = rows;
this.cols = cols;
maze = new Cell[rows][cols];
setPreferredSize(new Dimension(h,w));
}


public static void main(String[] args){
JFrame y = new JFrame();
y.setLayout(new BorderLayout());
Grid f = new Grid(25,25,400,400);
y.add(f, BorderLayout.CENTER);
y.setSize(450,450);
y.setVisible(true);
y.setDefaultCloseOperation(y.EXIT_ON_CLOSE);

}
public void paint(Graphics g) {
int k,j,z=0;
width = getSize().width;
height = getSize().height;

double htOfRow = height / (rows);
for (k = 0; k < rows; k++)
for(j=0; j< rows+1; j++){
if(j!=3){
g.drawLine(z, (int) (k * htOfRow) , (int) (j*(width/rows)) , (int) (k * htOfRow) );
z=(int)(j*(width/rows));
}}
double wdOfRow = width / (cols);
for (k = 0; k < cols; k++)
for(j=0; j< cols+1; j++){
if(j!=3){
g.drawLine((int) (k*wdOfRow) , z,(int) (k*wdOfRow) , (int) (j*(height/cols)));
z=(int)(j*(height/cols));}}
}
}

class Cell{}

最佳答案

您的绘图循环中存在一些逻辑错误。我认为这应该有效:

import java.awt.*;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import javax.swing.*;

public class Grid extends Canvas {

Cell[][] maze;
int rows;
int cols;
int pathSize;
double width, height;

public Grid(int rows, int cols, int h, int w) {
this.rows = rows;
this.cols = cols;
maze = new Cell[rows][cols];
setPreferredSize(new Dimension(h, w));
}

public static void main(String[] args) {
JFrame y = new JFrame();
y.setLayout(new BorderLayout());
Grid f = new Grid(40, 25, 400, 400);
y.add(f, BorderLayout.CENTER);
y.setSize(450, 450);
y.setVisible(true);

}

public void paint(Graphics g) {
int k, j, z = 0;
width = getSize().width;
height = getSize().height;

double htOfRow = height / (rows);
double wdOfRow = width / (cols);

for (j = 0; j < rows; j++) {
for (k = 0; k < cols; k++) {
g.drawLine((int) (k * wdOfRow), (int) (j * htOfRow), (int) (k * wdOfRow), (int) ((j+1) * htOfRow));
}
}
for (j = 0; j < rows; j++) {
for (k = 0; k < cols; k++) {
g.drawLine((int) (k * wdOfRow), (int) (j * htOfRow), (int) ((k+1) * wdOfRow), (int) (j * htOfRow));
}
}
}

class Cell {
}
}

关于java - 打破网格线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10067178/

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