编程语言
首页 > 编程语言> > java编写图像画画

java编写图像画画

作者:互联网

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);

    }

}

结果:

标签:200,java,g2,画画,void,abstract,int,编写,public
来源: https://blog.csdn.net/wdlizhi/article/details/115246605