import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class DrawCircle { static public void main(String args[]) throws Exception { try { int width = 200, height = 200; // TYPE_INT_ARGB specifies the image format: 8-bit RGBA packed // into integer pixels BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D ig2 = bi.createGraphics(); // set สีให้เป็นสีแดง ig2.setColor(Color.RED); // drawOval(x coordinate ของมุมซ้ายบน, y coordinate ของมุมซ้ายบน, width ของวงกลม, height ของวงกลม) // ดังนั้นเมื่อจะวาดวงกลมจาก xc,yc,r จะต้องนำ xc,yc,r มาคำนวณก่อนเพื่อให้เข้ากับคำสั่ง drawOval ได้ // สมมติว่า xc = 100, yc = 100, r = 80 // จะได้ x coordinate = 100-80, y coordinate = 100-80, width = r*2, height=r*2 ig2.drawOval(20,20,160,160); // เขียนภาพลงไฟล์ ImageIO.write(bi, "PNG", new File("c:\\yourImageName.PNG")); // เนื่องจากตัวอย่างในโปรแกรมนี้เป็นการสร้างภาพ BufferedImage ใหม่ขึ้นมา ผลลัพธ์ที่ได้จึงได้ background สีดำ // และเส้นวงกลมสีแดง ถ้าจะนำภาพที่อ่านเข้ามาจากไฟล์มาเขียนวงกลมสีแดงทับ ก็ให้ทำการอ่านภาพ // เข้ามาเป็น BufferedImage ตามปกติ แล้วเอา BufferedImage นั้นมาเขียนวงกลมสีแดงทับ // ก็จะได้ภาพเดิม ที่มีการเขียนวงกลมสีแดง เติมลงไป } catch (IOException ie) { ie.printStackTrace(); } } }