日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

Java绘图,图像处理

發(fā)布時(shí)間:2023/12/20 java 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java绘图,图像处理 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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ù):

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);//坐標(biāo)位置(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){//遍歷圖形數(shù)組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);//參數(shù)分別表示坐標(biāo)起點(diǎn),控制點(diǎn)1,控制點(diǎn)2,終點(diǎn)的坐標(biāo)for (Shape shape:shapes){//遍歷圖形數(shù)組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);//參數(shù)表示起點(diǎn),寬,高for (Shape shape:shapes){//遍歷圖形數(shù)組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);//參數(shù)表示起點(diǎn),寬,高for (Shape shape:shapes){//遍歷圖形數(shù)組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);//參數(shù)表示起點(diǎn)和終點(diǎn)for (Shape shape:shapes){//遍歷圖形數(shù)組g2d.draw(shape);//繪制圖形}}}

  • Point2D類:點(diǎn)

  • 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);//參數(shù)表示起點(diǎn),控制點(diǎn),終點(diǎn)for (Shape shape:shapes){//遍歷圖形數(shù)組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("繪圖實(shí)例");setSize(500,500);//設(shè)置窗體大小setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設(shè)置窗體的關(guān)閉模式add(new CanvasPanel());//設(shè)置窗體畫板為繪圖畫板對(duì)象}class CanvasPanel extends JPanel{ //繪圖畫板public void paint(Graphics g){Graphics2D g2d = (Graphics2D)g;Shape[] shapes = new Shape[4];//聲明圖形數(shù)組shapes[0] = new Ellipse2D.Double(5,5,100,100);//創(chuàng)建圓形對(duì)象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){//遍歷圖形數(shù)組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.設(shè)置顏色
語法:

Color col = new Color(int r,ing g,int b);//參數(shù)表示三原色 Color col = new Color(int rgb);//參數(shù)表示顏色值,是三原色的總和


在繪圖類中使用setColor方法快速設(shè)置顏色

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){//遍歷圖形數(shù)組g2d.fill(shape);g2d.draw(shape);//繪制圖形}}}public static void main(String[] args) {new G_2().setVisible(true);} }

程序展示:

設(shè)置畫筆
語法:

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){//遍歷圖形數(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ù)說明

參數(shù)說明
width畫筆寬度默認(rèn)為1px
cap線端點(diǎn)裝飾
join應(yīng)用在路徑線段交匯處的裝飾
miterlimit斜接處的剪裁限制,參數(shù)必須大于等于1.0f
dash表示虛線模式的數(shù)組
dash_phase開始虛線模式的偏移量

cap參數(shù)的三個(gè)常量:

  • CAP_BUTT:末端圓滑
  • CAP_ROUND:末端平整
  • CAP_SQUARE:末端設(shè)置以線段寬度為邊長的方形
  • join參數(shù)的三個(gè)常量:

  • JOIN_BEVEL:平角
  • JOIN_MITER:尖角
  • JOIN_ROUND:圓角
  • 繪制文本

    1.設(shè)置字體
    可以設(shè)置名稱,樣式,字體大小
    語法:

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

    程序展示:

    例子:

    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("現(xiàn)在是:",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)

    參數(shù)表示要顯示的圖片對(duì)象,坐標(biāo),要通知的圖像觀察者
    例子:

    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);//設(shè)置窗體大小和位置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);} }

    程序展示:


    圖像翻轉(zhuǎn)
    語法:

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

    參數(shù)說明:

    參數(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)圖像語法:

    rotate(double 旋轉(zhuǎn)弧度)

    我們可以使用Math類中的toRadians方法把旋轉(zhuǎn)角度轉(zhuǎn)化為弧度
    例子:

    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("圖像旋轉(zhuǎn)");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);} }

    程序展示:

    總結(jié)

    以上是生活随笔為你收集整理的Java绘图,图像处理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。