使用Java错误绘制六角形

时间:2016-03-07 20:59:21

标签: 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);
            }
        });
    }
}

2 个答案:

答案 0 :(得分:4)

您不应该覆盖paint(g)。改为覆盖paintComponent(g)。您可以使用循环绘制多边形所需的所有点。

绘制点并创建一个Polygon对象,然后绘制多边形对象:

enter image description here

public class DrawPolyPanel extends JPanel{
    public DrawPolyPanel(){
        setPreferredSize(new Dimension(200, 200));
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);            
        Polygon p = new Polygon();
        for (int i = 0; i < 6; i++)
            p.addPoint((int) (100 + 50 * Math.cos(i * 2 * Math.PI / 6)),
              (int) (100 + 50 * Math.sin(i * 2 * Math.PI / 6)));        
        g.drawPolygon(p);    
    }

    public static void main(String[] args){
        JFrame frame = new JFrame("DrawPoly");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new DrawPolyPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

答案 1 :(得分:3)

您正在使用JFrame。永远不要在JFrame上绘制。总是在JPanel上画画。

这是GUI。

Hexagon

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

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

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

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

  4. 这是代码。

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