1. 程式人生 > >Java繪圖功能簡介-Graphics

Java繪圖功能簡介-Graphics

package hhxy.Test;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;

public class GraphicsFrame extends JFrame{
   public GraphicsFrame(){
	   super("Graphics範例");
	   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	   setLocation(300, 250);
	   setSize(400,180);
	   setVisible(true);
   }
   public void paint(Graphics g){
	   g.setColor(Color.BLACK);
	   g.drawLine(0, 35, 100, 35);//畫一條直線
	   
	   g.setColor(Color.RED);
	   g.drawRect(5, 40, 90, 55);//畫空心矩形
	   g.fillRect(100, 40, 90, 55);//畫實心矩形
	   
	   g.setColor(Color.ORANGE);
	   g.drawRoundRect(195, 40, 90, 55, 50, 50);//畫空心圓角矩形
	   g.fillRoundRect(290, 40, 90, 55, 20, 20);//畫實心圓角矩形
	   
	   g.setColor(Color.YELLOW);
	   g.draw3DRect(5, 100, 90, 55, true);//畫空心3D矩形
	   g.fill3DRect(100, 100, 90, 55, true);//畫實心3D矩形
	   g.setColor(Color.GREEN);
	   g.drawOval(195, 100, 90, 55);//畫空心橢圓
	   g.fillOval(290, 100, 90, 55);//畫實心橢圓
   }
   
	public static void main(String[] args) {
	new GraphicsFrame();
	}

}

結果: