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() {@Overridepublic 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() {@Overridepublic void actionPerformed(ActionEvent e) {sy1 = Math.abs(sy1-imgh);   //纵坐标交换sy2 = Math.abs(sy2-imgh);canvas.repaint();}});hbtn.addActionListener(new ActionListener() {@Overridepublic 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);}
}

程序展示:

Java绘图,图像处理相关推荐

  1. Java绘图之设置字型和颜色

    Java绘图中,显示文字的方法主要有三种: (1)drawString(String str,int x,int y):在指定的位置显示字符串. (2)drawChars(char data[],in ...

  2. Java数字图像处理基础-------Java Swing简单使用,图形绘画---画五角星

    Java数字图像处理基础-------Java Swing简单使用,图形绘画-画五角星 一:简介 要画出五角星出来,我们只需要在面板上产生5个点,然后把这5个点进行连接就可实现: 二:代码演示 imp ...

  3. java绘图- 绘图用法(基于Graphics2D)

    java绘图(基于Graphics2D) 1.绘图基本操作 请参考下面基础示例: 1 int width = 200, height = 250;2 //创建图片对象3 BufferedImage i ...

  4. Java OpenCV 图像处理34 图形图像 分水岭 watershed

    Java OpenCV 图像处理34 图形图像 分水岭 watershed package com.xu.opencv;import org.opencv.core.Core; import org. ...

  5. java绘图技术,演示绘制不同的图形

    java绘图技术,演示绘制不同的图形. 做一个笔记,方便日后查找. import javax.swing.*; import java.awt.*;public class DrawCircle ex ...

  6. 用java画只乌龟_Swing编程方面步骤之四java绘图技术画小乌龟

    首先,手动画一个小乌龟,如下: 然后,按照java绘图基本步骤一步步来. swing 编程步骤: 1. 继承JFrame 2. 定义组件 3.创建组件(构造函数) 4.添加组件 5.对窗体设置 6.显 ...

  7. Java OpenCV 图像处理30 视频分析和对象跟踪 视频读取

    Java OpenCV 图像处理30 视频分析和对象跟踪 视频读取 Java OpenCV-4.0.0 图像处理 视频分析和对象跟踪 视频读取 package com.xu.opencv.video; ...

  8. Java OpenCV 图像处理32.4 视频分析和对象跟踪 切换背景

    Java OpenCV 图像处理32.4 视频分析和对象跟踪 切换背景 方法 含义 解释 bitwise_and "与"操作,即对图像(灰度图像或彩色图像均可)每个像素值进行二进制 ...

  9. 关于《Java数字图像处理-编程技巧与应用实践》一书 源代码

    关于<Java数字图像处理-编程技巧与应用实践>一书 源代码 本书所有的源代码我已经整理上传到华章图书的官方网站与 我自己的GITHUB上,本人GITHUB的地址如下: https://g ...

最新文章

  1. c++——结构与指针 类与指针
  2. Jmeter中中文乱码
  3. linux的常用操作——静态库
  4. es6 prototype 属性和__proto__属性
  5. Win7如何禁用无线网卡
  6. Python入门--字典的创建
  7. linux 使用systemctl 启动服务报错: Error: No space left on device
  8. mysql命令语句连接数据库_MySQL_MySQL常用基本SQL语句总结,1. 常见命令连接本地数据库 - phpStudy...
  9. QCC---Temperature component
  10. 测试开发岗面试,需要准备的100道题型
  11. 查看CentOS版本信息
  12. linux可以用tab键,linux下tab键在命令行情况下的强大
  13. Qt程序运行时出现:0xc000007b错误参考解决方法
  14. 《Adobe Photoshop CS5中文版经典教程(全彩版)》目录—导读
  15. Github代码复现-IVIX中国波指计算
  16. android模拟走路,走路模拟器官网版
  17. AI智能抠图工具--头发丝都可见
  18. CDOJ 第七届ACM趣味程序设计竞赛第三场(正式赛) 题解
  19. android 仿ios带弹簧效果的ScrollView
  20. 在公众号文章中添加**人员名单

热门文章

  1. 解决Word、Excel启动时提示向程序发送命令出现问题
  2. 计算机命令提示符开热点,win7系统使用cmd命令创建wifi热点的方法
  3. 1. vue.js的快速入门使用
  4. NAS群晖DSM 进阶教程 篇三:DSM 4458 升级 DSM 4493 update7
  5. android 画图 出界,android:字符串索引出界
  6. 三星计划降低存储芯片产能维持高价凸显它的垄断地位
  7. 中国移动GPRS概况
  8. Cisco Anyconnect 导致 wsl2 网络连接异常问题处理
  9. Fly.Box 企业网盘2.2.1 发布
  10. python运行selenium时浏览器闪退情况: