Java绘图,图像处理
Java繪圖類
1.Graphics類
提供了繪圖常用的方法,利用這些方法可以實(shí)現(xiàn)直線,矩形,多邊形等文本,圖片的繪制其操作主要包括顏色,字體,畫筆,文本,圖像等
2.Graphics2D類
使用Graphics2D類可以實(shí)現(xiàn)更強(qiáng)的圖像繪制功能
3.將Graphics強(qiáng)制轉(zhuǎn)換成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) | 實(shí)心弧形 |
| fillOval(int x,int y,int width,int height) | 實(shí)心橢圓 |
| fillPolygon(int[] xPoints,int[] yPoints,int nPoints) | 實(shí)心多邊形 |
| fillRect(int x,int y,int width,int height) | 實(shí)心矩形 |
| fillRoundRect(int x,int y,int width,int height,int arcWidth,int arcHeight) | 實(shí)心圓角矩形 |
當(dāng)我們要繪制圖形時(shí),我們必須要進(jìn)行創(chuàng)建并初始化圖形類對(duì)象。這些圖形必須時(shí)Shape接口的實(shí)現(xiàn)類,然后使用Graphics2D類的draw方法進(jìn)行繪制,或者fill方法進(jìn)行填充。
語法:
draw(Shape form) fill(Shape form)常用圖形類:
主函數(shù):
- Arc2D類:弧
- CubicCurve2D類:立方曲線
- Ellipse2D類:橢圓
- Line2D類:線
-
Point2D類:點(diǎn)
-
QuadCurve2D類:二次曲線
- Rectangle2D類:矩形
- RoundRectangle2D類:圓角矩形
例子:
程序展示:
繪圖顏色和畫筆屬性
1.設(shè)置顏色
語法:
在繪圖類中使用setColor方法快速設(shè)置顏色
例子:
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){//遍歷圖形數(shù)組g2d.fill(shape);g2d.draw(shape);//繪制圖形}}}public static void main(String[] args) {new G_2().setVisible(true);} }程序展示:
設(shè)置畫筆
語法:
例子:
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){//遍歷圖形數(shù)組//g2d.fill(shape);g2d.draw(shape);//繪制圖形}}}public static void main(String[] args) {new G_2().setVisible(true);} }程序展示:
常用構(gòu)造方法:
- 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類構(gòu)造方法的參數(shù)說明
| width | 畫筆寬度默認(rèn)為1px |
| cap | 線端點(diǎn)裝飾 |
| join | 應(yīng)用在路徑線段交匯處的裝飾 |
| miterlimit | 斜接處的剪裁限制,參數(shù)必須大于等于1.0f |
| dash | 表示虛線模式的數(shù)組 |
| dash_phase | 開始虛線模式的偏移量 |
cap參數(shù)的三個(gè)常量:
join參數(shù)的三個(gè)常量:
繪制文本
1.設(shè)置字體
可以設(shè)置名稱,樣式,字體大小
語法:
Font類的常量:
例子:
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方法將圖片顯示到繪圖中
語法:
參數(shù)表示要顯示的圖片對(duì)象,坐標(biāo),要通知的圖像觀察者
例子:
程序展示:
圖像處理
1.放大和縮小
依然使用drawImage方法語法:
例子:
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);} }程序展示:
例子(用劃片改變大小):
程序展示:
圖像翻轉(zhuǎn)
語法:
參數(shù)說明:
| img | 要繪制的對(duì)象 |
| x1 | 目標(biāo)矩陣第一個(gè)坐標(biāo)的x位置 |
| y1 | 目標(biāo)矩陣第一個(gè)坐標(biāo)的y位置 |
| x2 | 目標(biāo)矩陣第二個(gè)坐標(biāo)的x位置 |
| y2 | 目標(biāo)矩陣第二個(gè)坐標(biāo)的y位置 |
| x3 | 源矩陣第一個(gè)坐標(biāo)的x位置 |
| y3 | 源矩陣第一個(gè)坐標(biāo)的y位置 |
| x4 | 源矩陣第二個(gè)坐標(biāo)的x位置 |
| y4 | 源矩陣第二個(gè)坐標(biāo)的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;//垂直翻轉(zhuǎn)JButton hbtn = null;//水平翻轉(zhuǎn)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("垂直翻轉(zhuǎn)");hbtn = new JButton("水平翻轉(zhuǎn)");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("圖像翻轉(zhuǎn)");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}void addL(){vbtn.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {sy1 = Math.abs(sy1-imgh); //縱坐標(biāo)交換sy2 = Math.abs(sy2-imgh);canvas.repaint();}});hbtn.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {sx1 = Math.abs(sx1-imgw); //橫坐標(biāo)交換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);} }程序展示:
圖像旋轉(zhuǎn)
使用Graphics2D中的rotate方法根據(jù)弧度旋轉(zhuǎn)圖像語法:
我們可以使用Math類中的toRadians方法把旋轉(zhuǎn)角度轉(zhuǎn)化為弧度
例子:
程序展示:
總結(jié)
以上是生活随笔為你收集整理的Java绘图,图像处理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一级计算机多选题,2016年计算机一级考
- 下一篇: 聊一聊软件配置项