2009-10-13 2 views
3

각 모양은 5 점 (약 50 점)이며 색상은 알파 투명도입니다. 이 모양을 픽셀 그리드에 렌더링하고 싶습니다.Java에서 렌더링 벡터 모양

나는 아마추어로 프로그램하므로, 어떻게해야하는지 잘 모릅니다.

누군가 나에게 출발점이나 의사 코드를 줄 수 있습니까?

미리 감사드립니다.

답변

12
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.util.List; 
import java.util.ArrayList; 
import java.awt.Point; 
import javax.swing.JFrame; 
import java.awt.Color; 
import java.util.Random; 
import java.awt.Polygon; 
import java.awt.Shape; 


public class GraphicsTest extends JFrame { 

    private List<ColoredShape> shapes; 
    private static final int NUM_SHAPES = 50; 
    private static final int NUM_POINTS_PER_SHAPE = 5; 

    private static final int WIDTH = 640; 
    private static final int HEIGHT = 480; 

    private Random randomGenerator; 

    public GraphicsTest(String title) { 
     super(title); 
     setVisible(true); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setSize(WIDTH, HEIGHT); 


     randomGenerator = new Random(); 

     initShapes(); 
    } 

    private void initShapes() { 
     shapes = new ArrayList<ColoredShape>(NUM_SHAPES); 
     for (int i = 0; i < NUM_SHAPES; i++) { 
      Point[] points = getRandomPoints(); 
      Color color = getRandomColor(); 

      shapes.add(i, new ColoredShape(points, color)); 
     } 

    } 


    private Point[] getRandomPoints() { 
     Point[] points = new Point[NUM_POINTS_PER_SHAPE]; 
     for (int i = 0; i < points.length; i++) { 
      int x = randomGenerator.nextInt(WIDTH); 
      int y = randomGenerator.nextInt(HEIGHT); 
      points[i] = new Point(x, y); 
     } 
     return points; 
    } 

    /** 
    * @return a Color with random values for alpha, red, green, and blue values 
    */ 
    private Color getRandomColor() { 
     float alpha = randomGenerator.nextFloat(); 
     float red = randomGenerator.nextFloat(); 
     float green = randomGenerator.nextFloat(); 
     float blue = randomGenerator.nextFloat(); 

     return new Color(red, green, blue, alpha); 
    } 


    public void paint(Graphics g) { 
     Graphics2D g2 = (Graphics2D) g; 
     for (ColoredShape shape : shapes) { 
      g2.setColor(shape.getColor()); 
      g2.fill(shape.getOutline()); 
     } 
    } 


    public static void main(String[] args) { 
     GraphicsTest b = new GraphicsTest("Testing polygons"); 
    } 


    private class ColoredShape { 

     private Polygon outline; 
     private Color color; 

     public ColoredShape(Point[] points, Color color) { 
      this.color = color; 
      // Would be better to separate out into xpoints, ypoints, npoints 
      // but I'm lazy 
      outline = new Polygon(); 
      for (Point p : points) { 
       outline.addPoint((int) p.getX(), (int) p.getY()); 
      } 
     } 

     public Color getColor() { 
      return color; 
     } 

     public Shape getOutline() { return outline; } 

    } 


} 

Output on my machine http://i38.tinypic.com/14nmglj.png

+1

환각 사진 : –

+1

고마워요! 이것은 매우 유용한 예입니다. –

4

Java에는 꽤 멋진 2D 그래픽 API가 포함되어 있습니다. 기본에 대한 자습서는 http://java.sun.com/docs/books/tutorial/2d/index.html을 살펴 보시기 바랍니다.

+0

+1 - 내가 빨리 또한 태양 튜토리얼을 제시하는 경향이있다. –