2017-03-02 66 views
0

我想知道如何绘制多行,看起来像分割面板分成部门。如何绘制多个行看起来像分割面板的Java的部门?

This is the example of lines that I wanted to draw

下面是到目前为止,我想通了,但它只能画“X”行的代码,并在面板上的一个水平线上。我想知道我如何画上面的图像。

public void paintComponent(Graphics g){ 
    super.paintComponent(g); 

    Graphics2D graphic = (Graphics2D)g; 
    Insets insets = getInsets(); 
    graphic.setStroke(new BasicStroke(5.0f)); 
    graphic.draw(new Line2D.Double(insets.left, insets.top,getWidth()-insets.right, getHeight()-insets.bottom)); 
    graphic.draw(new Line2D.Double(insets.left,getHeight()-insets.bottom,getWidth()-insets.right,insets.top)); 
    graphic.drawLine(0,200,800,200); 


} 

谢谢。

+0

我首先想到的是,基本上是计算各地根据给定角圆点,并在二者之间画线 - 听起来很蠢,但我会给你更好的基本控制过程。从概念上讲,像[此为示例](http://stackoverflow.com/questions/30228146/drawing-a-line-maximum-point/30228270#30228270) – MadProgrammer

回答

1

有可能是你能做到这一点的几种方式,但对我来说,这看起来像一个轮辐,因为我知道如何计算一个点一个圆圈,这就是我会回落到。

什么做的,我们知道:

  • 我们了解该地区(面板的尺寸)
  • 段的数量,我们希望
  • 如何在一个圆圈计算点/司

因此,通过这些基本信息,我们可以设计出需要在中心点周围均匀划分分割数所需的角度增量

Overshoot

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.geom.Line2D; 
import java.awt.geom.Point2D; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class Test { 

    public static void main(String[] args) { 
     new Test(); 
    } 

    public Test() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     public TestPane() { 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(200, 200); 
     } 

     protected Point2D pointAt(double radians, double radius) { 
      double x = radius * Math.cos(radians); 
      double y = radius * Math.sin(radians); 

      return new Point2D.Double(x, y); 
     } 

     protected Point2D translate(Point2D point, Point2D to) { 
      Point2D newPoint = new Point2D.Double(point.getX(), point.getY()); 
      newPoint.setLocation(point.getX() + to.getX(), point.getY() + to.getY()); 
      return newPoint; 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Graphics2D g2d = (Graphics2D) g.create(); 
      g2d.setColor(Color.BLACK); 

      double startAngle = 0; 
      double divisions = 12; 
      double delta = 360.0/divisions; 

      int centerX = getWidth()/2; 
      int centerY = getHeight()/2; 
      int radius = Math.min(centerX, centerY) * 2; // Overshoot the visible bounds 

      Point2D centerPoint = new Point2D.Double(centerX, centerY); 
      double angle = startAngle; 
      for (int index = 0; index < divisions; index++) { 
       Point2D point = pointAt(Math.toRadians(angle), radius); 
       point = translate(point, centerPoint); 
       g2d.draw(new Line2D.Double(centerPoint, point)); 
       angle += delta; 
      } 

      g2d.dispose(); 
     } 

    } 

} 

现在,如果你不想让行 “超调”,然后更改

int radius = Math.min(centerX, centerY) * 2; // Overshoot the visible bounds 

int radius = Math.min(centerX, centerY); 

Undershoot

现在,如果你希望它看起来有点“更好”,你会发现ð考虑加入

RenderingHints hints = new RenderingHints(
     RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
g2d.setRenderingHints(hints); 

paintComponent方法,你画什么之前

相关问题