Java2D Polygon füllen

tabina

Mitglied
Hallo,

ich bin auf der Suche nach einer einfachen Möglichkeit, ein beliebiges konvexes Polygon mit parallelen Linien (also eine Art Schraffur) zu füllen. Leider bin ich noch ziemlich neu in Java2D und weiß daher nicht so genau, wonach ich suchen muss.

Gruß,

tabina
 
Hallo,

schau mal hier:
Java:
/**
 * 
 */
package de.tutorials;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Polygon;
import java.awt.TexturePaint;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;

/**
 * @author Thomas.Darimont
 * 
 */
public class PolygonFillingExample extends JFrame {

    Paint paint;
    int textureWidth = 32;
    int stepWidth = 4;
    Color textureColor = Color.BLUE;

    public PolygonFillingExample() {
        super("PolygonFillingExample");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(640, 480);

        paint = new TexturePaint(createTexture(), new Rectangle2D.Double(0, 0,
                textureWidth, textureWidth));

        setVisible(true);
    }

    private BufferedImage createTexture() {
        BufferedImage texture = new BufferedImage(textureWidth, textureWidth,
                BufferedImage.TYPE_INT_ARGB);

        Graphics2D g = texture.createGraphics();
        g.setColor(textureColor);

        for (int x = -textureWidth; x < textureWidth; x += stepWidth) {
            g.drawLine(x, 0, textureWidth + x, textureWidth);
        }
        return texture;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        new PolygonFillingExample();
    }

    @Override
    public void paint(Graphics gra) {
        super.paint(gra);
        Graphics2D g = (Graphics2D) gra;
        Polygon polygon = new Polygon(new int[] { 100, 150, 200, 250, 175 },
                                      new int[] { 100, 200, 200, 100, 50 }
         , 5);
        g.drawPolygon(polygon);
        g.setPaint(paint);
        g.fillPolygon(polygon);
    }

}

Gruß Tom
 
Zurück