1. 环境
JDK1.6
2.代码
package tmp.j2d; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Shape; import java.awt.geom.Area; import java.awt.geom.Ellipse2D; import java.awt.geom.Rectangle2D; import javax.swing.JPanel; import com.yuan.common.swing.Circle; public class AreaPanel extends JPanel { private static final long serialVersionUID = 1L; public AreaPanel(){ super(); super.setPreferredSize(new Dimension(600, 600)); } public void paintComponent(Graphics g){ super.paintComponents(g); Graphics2D g2 = (Graphics2D)g; Shape s1 = new Ellipse2D.Double(100, 100, 400, 400); Shape s2 = new Ellipse2D.Double(200, 100, 200, 200); Shape s3 = new Ellipse2D.Double(200, 300, 200, 200); Shape leftRect = new Rectangle2D.Double(100, 100, 200, 400); Shape rightRect = new Rectangle2D.Double(300, 100, 200, 400); Area a2 = new Area(s2); Area a3 = new Area(s3); Area leftRectArea = new Area(leftRect); Area rightRectArea = new Area(rightRect); Area leftCircleArea = new Area(s1); leftCircleArea.subtract(rightRectArea); leftCircleArea.add(a2);//radius leftCircleArea.subtract(a3); Area rightCircleArea = new Area(s1); rightCircleArea.subtract(leftRectArea); rightCircleArea.subtract(a2); rightCircleArea.add(a3); g2.setColor(Color.white); g2.fill(leftCircleArea); g2.setColor(Color.black); g2.fill(rightCircleArea); g2.fill(new Circle(300, 200, 10)); g2.setColor(Color.white); g2.fill(new Circle(300, 400, 10)); } }
package com.yuan.common.swing; import java.awt.geom.*; public class Circle extends Ellipse2D.Double { private static final long serialVersionUID = 1L; protected double cx; protected double cy; protected double radius; public Circle(double cx, double cy, double radius){ super(cx - radius, cy - radius, 2*radius, 2*radius); this.cx = cx; this.cy = cy; this.radius = radius; } public double getCx() { return cx; } public double getCy() { return cy; } public double getRadius() { return radius; } }
3.测试
package tmp.j2d; import java.awt.Component; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; public class ImageFrame extends JFrame { private static final long serialVersionUID = 1L; public ImageFrame(String title){ super(); this.setTitle(title); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //必须,否则内存泄露 } /** * 设置窗口图标, 可以是png,gif等 * @param iconFile String */ public void setIcon(String iconFile){ try { super.setIconImage(ImageIO.read(new File(iconFile))); } catch (IOException e) { e.printStackTrace(); } } public void fullScreen(){ GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gd = ge.getDefaultScreenDevice(); if ( gd.isFullScreenSupported() ){ gd.setFullScreenWindow(this); } if (gd.isDisplayChangeSupported()) { // gd.setDisplayMode(new DisplayMode(800, 600, DisplayMode.BIT_DEPTH_MULTI, 60)); } } private void center(){ double screenWidth = Toolkit.getDefaultToolkit().getScreenSize().getWidth(); double screenHeight = Toolkit.getDefaultToolkit().getScreenSize().getHeight(); this.setLocation((int) (screenWidth - this.getWidth()) / 2, (int) (screenHeight - this.getHeight()) / 2); // new Robot().createScreenCapture(new Rectangle(0, 0, (int)screenWidth, (int)screenHeight)); } private void renderToScreen(){ // fullScreen(); this.pack(); center(); this.setVisible(true); } public void showImage(BufferedImage bufferedImage){ this.getContentPane().add(new ImagePanel(bufferedImage)); renderToScreen(); } public void showPanel(JPanel panel){ this.getContentPane().add(panel); renderToScreen(); } public void showComponent(Component comp){ this.getContentPane().add(comp); renderToScreen(); } } class ImagePanel extends JPanel{ private static final long serialVersionUID = 1L; private BufferedImage bufferedImage; public ImagePanel(BufferedImage bufferedImage){ this.bufferedImage = bufferedImage; //设置组件首选大小 this.setPreferredSize(new Dimension(bufferedImage.getWidth(), bufferedImage.getHeight())); } public void paintComponent(Graphics g){ super.paintComponents(g); Graphics2D g2d = (Graphics2D)g; g2d.drawImage(bufferedImage, 0, 0, null); } }
public static void main(String[] args) throws Exception{ EventQueue.invokeLater(new Runnable() { public void run() { ImageFrame imageFrame = new ImageFrame("Java2D 演示"); imageFrame.showPanel(new AreaPanel()); } }); }
运行效果: