java - 使用Java错误绘制六边形

标签 java graphics

我写了一个代码来画一个六边形。首先,我使用这个公式绘制 6 个点: (x + r*cos(i*2*pi/6), y + r*sin(i*2*pi/6)) 然后在我绘制这些点之后,我尝试使用 Bresnham 的算法来绘制我在方法中实现的线条来匹配这些点。 不幸的是,该代码无法成功运行,我得到的是这个而不是六边形。我认为 Bresnham 的算法实现存在错误。此外,我试图单独绘制每个点,但它不会起作用。 如果有人可以帮助我?

enter image description here

这也是我的代码:

package javaapplication1;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.geom.Line2D;
import static java.lang.Math.cos;
import static java.lang.Math.sin;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;


public class LinesDrawingExample extends JFrame {

    public LinesDrawingExample() {
        super("Lines Drawing Demo");

        //Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();

        setSize(500, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    void drawLines(Graphics g , int x,int y) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.RED);


        g2d.drawLine(x, y, x,y);


        //g2d.draw(new Line2D.Double(59.2d, 99.8d, 419.1d, 99.8d));

       // g2d.draw(new Line2D.Float(21.50f, 132.50f, 459.50f, 132.50f));

    }

    public void getvalue(Graphics g,double x1,double y1 ,double x2,double y2){









        int x=(int)x1;
        int y=(int)y1;
        int deltax=(int)x2-(int)x1;
        int deltay=(int)y2-(int)y1;
        int twodeltay=2*deltay;
        int var1=twodeltay-(2*deltax);
        int p=twodeltay-deltax;
        drawLines(g,x,y);


        while(x<x2)
        {
            drawLines(g,x,y);

            if(p>0)
            {
            y=y+1;
            p=p+twodeltay-(2*deltax);

            }
            else
            {
                p=p+twodeltay;
            }
            x++;


}
    }

    public void paint(Graphics g) {
        super.paint(g);
        double r=50;
        double pi=3.14;

        int [] xval =new int [6];
        int [] yval=new int [6];

        int x=100,y=100;

       for(int i=0; i<6; i++) {
   getvalue(g,x + r*cos(i*2*pi/6), y + r*sin(i*2*pi/6),x + r*cos(i*2*pi/6),y + r*cos(i*2*pi/6));
   xval[i]=(int)(x + r*cos(i*2*pi/6));
   yval[i]=(int)(y + r*sin(i*2*pi/6));
       }


           getvalue(g,xval[4],yval[4],xval[5],yval[5]);
           getvalue(g,xval[2],yval[2],xval[1],yval[1]);


             getvalue(g,xval[3],yval[3],xval[2],yval[2]);
              getvalue(g,xval[3],yval[3],xval[3],yval[3]);
               getvalue(g,xval[4],yval[4],xval[4],yval[4]);
              getvalue(g,xval[5],yval[5],xval[5],yval[5]);









    }




    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new LinesDrawingExample().setVisible(true);
            }
        });
    }
}

最佳答案

您正在 JFrame 上绘图。永远不要在 JFrame 上绘制。始终在 JPanel 上绘制。

这是 GUI。

Hexagon

以下是我所做的主要更改。

  1. 我将六边形的创建移到了它自己的类 Hexagon 中。这样,您可以根据需要创建六边形列表。

  2. 我将绘图面板的创建移到了它自己的类 DrawingPanel 中。这样,我就有了一个绘制六边形的 GUI View 类和一个生成六边形的 GUI 模型类。一个漂亮、干净的关注点分离。

  3. 这在 LinesDrawingExample 类的构造函数中留下了 JFrame 代码和 Hexagon 对象的实例化。

这是代码。

package com.ggl.testing;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Polygon;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class LinesDrawingExample extends JFrame {

    private static final long serialVersionUID = 3775690273871048733L;

    private DrawingPanel drawingPanel;

    public LinesDrawingExample() {
        super("Lines Drawing Demo");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Hexagon hexagon = new Hexagon(new Point(250, 250), 200);

        drawingPanel = new DrawingPanel(hexagon);
        add(drawingPanel);

        pack();
        setLocationByPlatform(true);
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new LinesDrawingExample();
            }
        });
    }

    public class DrawingPanel extends JPanel {
        private static final long serialVersionUID = 5701311351092275287L;

        private Hexagon hexagon;

        public DrawingPanel(Hexagon hexagon) {
            this.hexagon = hexagon;
            this.setPreferredSize(new Dimension(500, 500));
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            g.setColor(Color.RED);
            g.drawPolygon(hexagon.getHexagon());
        }
    }

    public class Hexagon {
        private final int radius;

        private final Point center;

        private final Polygon hexagon;

        public Hexagon(Point center, int radius) {
            this.center = center;
            this.radius = radius;
            this.hexagon = createHexagon();
        }

        private Polygon createHexagon() {
            Polygon polygon = new Polygon();

            for (int i = 0; i < 6; i++) {
                int xval = (int) (center.x + radius
                        * Math.cos(i * 2 * Math.PI / 6D));
                int yval = (int) (center.y + radius
                        * Math.sin(i * 2 * Math.PI / 6D));
                polygon.addPoint(xval, yval);
            }

            return polygon;
        }

        public int getRadius() {
            return radius;
        }

        public Point getCenter() {
            return center;
        }

        public Polygon getHexagon() {
            return hexagon;
        }

    }
}

关于java - 使用Java错误绘制六边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35853902/

相关文章:

java - Android Studio 无法在 Mac OSX (Mavericks) 上加载 JVM

c# - 系统.绘图.图形

c# - 如何绘制亚像素线

java - 用于模块化应用程序 (JPA) 的单个持久性单元

java - Hibernate 生成不必要的查询

java - Lombok 发布@Builder 构建事件

java - 屏幕上的按钮不会将我切换到游戏 Pane

algorithm - 任意网格的纹理展开

c# - 如何将多行文本添加到 ListBox 项?

java - Java 中的 PDF 解析器 API