Pour une affectation, je devais dessiner une grille hexagonale. J'ai été capable de l'obtenir, mais la fenêtre ne montrerait que les 2 premières rangées (sur 7) horizontalement, mais ne montrerait pas tout verticalement. La grille n'est pas entièrement affichée dans la fenêtre et je me demande pourquoi il le fait.Java Graphics Fenêtre/Canevas ne montrant pas la grille hexagonale complète verticalement
import java.awt.*;
import java.awt.event.*;
import static java.lang.Math.sqrt;
public class hex extends Frame {
public static void main(String[] args) {
new hex();
}
public hex(){ //the class for the frame, also to close the application
super("hex");
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);}
});
setSize(600,400);
add("Center", new CvHex());
show();
}
}
class CvHex extends Canvas{
int centerX, centerY;
float pixelSize, rWidth = 10.0F, rHeight = 10.0F, xP = 1000000, yP;
CvHex() {
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent evt){
xP = fx(evt.getX()); yP = fy(evt.getY());
repaint();
}
});
}
void initgr(){
Dimension d = getSize();
int maxX = d.width-1, maxY = d.height-1;
pixelSize = Math.max(rWidth/maxX, rHeight/maxY);
centerX = maxX/2; centerY = maxY/2;
}
int iX(float x){ return
Math.round(centerX + x/pixelSize);} //114
float fx(int x){
return (x-centerX)*pixelSize;}
int iY(float y){
return Math.round(centerY - y/pixelSize);}
float fy(int y){
return (centerY - y)*pixelSize; }
public void paint(Graphics g){
initgr();
int left = iX(-rWidth/2), right = iX(rWidth/2), bottom = iY(-rHeight/2),
top = iY(rHeight/2), xMiddle = iX(0), yMiddle = iY(0);
//to rotate to flat
right = ((right - xMiddle)/2) + xMiddle;
left = ((xMiddle - left)/2) + left;
top = ((yMiddle - top)/2) + top;
bottom = ((bottom - yMiddle)/2) + yMiddle;
int side = (right - xMiddle)/2;
int rightmost = (int)sqrt((side * side) + (yMiddle * yMiddle));
rightmost = (rightmost/2) + right;
int leftmost = side;
leftmost = left - (2*leftmost);
int totalL = rightmost - leftmost;
//for the offset (side) hexagon
int lM, rM, sTop, sBottom, sY, sLeft, sRight;
sTop = yMiddle; //should be good
sBottom = yMiddle * 2; //should be good
lM = right; //good
sY = bottom; //good
sLeft = rightmost; //good
sRight = rightmost + (4*side);
int length = sLeft - lM;
rM = sRight + length;
for (int i = 0; i < 7; i++){
//first hexagon
g.drawLine(right, top, rightmost, yMiddle);
g.drawLine(right, bottom, rightmost, yMiddle);
g.drawLine(left, top, leftmost, yMiddle);
g.drawLine(left, bottom, leftmost, yMiddle);
g.drawLine(left, bottom, right, bottom); //x, y, w, h
g.drawLine(right, top, left, top);
//for the bottom hexagon
int dMiddle = yMiddle * 2;
int dTop = bottom;
int dBottom = bottom * 2;
g.drawLine(right, dTop, rightmost, dMiddle);
g.drawLine(right, dBottom, rightmost, dMiddle);
g.drawLine(left, dTop, leftmost, dMiddle);
g.drawLine(left, dBottom, leftmost, dMiddle);
g.drawLine(left, dBottom, right, dBottom); //x, y, w, h
g.drawLine(right, dTop, left, dTop);
//offset right
leftmost = rightmost + (4*side);
left = right + totalL;
right = left + (4*side);
rightmost = right + length;
}
//for the offset side column
for (int j = 0; j < 7; j++){
g.drawLine(sRight, sTop, rM, sY);
g.drawLine(sRight, sBottom, rM, sY);
g.drawLine(sLeft, sTop, lM, sY);
g.drawLine(sLeft, sBottom, lM, sY);
g.drawLine(sLeft, sBottom, sRight, sBottom); //x, y, w, h
g.drawLine(sRight, sTop, sLeft, sTop);
int oY = sY * 2;
int oTop = sBottom;
int oBottom = sBottom * 2;
g.drawLine(sRight, oTop, rM, oY);
g.drawLine(sRight, oBottom, rM, oY);
g.drawLine(sLeft, oTop, lM, oY);
g.drawLine(sLeft, oBottom, lM, oY);
g.drawLine(sLeft, oBottom, sRight, oBottom); //x, y, w, h
g.drawLine(sRight, oTop, sLeft, oTop);
//offset right
lM = rM + (4*side);
sLeft = sRight + totalL;
sRight = sLeft + (4*side);
rM = sRight + length;
}
}
}
C'est de cela dont je parle. normal:
étiré Verticalement, d'échelles régulière comme il devrait, mais ne pas montrer toutes les formes que je veux qu'il:
Je l'ai regardé ici et ailleurs ce qui pourrait y remédier mais les solutions que j'ai trouvées sont avec Graphics2d et swing, jFrame, etc. Choses que je ne connais pas.
Merci pour les conseils, mais quand vous voulez dire Faites de la distance x et de la distance y indépendamment, vous voulez dire mes maxX et maxy? indépendant de la fenêtre? – CodingNoob
Je veux dire la valeur pixelScale. Si vous voulez que la grille hexagonale corresponde exactement à l'écran, la largeur hexagonale doit correspondre à la largeur de l'écran et vice versa. Vous devez élaborer sur vos besoins. –