Java 绘制艺术图案

版权声明:文章版权归作者所有,请不要随意转载抄袭,情节严重,追究法律责任!! https://blog.csdn.net/Ibelievesunshine/article/details/84477910

1. 使用 Graphics2D 类的 translate() 方法,将坐标轴平移到指定点

2. 使用 Graphics2D 类的 setColor() 方法,设置颜色

3. 使用 Graphics2D 类的 rotate() 方法,旋转绘图上下文

4. 使用 Graphics2D 类的 draw() 方法,在指定位置绘制椭圆

最终效果如下:

package com.wk.image

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ArtDesignFrame extends JFrame {
    ArtDesignPanel artDesignPanel = new ArtDesignPanel(); // 创建面板类的实例
    
    public static void main(String args[]) { // 主方法
        ArtDesignFrame frame = new ArtDesignFrame(); // 创建窗体类的实例
        frame.setVisible(true); // 显示窗体
    }
    
    public ArtDesignFrame() {
        super(); // 调用超类的构造方法
        setTitle("绘制艺术图案"); // 窗体标题
        setBounds(100, 100, 338, 230); // 窗体的显示位置和大小
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 窗体关闭方式
        add(artDesignPanel); // 将面板类的实例添加到窗体容器中
    }
    
    class ArtDesignPanel extends JPanel { // 创建内部面板类
        public void paint(Graphics g) {     // 重写paint()方法
            Graphics2D g2 = (Graphics2D)g; // 获得Graphics2D对象
            Ellipse2D.Float ellipse = new Ellipse2D.Float(-80, 5, 160, 10);// 创建椭圆对象
            Random random = new Random();// 创建随机数对象
            g2.translate(160, 90);// 平移坐标轴
            int R = random.nextInt(256);//随机产生颜色的R值
            int G = random.nextInt(256);//随机产生颜色的G值
            int B = random.nextInt(256);//随机产生颜色的B值
            Color color = new Color(R,G,B);//创建颜色对象
            g2.setColor(color);//指定颜色
            g2.draw(ellipse);// 绘制椭圆
            int i=0;
            while (i<100){
                R = random.nextInt(256);//随机产生颜色的R值
                G = random.nextInt(256);//随机产生颜色的G值
                B = random.nextInt(256);//随机产生颜色的B值
                color = new Color(R,G,B);//创建新的颜色对象
                g2.setColor(color);//指定颜色
                g2.rotate(10);// 旋转画布
                g2.draw(ellipse);// 绘制椭圆
                i++;
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/Ibelievesunshine/article/details/84477910