java编写图像画画

这篇博客介绍了Java的Graphics2D API,包括如何创建画板、设置渲染效果、画图等基本操作。通过示例展示了如何绘制椭圆、矩形、线条和曲线,并演示了填充和描边的效果。还提供了遍历系统字体的方法。
摘要由CSDN通过智能技术生成

graphics2d api整理(列举一些经常使用的)

// 画板笔设置:
/**
 * 创建画板
 */
 public Graphics2D createGraphics();
/**
 * 渲染效果配置
 */
 public abstract void setRenderingHint(Key hintKey, Object hintValue);
/**
 * 设置画笔颜色
 */
public abstract void setPaint( Paint paint );
/**
 * 设置文字样式
 */
public abstract void setFont(Font font);
// 画图

/**
 * 往画板上填充图片,observer一般设置为null
 */
public abstract boolean drawImage(Image img, int x, int y,
                                      ImageObserver observer);
/**
 * 往画板上写字
 */
public abstract void drawString(String str, int x, int y);
/**
 * 画矩形
 */
public void drawRect(int x, int y, int width, int height);
/**
 * 填充矩形
 */
public abstract void fillRect(int x, int y, int width, int height);
/**
 * 画圆弧 startAngle=90时指向北方,逆时针方向画线
 */
public abstract void drawArc(int x, int y, int width, int height,
                                 int startAngle, int arcAngle);
/**
 * 填充扇形 startAngle=90时指向北方,逆时针方向填充
 */
public abstract void fillArc(int x, int y, int width, int height,
                                 int startAngle, int arcAngle);
/**
 * 画椭圆 width = height时为圆
 */
public abstract void drawOval(int x, int y, int width, int height);
/**
 * 填充椭圆
 */
public abstract void fillOval(int x, int y, int width, int height);

案例:

package com.example.text;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;

/**
 * java编写图像画画
 */

public class Testing2D {
    public static void main(String[] args) {
        TestF1 test=new TestF1();
        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        test.setVisible(true);

        fonts();
    }

    /**
     * 遍历查询字体
     */
    public static void fonts(){
        // 打印字体选择
        Font[] fonts = GraphicsEnvironment
                .getLocalGraphicsEnvironment()
                .getAllFonts();
        for (Font font : fonts) {
            System.out.println(font.getFontName());
        }
    }
}


/**
 * 构造窗口的初始化时的大小
 * @author Administrator
 */

class TestF1 extends JFrame {
    public TestF1() {
        setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);

        TestF2 test=new TestF2();

        test.setForeground(SystemColor.window);

        add(test);
    }

    public static final int DEFAULT_WIDTH=300;

    public static final int DEFAULT_HEIGHT=300;

}

/**

 *构造Graphics2D功能
 * @author Administrator
 */

class TestF2 extends JComponent {
    public void paintComponent(Graphics g) {
        Graphics2D g2=(Graphics2D)g;//将该功能转换为2D的功能
        // 实例化一个椭圆的对象
        Ellipse2D.Float test2D=new Ellipse2D.Float(100.0f,100.0f,300.0f,600.0f);//第1/2个参数是表示左上角的位置,3/4表示宽和高
        // 长方形
        Rectangle2D.Float test2Ddd=new Rectangle2D.Float(450.0f,100.0f,30.0f,300.0f);

        // 圆圈
        Ellipse2D.Float test2Dd=new Ellipse2D.Float(100.0f,100.0f,50.0f,50.0f);
        // 直线
        Line2D test=new Line2D.Float(12.0f,13.0f, 250.0f, 16.0f);
        // 曲线
        CubicCurve2D curve1 = new CubicCurve2D.Double(12,30,50,75,15,15,115,93);
        CubicCurve2D curve2 = new CubicCurve2D.Double(12,30,15,70,20,25,35,94);
        CubicCurve2D curve3 = new CubicCurve2D.Double(12,30,50,75,20,95,95,95);

/**
 * 将这个对象用2D方法画出来
 */

        g2.setPaint(Color.BLUE);
        g2.drawArc(200,200,200,200,90,450-30);
        g2.fillArc(200,200,200,200,90,450-30);
//
//        // draw sector
        g2.setPaint(Color.RED);
        g2.drawArc(200,200,200,200,90-30,30);
        g2.fillArc(200,200,200,200,90-30,30);

        g2.setPaint(Color.blue);
        g2.draw(test);
//
        g2.setPaint(Color.green);
        g2.draw(curve1);
        g2.draw(curve2);
        g2.draw(curve3);
//
        g2.setPaint(Color.yellow);
        g2.draw(test2D);

        g2.setPaint(Color.black);
        g2.draw(test2Ddd);
        g2.fill(test2Ddd);//填充颜色
//
        g2.draw(test2Dd);

    }

}

结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值