Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 407|回复: 0

[Swing学习]java制作韩国太级图

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-11-13 23:59:44 | 显示全部楼层 |阅读模式
      
      
       
       

         
       

         
       
      



    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));
            }
    }
                      [/code]
    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;
            }
    }
                      [/code]
    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);
            }
           
    }
            [/code]
    public static void main(String[] args) throws Exception{
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    ImageFrame imageFrame = new ImageFrame("Java2D 演示");
                   
                    imageFrame.showPanel(new AreaPanel());
               }
           });
    }
                      [/code]
       
    运行效果:

      



      
      
       
       

         
       

         
       
      
    复制代码

    源码下载:http://file.javaxxz.com/2014/11/13/235944500.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-25 07:45 , Processed in 0.355799 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表