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方法进行填充。

语法:

draw(Shape form)
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);//绘制图形
                }
            }
    }

在这里插入图片描述

  • Ellipse2D类:椭圆
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.设置颜色
语法:

Color col = new Color(int r,ing g,int b);//参数表示三原色
Color col = new Color(int rgb);//参数表示颜色值,是三原色的总和

在这里插入图片描述
在绘图类中使用setColor方法快速设置颜色

g.setColor(Color.color)//注意是有.的

例子:

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

程序展示:
在这里插入图片描述
设置画笔
语法:

Stroke st = new BasicStroke(4);
g2d.setStroke(st);

例子:

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);
                Stroke st = new BasicStroke(15);
                g2d.setStroke(st);
                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);
    }
}

程序展示:
在这里插入图片描述
常用构造方法:

  • 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参数的三个常量:

  1. CAP_BUTT:末端圆滑
  2. CAP_ROUND:末端平整
  3. CAP_SQUARE:末端设置以线段宽度为边长的方形

join参数的三个常量:

  1. JOIN_BEVEL:平角
  2. JOIN_MITER:尖角
  3. JOIN_ROUND:圆角

绘制文本

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

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

Font类的常量:

  1. PLAIN:普通
  2. BOLD:加粗
  3. ITALIC:斜体
  4. 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);
    }
}

程序展示:
在这里插入图片描述
例子:

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

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;
            Date time = new Date();
            Font f = new Font("方正舒体",Font.BOLD,25);
            g2d.setColor(Color.RED);
            g2d.setFont(f);
            g2d.drawString("现在是:",50,100);
            g2d.drawString(String.format("%tr",time),100,200);
        }
    }

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

程序展示:
在这里插入图片描述

显示图片

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

drawImage(Image img,int x,int y,ImageObserver observer)

参数表示要显示的图片对象,坐标,要通知的图像观察者
例子:

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)

例子:

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.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.io.File;
import java.io.IOException;

public class show_p_change extends JFrame {
    Image img;
    int imgwidth,imgheight;
    private JSlider js;
    public show_p_change(){
        try{
            img = ImageIO.read(new File("D:\\work\\累.png"));
        }catch(IOException e){
            e.printStackTrace();
        }
        showplace sp = new showplace();
        js = new JSlider();
        js.setMaximum(1500);
        js.setValue(50);
        js.setMinimum(10);
        js.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                sp.repaint();
            }
        });
        JPanel center = new JPanel();
        center.setLayout(new BorderLayout());
        center.add(js,BorderLayout.SOUTH);
        center.add(sp,BorderLayout.CENTER);
        setContentPane(center);
        setTitle("change_p_show");
        setBounds(100,100,800,600);//设置窗体大小和位置
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    class showplace extends JPanel{
        public void paint(Graphics g){
            Graphics2D g2d = (Graphics2D)g;
            int n_w = 0,n_h = 0;
            imgwidth = img.getWidth(this);
            imgheight = img.getHeight(this);
            float value = js.getValue();
            n_w = (int)(imgwidth*value/100);
            n_h = (int)(imgheight*value/100);
            g2d.drawImage(img,0,0,n_w,n_h,this);
        }
    }

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

程序展示:
在这里插入图片描述
在这里插入图片描述
图像翻转
语法:

drawImage(image img,int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4,ImageObserver observer)

参数说明:

参数说明
img要绘制的对象
x1目标矩阵第一个坐标的x位置
y1目标矩阵第一个坐标的y位置
x2目标矩阵第二个坐标的x位置
y2目标矩阵第二个坐标的y位置
x3源矩阵第一个坐标的x位置
y3源矩阵第一个坐标的y位置
x4源矩阵第二个坐标的x位置
y4源矩阵第二个坐标的y位置
observer要通知的图像观察者

例子:

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

public class G_flip extends JFrame {
    Image img;
    int dx1,dy1,dx2,dy2;
    int sx1,sx2,sy1,sy2;
    int imgw= 800,imgh=500;//照片的宽高
    JButton vbtn = null;//垂直翻转
    JButton hbtn = null;//水平翻转
    Cpanel canvas = null;
    public G_flip(){
        try{
            img = ImageIO.read(new File("D:\\work\\累.png"));
        }catch(IOException e){
            e.printStackTrace();
        }
        dx2 = sx2  =imgw;
        dy2 = sy2 = imgh;
        vbtn = new JButton("垂直翻转");
        hbtn = new JButton("水平翻转");
        JPanel bottom = new JPanel();
        bottom.add(vbtn);
        bottom.add(hbtn);
        Container c = getContentPane();
        c.add(bottom,BorderLayout.SOUTH);
        canvas = new Cpanel();
        c.add(canvas,BorderLayout.CENTER);
        addL();
        setBounds(100,100,800,700);
        setTitle("图像翻转");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    void addL(){
        vbtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                sy1 = Math.abs(sy1-imgh);   //纵坐标交换
                sy2 = Math.abs(sy2-imgh);
                canvas.repaint();
            }
        });
        hbtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                sx1 = Math.abs(sx1-imgw);   //横坐标交换
                sx2 = Math.abs(sx2-imgw);
                canvas.repaint();
            }
        });
    }
    class Cpanel extends JPanel{
        public void paint(Graphics g){
            Graphics2D g2d = (Graphics2D)g;
            g2d.drawImage(img,dx1,dy1,dx2,dy2,sx1,sy1,sx2,sy2,this);
        }
    }

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

程序展示:
在这里插入图片描述
图像旋转
使用Graphics2D中的rotate方法根据弧度旋转图像语法:

rotate(double 旋转弧度)

我们可以使用Math类中的toRadians方法把旋转角度转化为弧度
例子:

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

public class G_route extends JFrame {
    Image img;
    public G_route(){
        try{
            img = ImageIO.read(new File("D:\\work\\累.png"));
        }catch(IOException e){
            e.printStackTrace();
        }
        setBounds(100,100,800,800);
        setTitle("图像旋转");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        add(new Cp());
    }

    class Cp extends JPanel{
        public void paint(Graphics g){
            Graphics2D g2d = (Graphics2D)g;
            g2d.rotate(Math.toRadians(10));
            g2d.drawImage(img,100,100,300,300,this);
            g2d.rotate(Math.toRadians(60));
            g2d.drawImage(img,300,-300,300,300,this);
        }
    }

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

程序展示:
在这里插入图片描述


版权声明:本文为qq_51553982原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。