2017-05-02 3 views
-1

Javaでは、線2Dを使用して点を連結して閉じた形を作成します。どのように塗ることができますか/色で塗りつぶしますか?Line2Dで作成された塗りつぶしの形の色

+0

コードを入力してください。 – user7185318

+0

まず、[AWT and Swingのペイント](http://www.oracle.com/technetwork/java/painting-140037.html)と[カスタム・ペイントの実行(https://docs.oracle.com/)を参照してください。 .com/javase/tutorial/uiswing/painting /)と[2D Graphics](https://docs.oracle.com/javase/tutorial/2d/)を試してみてください。あなたが試したことがある特定の問題がある場合は、あなたが試したこととそれがあなたのために働いていない理由を提供することをためらうことを躊躇しないでください – MadProgrammer

答えて

-1

私はあなたのコードが何であるか知らないが、私はあなたがこのようになりますクラスがあると仮定:

あなたは

public class App extends JFrame{ 
    public App() { 
     super("Paintings"); 
     requestFocus(); 
     DrawingPane dpane=new DrawingPane(); 
     setContentPane(dpane); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setSize(600, 600); 
     setResizable(true); 
     setVisible(true); 
     long start = System.currentTimeMillis(); 
     while (true){ 
      long now = System.currentTimeMillis(); 
      if (now-start > 10) { //FPS 
       start=now; 
       dpane.revalidate(); 
       dpane.repaint(); 
      } 
     } 
    } 
    public static void main(String[] args) { 
     new App(); 
    } 
    class DrawingPane extends JPanel{ 
     @Override 
     public void paintComponent(Graphics g2){ 
      Graphics2D g=(Graphics2D)g2; 
      g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
      g.setColor(Color.BLACK); 
      //-THE DRAWING- 
      //You wont need any lines 
      Point[] points=new Point[] {new Point(0,0),new Point(1,0),new Point(1,1)}; 
      int[] points_x=new int[points.length]; 
      int[] points_y=new int[points.length]; 
      for (int p=0; p < points.length; p++) { 
       points_x[p]=points[p].x; 
       points_y[p]=points[p].y; 
      } 
      g.drawPolygon(points_x,points_y,points.length); //Draw the outlines 
      g.fillPolygon(points_x,points_y,points.length); //Filled Polygon 
     } 
    } 
} 
1

まずfillPolygon(int[] xpoints, int[] ypoints, int nPoints)方法を探しているが、追加することで、1つのシェイプを作成しますPath2Dにあなたのライン:次に

private Path2D createSingleShape(Line2D[] lines) { 
    Path2D path = new Path2D.Float(); 

    for (Line2D line : lines) { 
     path.append(line, path.getCurrentPoint() != null); 
    } 

    path.closePath(); 

    return path; 
} 

Graphics2D.fill(Shape)に渡し:

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

    Graphics2D g = (Graphics2D) graphics; 

    Shape shape = createSingleShape(lines); 
    g.fill(shape); 
} 
関連する問題