第十九章Java绘图

Java绘图类


1.Graphics类
提供了绘图常用的方法,利用这些方法可以实现直线,矩形,多边形等文本,图片的绘制其操作主要包括颜色,字体,画笔,文本,图像等

2.Graphics2D类
使用Graphics2D类可以实现更强的图像绘制功能

3.将Graphics强制转换成Graphics2D类

public static void paint(Graphics g){
    Graphics2D g2 = (Graphics2D)g;
}
 

绘制图形
Graphics 类常用图形绘制方法

方法    说明

  • draw(int x,int y,int height,int startAngle,int arcAngle)    弧形
  • drawLine(int x1,int y1,int x2,int y2)    直线
  • drawOval(int x,int y,int width,int height)    椭圆
  • drawPolygon(int[] xPoints,int[] yPoints,int nPoints)    多边形
  • drawPolyline(int[] xPoints,int[] yPoints,int nPoints)    多边线
  • drawRect(int x,int y,int width,int height)    矩形
  • drawRoundRect(int x,int y,int width,int height,int arcWidth,int arcAngle)    圆角矩形
  • fillArc(int x,int y,int width,int height,int startAngle,int arcAngle)    实心弧形
  • fillOval(int x,int y,int width,int height)    实心椭圆
  • fillPolygon(int[] xPoints,int[] yPoints,int nPoints)    实心多边形
  • fillRect(int x,int y,int width,int height)    实心矩形
  • fillRoundRect(int x,int y,int width,int height,int arcWidth,int arcHeight)    实心圆角矩形

当我们要绘制图形时,我们必须要进行创建并初始化图形类对象。这些图形必须时Shape接口的实现类,然后使用Graphics2D类的draw方法进行绘制,或者fill方法进行填充。

语法:

  1. draw(Shape form)
     
  2. fill(Shape form)


常用图形类:


主函数:

public static void main(String[] args) {
        new G_1().setVisible(true);
    }


Arc2D类:弧

Graphics2D g2d =(Graphics2D)g;
            Shape[] shapes = new Shape[3];
            shapes[0] = new Arc2D.Double(50,50,185,160,10,90,Arc2D.OPEN);//坐标位置(50,50),宽85,高60,起始角是5度,终止角是90度
            shapes[1] = new Arc2D.Double(150,150,185,160,10,90,Arc2D.CHORD);
            shapes[2] = new Arc2D.Double(200,300,185,160,10,90,Arc2D.PIE);
            for (Shape shape:shapes){//遍历图形数组
                g2d.draw(shape);//绘制图形
            }
//Arc2D.OPEN、Arc2D.CHORD、Arc2D.PIE分别表示圆弧是开弧、弓弧和饼弧。

  • CubicCurve2D类:立方曲线
     class drawplace extends JPanel{
                public void paint(Graphics g){
                    Graphics2D g2d =(Graphics2D)g;
                    Shape[] shapes = new Shape[1];
                    shapes[0] =new CubicCurve2D.Double(100,100,50,75,15,15,260,300);
                    //参数分别表示坐标起点,控制点1,控制点2,终点的坐标
                    for (Shape shape:shapes){//遍历图形数组
                        g2d.draw(shape);//绘制图形
                    }
                }
        }
    
    class drawplace extends JPanel{
                public void paint(Graphics g){
                    Graphics2D g2d =(Graphics2D)g;
                    Shape[] shapes = new Shape[1];
                    shapes[0] = new Ellipse2D.Double(150,150,200,100);
                    //参数表示起点,宽,高
                    for (Shape shape:shapes){//遍历图形数组
                        g2d.draw(shape);//绘制图形
                    }
                }
        }
    


    class drawplace extends JPanel{
                public void paint(Graphics g){
                    Graphics2D g2d =(Graphics2D)g;
                    Shape[] shapes = new Shape[1];
                    shapes[0] = new Ellipse2D.Double(150,150,200,100);
                    //参数表示起点,宽,高
                    for (Shape shape:shapes){//遍历图形数组
                        g2d.draw(shape);//绘制图形
                    }
                }
        }

     

    Line2D类:线
    class drawplace extends JPanel{
                public void paint(Graphics g){
                    Graphics2D g2d =(Graphics2D)g;
                    Shape[] shapes = new Shape[1];
                    shapes[0] = new Line2D.Double(10,150,250,150);
                    //参数表示起点和终点
                    for (Shape shape:shapes){//遍历图形数组
                        g2d.draw(shape);//绘制图形
                    }
                }
        }

    Point2D类:点

    QuadCurve2D类:二次曲线

    class drawplace extends JPanel{
                public void paint(Graphics g){
                    Graphics2D g2d =(Graphics2D)g;
                    Shape[] shapes = new Shape[1];
                    shapes[0] = new QuadCurve2D.Double(100,100,10,150,250,300);
                    //参数表示起点,控制点,终点
                    for (Shape shape:shapes){//遍历图形数组
                        g2d.draw(shape);//绘制图形}
                    }
                }
      }

  • Rectangle2D类:矩形
  • RoundRectangle2D类:圆角矩形
    例子:
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;

