Skip to content

Instantly share code, notes, and snippets.

@sumtyme
Created August 9, 2012 20:15
Show Gist options
  • Save sumtyme/3307711 to your computer and use it in GitHub Desktop.
Save sumtyme/3307711 to your computer and use it in GitHub Desktop.
Path2d
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Path2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class GraphicTest extends JPanel{
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.RED);
g2.fillRect(0, 0, getWidth(), getHeight());
g2.setColor(Color.BLUE);
Path2D path = new Path2D.Double(new Rectangle(getWidth() / 2 - 100, getHeight() / 2 - 100, 200, 200));
path.setWindingRule(Path2D.WIND_EVEN_ODD);
path.append(new Ellipse2D.Double(getWidth() / 2 - 50, getHeight() / 2 - 50, 100, 100), false);
g2.fill(path);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(500, 500);
}
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setContentPane(new GraphicTest());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment