Java Graphics Window / Canvas不垂直显示完整的六边形网格

时间:2017-09-09 05:36:17

标签: java canvas graphics

对于作业,我要绘制一个六边形网格。我能够得到它,但窗口只能水平显示前2行(7个中),但不会垂直显示。网格没有完全显示在窗口中,我想知道它为什么这样做。

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;
        }

     }
}

这就是我所说的。 常规:Regular pop-out

延伸:across

垂直拉伸,只是按比例正常缩放,但不显示我想要的所有形状stretched vertically but nothing changes except that it scales the original window instead of fitting all the shapes as I want it to

我在这里和其他地方看过什么可以解决它,但我找到的解决方案是使用Graphics2d和swing,jFrame等。我不熟悉的事情。

1 个答案:

答案 0 :(得分:0)

在这里,您的缩放代码存在许多错误,特别是在线条绘制阶段。我运行它,如果x值非常小,它会完全变形,所以我无法在不重写的情况下对其进行纠正。

一些提示:

  • 确保您独立缩放x距离和y距离,除非六边形必须是对称的,然后计算 min 并从那里开始。
  • 不要打扰使用中心(除非你需要一个对称/无限网格)。将整个屏幕分成X列和Y行,并将其用作宽度/长度。
  • 一旦你有宽度&amp;长度和左上角,你可以从左上角那里制作六边形

    top-left + width/3
    top-left + 2*width/3
    top-left + width + height/2, etc