Java ——數位影像處理(Java Graphics及其API簡介)

來源:互聯網
上載者:User

標籤:string   buffer   水平   file   hello   his   你好   dash   war   

1、建立一個Graphics對象
BufferedImage bi = new BufferedImage(120,120, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bi.createGraphics();
2、控製圖形邊緣反鋸齒
Graphics2D .setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
3、線段 / 折線

//兩點繪製線段
g2d.drawLine(50,50,200,50);
//多點繪製折線 點(50, 100), 點(100, 130), 點(150, 70), 點(200, 100)
int[] xpoints = new int[]{50,100,150,200};
int[] ypoints = new int[]{100,130,70,100};
int npoints=4;
g2d.drawPolyline(xpoints,ypoints,npoints);
g2d.dispose();
//兩點繪製線段(設定線寬為5px): 點(50, 150), 點(200, 150)
BasicStroke bs1 = new BasicStroke(5);
g2d.setStroke(bs1);
g2d.drawLine(50,150,200,150);
//繪製虛線: 將虛線分為若干段( 實線段 和 空白段 都認為是一段), 實線段 和 空白段 交替繪製,
// 繪製的每一段(包括 實線段 和 空白段)的 長度 從 dash 虛線模式數組中取值(從首
// 元素開始迴圈取值), 下面數組即表示每段長度分別為: 5px, 10px, 5px, 10px, ...
float[] dash = new float[]{5,10};
BasicStroke bs2=new BasicStroke(1,BasicStroke.CAP_SQUARE,BasicStroke.JOIN_MITER,10.0f,dash,0.0f);
g2d.setStroke(bs2);
g2d.drawLine(50,200,200,200);

g2d.dispose();

  4、矩形

// 1. 繪製一個矩形: 起點(30, 20), 寬80, 高100
g2d.drawRect(30,20,80,100);

// 2. 填充一個矩形
g2d.fillRect(30,20,80,100);

// 3. 繪製一個圓角矩形: 起點(30, 150), 寬80, 高100, 圓角寬30, 圓角高30
g2d.drawRoundRect(30,150,80,100,30,30);

// 4. 繪製一個多邊形(收尾相連): 點(140, 150), 點(180, 250), 點(220, 200)
int[] xpoints = new int[]{140,180,220};
int[] ypoints = new int[]{150,250,200};
int npoints = 3;
g2d.drawPolygon(xpoints,ypoints,npoints);

g2d.dispose();

  5、圓弧、扇形

// 1. 繪製一條圓弧: 橢圓的外切矩形 左上方座標為(0, 0), 寬100, 高100,
// 弧的開始角度為0度, 需要繪製的角度數為-90度,
// 橢圓右邊水平線為0度, 逆時針為正角度, 順時針為負角度
g2d.drawArc(0,0,100,100,0,-90);
// 2. 繪製一個圓: 圓的外切矩形 左上方座標為(120, 20), 寬高為100
g2d.drawArc(120,20,100,100,0,360);
// 3. 填充一個扇形
g2d.fillArc(80, 150, 100, 100, 90, 270);

g2d.dispose();

  6、橢圓

// 1. 繪製一個圓: 圓的外切矩形 左上方座標為(0, 0), 寬高為100
g2d.drawOval(0,0,100,100);
// 2. 填充一個橢圓
g2d.fillOval(120, 100, 100, 150);
g2d.dispose();

  7、圖片

String filepath = "C:/Users/LENOVO/Desktop/demo.jpg";
Image image = Toolkit.getDefaultToolkit().getImage(filepath);
g2d.drawImage(image,50,50,image.getWidth(this),image.getHeight(this),this);

g2d.dispose();

  8、文本

// 設定字型樣式, null 表示使用預設字型, Font.PLAIN 為普通樣式, 大小為 25px
g2d.setFont(new Font(null,Font.PLAIN,25));

// 繪製文本, 其中座標參數指的是文本繪製後的 左下角 的位置
// 首次繪製需要初始化字型, 可能需要較耗時
g2d.drawString("Hello world!",20,60);
g2d.drawString("你好,世界!",20,120);

 

  9、繪製太極圖形

private void paintTAiJi(Graphics g){
mf.setTitle("太極");
Graphics2D g2d = (Graphics2D) g.create();

g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

Shape lefthalfCircle = new Ellipse2D.Double(10,10,300,300);
Shape righthalfCircle = new Ellipse2D.Double(10,10,300,300);
Shape innerCircle1 = new Ellipse2D.Double(85,10,150,150);
Shape innerCircle2 = new Ellipse2D.Double(85,160,150,150);

Shape rectangel1 = new Rectangle2D.Double(160,10,150,300);
Shape rectangle2 = new Rectangle2D.Double(10,10,150,300);

Area left = new Area(lefthalfCircle);
Area right = new Area(righthalfCircle);

Area area1 = new Area(rectangel1);
Area area2 = new Area(rectangle2);
//左半圓白色,右半圓黑色
left.subtract(area1);
right.subtract(area2);

Area inner1 = new Area(innerCircle1);
Area inner2 = new Area(innerCircle2);

left.add(inner1);
right.add(inner2);

right.subtract(inner1);
//小白圓
Shape minorWhiteCircle = new Ellipse2D.Double(150,70,20,20);
//小黑圓
Shape minorBalckCircle = new Ellipse2D.Double(150,230,20,20);

//
g2d.setPaint(Color.WHITE);
g2d.fill(left);
g2d.setPaint(Color.black);
g2d.fill(right);

g2d.fill(minorWhiteCircle);
g2d.setPaint(Color.WHITE);
g2d.fill(minorBalckCircle);


}

10、單一顏色填充

private void singleColor(Graphics g){
mf.setTitle("8.畫筆顏色設定");
Graphics2D g2d = (Graphics2D) g.create();

g2d.setPaint(Color.blue);

g2d.fillRect(0,0,this.getWidth(),this.getHeight());
}

11、設定漸層色

private void setGradient(Graphics g){
mf.setTitle("9.漸層色填充");
Graphics2D g2d = (Graphics2D) g.create();

Color secondColor = new Color(99,153,255);
//水平方向填充
//GradientPaint hLinePaint = new GradientPaint(0,0,Color.BLACK,this.getWidth(),0,secondColor);
//豎直方向填充
GradientPaint hLinePaint = new GradientPaint(0,0,Color.BLACK,0,this.getHeight(),secondColor);
g2d.setPaint(hLinePaint);

g2d.fillRect(0,0,this.getWidth(),this.getHeight());

}

12、圓周放射狀漸層顏色填充

private void radialGradientPaint(Graphics g){
mf.setTitle("10.圓周放射狀漸層顏色填充");
Graphics2D g2d = (Graphics2D) g.create();

float cx = this.getWidth()/2;
float cy = this.getHeight()/2;//(cx,cy為圓心座標)
float radius = Math.min(cx,cy);//圓半徑
float[] fractions = new float[]{0.1f,0.5f,1.0f};//色彩漸層主要畫面格位置
Color[] colors = new Color[]{Color.red,Color.GREEN,Color.black};//顏色數組
RadialGradientPaint rgp = new RadialGradientPaint(cx,cy,radius,fractions,colors, MultipleGradientPaint.CycleMethod.NO_CYCLE);
g2d.setPaint(rgp);

g2d.fillRect(0,0,this.getWidth(),this.getHeight());
}

 

 

 



Java ——數位影像處理(Java Graphics及其API簡介)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.