public class G_1 extends JFrame {
    public G_1(){
        setTitle("绘图实例");
        setSize(500,500);//设置窗体大小
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗体的关闭模式
        add(new CanvasPanel());//设置窗体画板为绘图画板对象
    }
    class CanvasPanel extends JPanel{   //绘图画板
        public void paint(Graphics g){
            Graphics2D g2d = (Graphics2D)g;
            Shape[] shapes = new Shape[4];//声明图形数组
            shapes[0] = new Ellipse2D.Double(5,5,100,100);//创建圆形对象
            shapes[1] = new Rectangle2D.Double(110,5,160,40);//矩形
            shapes[2] = new Ellipse2D.Double(120,15,80,80);
            shapes[3] = new Rectangle2D.Double(35,15,80,80);
            for (Shape shape:shapes){//遍历图形数组
                Rectangle2D bounds = shape.getBounds2D();
                if (bounds.getWidth()==80){
                    g2d.fill(shape);//填充
                }
                else {
                    g2d.draw(shape);//绘制图形
                }
            }
        }
    }
    public static void main(String[] args) {
        new G_1().setVisible(true);
    }
}

绘图颜色和画笔属性

1.设置颜色
语法:

常用构造方法:

  • BasicStroke()
  • BasicStroke(float width)
  • BasicStroke(float width,int cap,int join)
  • BasicStroke(float width,int cap,int join,float miterlimit)
  • BasicStroke(float width,int cap,int join,float miterlimit,float[] dash,float dash_phase)

BasicStroke类构造方法的参数说明

参数    说明
width    画笔宽度默认为1px
cap    线端点装饰
join    应用在路径线段交汇处的装饰
miterlimit    斜接处的剪裁限制,参数必须大于等于1.0f
dash    表示虚线模式的数组
dash_phase    开始虚线模式的偏移量
cap参数的三个常量:

CAP_BUTT:末端圆滑
CAP_ROUND:末端平整
CAP_SQUARE:末端设置以线段宽度为边长的方形
join参数的三个常量:

JOIN_BEVEL:平角
JOIN_MITER:尖角
JOIN_ROUND:圆角
 

绘制文本


1.设置字体
可以设置名称,样式,字体大小
语法:

Font f = new Font("方正舒体",Font.BOLD,25);
g2d.setFont(f);

Font类的常量:

PLAIN:普通
BOLD:加粗
ITALIC:斜体
ITALIC|BOLD:斜体加粗
例子:

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

public class G_3 extends JFrame {
    public G_3(){
        setTitle("font_test");
        setSize(400,400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        add(new drawplace());
    }
    class drawplace extends JPanel{
        public void paint(Graphics g){
            Graphics2D g2d = (Graphics2D)g;
            Font f = new Font("方正舒体",Font.BOLD,25);
            g2d.setFont(f);
            g2d.drawString("文字:方正舒体",100,100);
        }
    }

    public static void main(String[] args) {
        new G_3().setVisible(true);
    }
}

显示图片


使用drawImage方法将图片显示到绘图中
语法:

drawImage(Image img,int x,int y,ImageObserver observer)
1
参数表示要显示的图片对象,坐标,要通知的图像观察者
例子:

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;

public class show_photos extends JFrame {
    Image img;

    public show_photos(){
        try{
            img = ImageIO.read(new File("D:\\work\\累.png"));
        }catch(IOException e){
            e.printStackTrace();
        }

        setTitle("show_photos");
        setSize(916,525);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        add(new drawplace());
    }

    class drawplace extends JPanel{
        public void paint(Graphics g){
            Graphics2D g2d = (Graphics2D)g;
            g2d.drawImage(img,0,0,this);
        }
    }

    public static void main(String[] args) {
        new show_photos().setVisible(true);
    }
}

图像处理


1.放大和缩小
依然使用drawImage方法语法:

drawImage(Image img,int x,int y,int width,int height,ImageObserver observer)
1
例子:

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;

public class show_photos extends JFrame {
    Image img;

    public show_photos(){
        try{
            img = ImageIO.read(new File("D:\\work\\累.png"));
        }catch(IOException e){
            e.printStackTrace();
        }

        setTitle("show_photos");
        setSize(916,525);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        add(new drawplace());
    }

    class drawplace extends JPanel{
        public void paint(Graphics g){
            Graphics2D g2d = (Graphics2D)g;
            g2d.drawImage(img,0,0,300,200,this);
        }
    }

    public static void main(String[] args) {
        new show_photos().setVisible(true);
    }
}

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

public class G_2 extends JFrame {
    public G_2(){
        setTitle("Graphics2D绘制测试");
        setSize(500,500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        add(new drawplace());
    }
    class drawplace extends JPanel{
            public void paint(Graphics g){
                Graphics2D g2d =(Graphics2D)g;
                g2d.setColor(Color.green);
                Shape[] shapes = new Shape[1];
                shapes[0] = new Ellipse2D.Double(150,150,200,200);
                for (Shape shape:shapes){//遍历图形数组
                    g2d.fill(shape);
                    g2d.draw(shape);//绘制图形
                }
            }
    }

    public static void main(String[] args) {
        new G_2().setVisible(true);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值