等等會比較在Graphics和Graphics2D設定畫筆粗細會有何種變化,如果重複在image繪圖,圖會長的怎樣?
下面程式碼是第一個實驗,用來比較Graphics和Graphics2D設定畫筆粗細會有何種變化
import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class Circle { public static void main(String[] args) throws IOException { BufferedImage bufferedImage = new BufferedImage(400, 400, BufferedImage.TYPE_INT_RGB); Graphics graphics = bufferedImage.createGraphics(); graphics.setColor(Color.BLUE); graphics.setXORMode(Color.GRAY); graphics.drawArc(0, 0, 400, 400, 0, 360); ImageIO.write(bufferedImage, "jpeg", new File("test.jpeg")); BufferedImage bufferedImage2 = new BufferedImage(400, 400, BufferedImage.TYPE_INT_RGB); Graphics2D graphics2d = bufferedImage2.createGraphics(); graphics2d.setStroke(new BasicStroke(10)); graphics2d.setColor(Color.BLACK); graphics2d.setXORMode(Color.PINK); graphics2d.drawArc(0, 0, 400, 400, 0, 360); ImageIO.write(bufferedImage2, "jpeg", new File("test2.jpeg")); BufferedImage bufferedImage3 = new BufferedImage(400, 400, BufferedImage.TYPE_INT_RGB); Graphics graphics2 = bufferedImage3.createGraphics(); graphics2.setColor(Color.BLUE); graphics2.setXORMode(Color.GREEN); graphics2.drawArc(0, 0, 400, 400, 0, 360); ((Graphics2D) graphics2).setStroke(new BasicStroke(1000)); ImageIO.write(bufferedImage3, "jpeg", new File("test3.jpeg")); } }
bufferedImage是使用Graphics來繪畫一個圓形,Graphics本身沒有設定畫筆的方法,畫出來的圖如下
bufferedImage2是使用Graphics2D來繪畫一個圓形,Graphics2D有設定畫筆的方法『setStroke』,畫出來的圖如下
bufferedImage3是使用Graphics來繪畫一個圓形,Graphics2D就是從Graphics繼承而來的,試著強制轉型使用Graphics2D方法來設定畫筆,畫出來的圖如下
看來只能在Graphics2D才能設定畫筆了XDDD(或許是我太笨用不出來XD)
下面程式碼是第二個實驗,嘗試重複在image繪圖會出現怎樣的圖
import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class Circle { public static void main(String[] args) throws IOException { BufferedImage bufferedImage = new BufferedImage(400, 400, BufferedImage.TYPE_INT_RGB); Graphics graphics = bufferedImage.createGraphics(); graphics.setColor(Color.BLUE); graphics.setXORMode(Color.GRAY); graphics.drawArc(0, 0, 400, 400, 0, 360); ImageIO.write(bufferedImage, "jpeg", new File("test.jpeg")); Graphics2D graphics2d = bufferedImage.createGraphics(); graphics2d.setStroke(new BasicStroke(10)); graphics2d.setColor(Color.BLACK); graphics2d.setXORMode(Color.PINK); graphics2d.drawArc(0, 0, 400, 400, 0, 360); ImageIO.write(bufferedImage, "jpeg", new File("test2.jpeg")); } }
第一個圖如下
第二個圖就會在第一張圖重複繪畫
參考資料:
http://hi.baidu.com/songxulu/item/473ae938ba03a7c9392ffaee
http://www.javaworld.com.tw/jute/post/view?bid=5&id=152045&sty=0
http://www.wretch.cc/blog/lhu94a/23992322
http://algs4.cs.princeton.edu/41undirected/Cycle.java.html