자바 용 Graphic2D 그림 그리 기 간단 한 예

3522 단어
더 읽 기
package graphic2d;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

import javax.imageio.ImageIO;

public class Graphic2DTest {

	public static void draw() throws IOException{
		//   =480, =640   
		int width=480,hight=720;
		BufferedImage image = new BufferedImage(width,hight,BufferedImage.TYPE_INT_RGB);
		//       ,graphics       
		Graphics2D graphics = (Graphics2D)image.getGraphics();
		
		//      
		graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		
		//            
		graphics.setColor(Color.ORANGE);	//GREEN:  ;    :RED;     :GRAY
		graphics.fillRect(0, 0, 240, 720);
		//            
		graphics.setColor(Color.PINK);	
		graphics.fillRect(240, 0, 240, 720);
		
		//     
		Random random = new Random();
		/*
		 *    x,y   ,          
		 */
		graphics.setColor(Color.BLACK);
		int x=100,y=100,x1=100,y1=y;
		graphics.drawLine(x,y,x+x1,y1);
		/*
		 *      
		 */
		int[] xPoints = {100,100,250,250};
		int[] yPoints = {180,150,150,180};
		graphics.drawPolyline(xPoints, yPoints, 4);
		/*
		 *          (   )
		 */
		int[] xPoints1 = {100,100,200};
		int[] yPoints1 = {240,320,280};
		graphics.drawPolygon(xPoints1, yPoints1, 3);
		/*
		 *          (  )
		 */
		int[] xPoints2 = {240,300,360,300};
		int[] yPoints2 = {280,240,280,320};
		graphics.drawPolygon(xPoints2, yPoints2, 4);
		graphics.setColor(Color.ORANGE);
		graphics.fillPolygon(xPoints2, yPoints2, 4);
		/*
		 *        
		 */
		graphics.setColor(Color.GREEN);
		int xOval=100,yOval=360;
		graphics.drawOval(xOval, yOval, 100, 100);

		/*
		 *      
		 */
		//graphics.setColor(Color.GRAY);//--         。GREEN:  ;    :RED;     :GRAY
		int xRect=240,yRect=360;
		graphics.drawRect(xRect, yRect, 200, 100);

		//      
		graphics.setColor(new Color( 20+random.nextInt(100),  20+random.nextInt(100),  20+random.nextInt(100) ));
		//      、  
		graphics.drawString("  ",100+50,100-5);
		graphics.drawString("  ", 200, 150-5);
		graphics.drawString("     ", 110, 280);
		graphics.drawString("    ", 300-20, 280);
		graphics.drawString("   ", 100+50, 360+50);
		graphics.drawString("  ", 240+50, 360+50);
		//graphics.drawString("       ", 100, 540);
		
		//graphics.setPaintMode();
		//graphics.translate(400, 600);
		
		graphics.dispose();//                       
		FileOutputStream out=new FileOutputStream("d:/1.jpeg");
		ImageIO.write(image,"JPEG",out);
		
		out.flush();
		out.close();
		//super.doGet(request, response);

	}
	
	
	
	
	
	
	public static void main(String[] args) {
		try {
			draw();
		} catch (IOException e) {
			
			e.printStackTrace();
		}

	}

}

 
 
네트워크

좋은 웹페이지 즐겨찾기