----------------siwuxie095
工程名:TestSwingPaintAPI
包名:com.siwuxie095.swingpaint
類名:SwingPaintAPI.java(主類)、MyPanel.java
向 com.siwuxie095.swingpaint 包中放入圖像文件:img.png
工程結構目錄如下:
SwingPaintAPI.java(主類):
package com.siwuxie095.swingpaint;
import java.awt.BorderLayout; import java.awt.EventQueue;
import javax.swing.JFrame; import javax.swing.border.EmptyBorder;
public class SwingPaintAPI extends JFrame {
//將原本聲明的 JPanel 注釋掉,改為 MyPanel //private JPanel contentPane; private MyPanel contentPane;
/** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { SwingPaintAPI frame = new SwingPaintAPI(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); }
/** * Create the frame. */ public SwingPaintAPI() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300);
//將原本的實例化方式注釋掉,改為 MyPanel() //contentPane = new JPanel(); contentPane=new MyPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(0, 0)); setContentPane(contentPane); }
} |
MyPanel.java:
package com.siwuxie095.swingpaint;
import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.IOException;
import javax.imageio.ImageIO; import javax.swing.JPanel;
//MyPanel 繼承自 JPanel public class MyPanel extends JPanel {
//創建一個 BufferedImage BufferedImage image=null;
public MyPanel() {
try { //使用 ImageIO.read()讀取圖像,傳入 URL //可以是本地圖像,也可以是網絡圖像 //這里讀取本地圖像,因為使用的是 getClass().getResource() //所以 img.png 必須和調用的類 MyPanel 在同一個包中 //有異常拋出,使用 try catch 捕獲 image=ImageIO.read(getClass().getResource("img.png"));
} catch (IOException e) { e.printStackTrace(); } }
//覆蓋 JPanel 的 paintComponent() 方法, //右鍵->Source->Override/Implement Methods->JComponent
//對於每一個組件來說,paintComponent() 方法是繪制組件本身 //傳入 Graphics,通過它在界面繪制圖像 @Override protected void paintComponent(Graphics g) { //注釋掉默認的從父類繼承的繪圖方法 //super.paintComponent(g);
//最常用的繪圖方法,很多是以 draw 和 fill 開頭的 //draw 方法繪制的都是線框、輪廓(空心),而 fill 方法繪制的是填充的圖像(實心) //對於一般的 draw 方法,都會有對應的 fill 方法(空心對實心)
//先使用 setColor() 為當前的繪圖指定顏色 //使用匿名對象,創建一個新的Color對象 //每一個 rgb 的值最大為 255 ,最小為 0 //可以為不同的形狀填充不同的顏色 g.setColor(new Color(255,0,0));//紅色
//繪制方塊,需要指定 X Y 坐標,寬度,高度 // 0 0 即從左上角開始繪制 g.drawRect(0, 0, 100, 100);
//也可以使用靜態方法通過類調用 g.setColor(Color.GREEN); //繪制圓形、橢圓形,需要指定 X Y 坐標,寬度,高度 //如果寬高一致,繪制的就是圓形 g.drawOval(0, 0, 100, 100);//圓形 g.setColor(Color.BLUE); g.drawOval(0, 25, 100, 50);//橢圓形
g.setColor(Color.ORANGE); //填充方塊,需要指定 X Y 坐標,寬度,高度 g.fillRect(100, 100, 100, 100);
//填充帶圓角效果的方塊,需要指定 X Y 坐標,寬度,高度,圓角的弧寬,圓角的弧高 //一般情況下,將圓角的弧寬,圓角的弧高,兩個弧度值設置成相等 //弧寬越大,則 X 方向上圓角越長,弧高越大,則 Y 方向上圓角越長 //弧寬與弧高指定了 X Y 方向上圓角的大小 g.fillRoundRect(200, 0, 100, 100, 10, 10); g.fillRoundRect(300, 100, 100, 100, 200, 50);
//繪制 String,需要指定 X Y 坐標 //繪制 Bytes Chars 同 String //設置顏色與字體 g.setColor(Color.YELLOW); g.setFont(new Font("Arial", Font.BOLD, 20)); g.drawString("TestAPI", 110, 50);
g.setColor(Color.CYAN); //繪制弧線 需要指定 x y 坐標,寬度,高度,起始角度,弧線延長的角度 //繪制時按照逆時針繪制弧線 // //先畫一個矩形,然后以這個矩形的中心為所要畫的弧的中心, //以水平向右為 0 度,逆時針為正方向 g.drawArc(0, 200, 100, 100, 270, 90); g.fillArc(0, 200, 100, 100, 0, 270); g.fillArc(100, 200, 100, 200, 0, 90); g.drawArc(100, 200, 100, 200, 90, 90); g.fillArc(200, 200, 200, 200, 30, 120);
g.setColor(new Color(0,0,0)); //繪制直線 需要指定兩組 X Y 坐標 g.drawLine(200, 100, 300, 200);
//繪制圖像,選擇傳參最少的方法 //需要指定:圖像對象image,X Y 坐標,observer(可指定為空) //對於 image 可以在構造函數 MyPanel() 中加載圖像 if (image!=null) { g.drawImage(image, 0, 100, null); //繪制圖像時可以指定圖像的大小 }
//繪制多邊形,傳入一組 X 坐標,一組 Y 坐標,和 坐標數目 g.fillPolygon(new int[]{350,300,333,366,400}, new int[]{0,50,100,100,50}, 5);
}
} |
修改 SwingPaintAPI.java(主類) 中的 contentPane 的
聲明與實例化方式:
在 MyPanel.java 中覆蓋 JPanel 的 paintComponent() 方法
右鍵->Source->Override/Implement Methods->JComponent
運行程序:
【made by siwuxie095】