2016-03-07 77 views
1

我寫了一個代碼來繪製一個六角形。首先我使用這個公式繪製6點: (x + r * cos(i * 2 * pi/6),y + r * sin(i * 2 * pi/6)) 然後我繪製這些點後i試圖使用Bresnham的算法繪製線,我已經在一個方法中實現它之間的點之間進行匹配。 不幸的是,代碼無法成功工作,我得到這個而不是六角形。我認爲Bresnham的算法實現存在錯誤。此外,我試圖單獨繪製每個點,但它不會工作。 如果有人能幫助我?繪製六角形使用Java錯誤

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); 
      } 
     }); 
    } 
} 
+0

覆蓋'paintComponent(g)',而不是'paint(g)'。 – user3437460

+0

@ user3437460我試圖覆蓋paintComponent(g),但它不包含在要覆蓋的方法中。 –

+0

看看我的解決方案。 – user3437460

回答

2

你是在JFrame的繪圖。切勿畫JFrame。總是使用JPanel。

這是GUI。

Hexagon

這裏是我做了重大變化。

  1. 我將Hexagon的創建轉移到了它自己的類Hexagon中。這樣,你可以創建一個六邊形列表,如果你想。

  2. 我將創建繪圖面板移動到它自己的類DrawingPanel中。這樣,我有一個繪製六邊形的GUI視圖類和一個生成六邊形的GUI模型類。一個很好的,清晰的關注點分離。

  3. 這留下了JFrame代碼和在LinesDrawingExample類的構造函數中的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; 
     } 

    } 
} 
3

你不應該重寫paint(g)。改爲覆蓋paintComponent(g)。您可以使用循環繪製多邊形所需的所有點。

情節點,並創建一個多邊形對象,然後繪製多邊形對象:

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