Das Raster sieht so aus. http://a4.sphotos.ak.fbcdn.net/hphotos-ak-prn1/s320x320/532063_426018057411895_100000111130260_1879195_1266764275_n.jpgBreaking Lines vom Raster
Ich habe die if-Anweisung in die Schleife eingefügt, um die Linien zu zeichnen, um einen Zeilenumbruch zu machen. Das Programm funktioniert jedoch so, als ob die if-Anweisung nicht vorhanden wäre. Ist die if-Anweisung nicht genug?
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{}
Der Code, den Sie auskommentiert haben, scheint das Gitter auf die gleiche Weise wie der erste Link in Ihrer Frage zu malen. Ich bin mir nicht sicher, ob ich verstehe, was Sie fragen. – assylias
@assylias - bearbeitet die Frage und Code –