1. 程式人生 > >【Java】【繪圖】

【Java】【繪圖】

sstream gpo blue web.xml 結果 epo href context 相關

繪圖原理(1)
Component類提供了兩個和繪圖相關最重要的?法:
1paint(Graphics g)繪制組件的外觀
2repaint()刷新組件的外觀
當組件第?次在屏幕顯示的時候,程序會?動的調paint()來繪制組件

繪圖原理(2)
在以下情況paint()將會被調?:
1、窗?最小化,再最大化
2、窗?的大小發?變化
3repaint函數被調

import javax.swing.*;
import java.awt.*;
/*
栗子 Java繪圖基礎
*/

//定義一個MyPanel(我自己的面板是用於繪圖和顯示繪圖的區域)
class MyPanel extends JPanel{
//重寫父類JPanel的paint方法
public void paint(Graphics g){
//1. 調用父類函數,完成初始化任務
super.paint(g);//super.paint(g)這句話不能少
System.out.println("Paint被調用");
//2. paint調用,先畫一個圓
g.drawOval(10,10,30,30);

}
}
public class Test_Graphics extends JFrame {
MyPanel mp = null;

public Test_Graphics(){
mp = new MyPanel();
this.add(mp);
this.setSize(400,300);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args){
Test_Graphics th = new Test_Graphics();
}



}

技術分享圖片

import javax.swing.*;
import java.awt.*;
/*
栗子 Graphics類你可以理解就是畫筆,為我們提供了各種繪制圖形的?法: [多看jdk幫助?檔]
1、畫直線 drawLine(int x1,int y1,int x2,int y2);
2、畫矩形邊框 drawRect(int x,int y,int width,int height);
3、畫橢圓邊框 drawOval(int x,int y,int width,int height);
4、填充矩形 fillRect(int x,int y,int width,int height);
5、填充橢圓 fillOval(int x,int y,int width,int height);
6、畫圖? drawImage(Image img.int x,int y,..);
7、畫字符串 drawString(String str,int x,int y);
8、設置畫筆的字體 setFont(Font font);
9、設置畫筆的顏? setColor(Color c);
*/

//定義一個MyPanel(我自己的面板是用於繪圖和顯示繪圖的區域)
class MyPanel extends JPanel{
//重寫父類JPanel的paint方法
public void paint(Graphics g){
//1. 調用父類函數,完成初始化任務
super.paint(g);//super.paint(g)這句話不能少
//畫直線
g.drawLine(10,10,40,10);
//矩形邊框
g.drawRect(50,50,40,40);
//橢圓邊框
g.drawOval(100,100,60,60);
//填充矩形
g.setColor(Color.blue);
g.fillRect(10,150,70,70);
//填充橢圓
g.setColor(Color.red);
g.fillOval(200,50,80,80);
}
}
public class Test_Graphics extends JFrame {
MyPanel mp = null;

public Test_Graphics(){
mp = new MyPanel();
this.add(mp);
this.setSize(400,300);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args){
Test_Graphics th = new Test_Graphics();
}



}

技術分享圖片

import javax.swing.*;
import java.awt.*;
/*
栗子 畫圖片 畫字
*/

//定義一個MyPanel(我自己的面板是用於繪圖和顯示繪圖的區域)
class MyPanel extends JPanel{
//重寫父類JPanel的paint方法
public void paint(Graphics g){
//1. 調用父類函數,完成初始化任務
super.paint(g);//super.paint(g)這句話不能少
//在畫板上畫圖片
System.out.println(getClass());
Image im = Toolkit.getDefaultToolkit().getImage("D:\\zidonghua\\java_test_one\\images\\dog.jpg");
//下面的方法就不行,因為這裏的getClass()的路徑是編譯後的路徑,即使從target開始往下找的路徑
//Image im = Toolkit.getDefaultToolkit().getImage(getClass().getResource("D:\\zidonghua\\java_test_one\\images\\dog.jpg"));
//實現
g.drawImage(im,0,0,300,200,this);
//畫字
g.setColor(Color.red);
g.setFont(new Font("黑體",Font.BOLD,40));
g.drawString("祖國萬歲",100,100);
//畫弧形
g.drawArc(100,100,120,200,-50,-100);
}
}
public class Test_Graphics extends JFrame {
MyPanel mp = null;

public Test_Graphics(){
mp = new MyPanel();
System.out.println(" " + getClass());
this.add(mp);
this.setSize(400,300);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args){
Test_Graphics th = new Test_Graphics();
}



}

技術分享圖片

【備註】java中Class.getResource用法(用於配置文件的讀取)

  用JAVA獲取文件,聽似簡單,但對於很多像我這樣的新人來說,還是掌握頗淺,用起來感覺頗深,大常最經常用的,就是用JAVA的File類,如要取得c:/test.txt文件,就會這樣用File file = new File("c:/test.txt");這樣用有什麽問題,相信大家都知道,就是路徑硬編碼,對於JAVA精神來說,應用應該一次成型,到處可用,並且從現實應用來講,最終生成的應用也會部署到Windows外的操作系統中,對於linux來說,在應用中用了c:/這樣的字樣,就是失敗,所以,我們應該盡量避免使用硬編碼,即直接使用絕對路徑。

 在Servlet應用中,有一個getRealPath(String str)的方法,這個方法盡管也可以動態地獲得文件的路徑,不秘直接手寫絕對路徑,但這也是一個不被建議使用的方法,那麽,我們有什麽方法可以更好地獲得文件呢?

那就是Class.getResource()與Class.getResourceAsStream()方法,但很多人還是不太懂它的用法,因為很多人(比如不久前的我)都不知道應該傳怎麽樣的參數給它,當然,有些人己經用得如火純青,這些人是不需要照顧的,在此僅給不會或者還不是很熟的人解釋一點點

比如我們有以下目錄

|--project

|--src

|--javaapplication

|--Test.java

|--file1.txt

|--file2.txt

|--build

|--javaapplication

|--Test.class

|--file3.txt

|--file4.txt

在上面的目錄中,有一個src目錄,這是JAVA源文件的目錄,有一個build目錄,這是JAVA編譯後文件(.class文件等)的存放目錄

那麽,我們在Test類中應該如何分別獲得

file1.txt file2.txt file3.txt file4.txt這四個文件呢?

首先講file3.txt與file4.txt

file3.txt:

方法一:File file3 = new File(Test.class.getResource("file3.txt").getFile());

方法二:File file3 = new File(Test.class.getResource("/javaapplication/file3.txt").getFile());

方法三:File file3 = new File(Test.class.getClassLoader().getResource("javaapplication/file3.txt").getFile());

file4.txt:

方法一:File file4 = new File(Test.class.getResource("/file4.txt").getFile());

方法二:File file4 = new File(Test.class.getClassLoader().getResource("file4.txt").getFile());

很好,我們可以有多種方法選擇,但是file1與file2文件呢?如何獲得?

答案是,你只能寫上它們的絕對路徑,不能像file3與file4一樣用class.getResource()這種方法獲得,它們的獲取方法如下

假如整個project目錄放在c:/下,那麽file1與file2的獲取方法分別為

file1.txt

方法一:File file1 = new File("c:/project/src/javaapplication/file1.txt");

方法二:。。。沒有

file2.txt

方法一:File file2 = new File("c:/project/src/file2.txt");

方法二:。。。也沒有

總結一下,就是你想獲得文件,你得從最終生成的.class文件為著手點,不要以.java文件的路徑為出發點,因為真正使用的就是.class,不會拿個.java文件就使用,因為java是編譯型語言嘛

至於getResouce()方法的參數,你以class為出發點,再結合相對路徑的概念,就可以準確地定位資源文件了,至於它的根目錄嘛,你用不同的IDE build出來是不同的位置下的,不過都是以頂層package作為根目錄,比如在Web應用中,有一個WEB-INF的目錄,WEB-INF目錄裏面除了web.xml文件外,還有一個classes目錄,沒錯了,它就是你這個WEB應用的package的頂層目錄,也是所有.class的根目錄“/”,假如clasaes目錄下面有一個file.txt文件,它的相對路徑就是"/file.txt",如果相對路徑不是以"/"開頭,那麽它就是相對於.class的路徑。。

還有一個getResourceAsStream()方法,參數是與getResouce()方法是一樣的,它相當於你用getResource()取得File文件後,再new InputStream(file)一樣的結果

Windows絕對路徑:

以盤符開始 如C:/a.txt Windows相對路徑: . 指的是當前目錄 .. 指的是當前目錄的上一級目錄 ./test 表示當前目錄下的test文件夾 /test 表示當前盤符下的test文件夾 www.2cto.com Linux絕對路徑:
以root根目錄/開始的路徑 如/ 表示root根目錄 Linux相對路徑: ./ 指的是當前目錄 ../ 指的是當前目錄的上一級目錄 .代表當前目錄,..代表上級目錄 好像是統一的。

在windows或者Linux獲取配置文件如:.properties文件時

使用Class.getResource或者ClassLoader.getResource獲取絕對路徑classpath時返回的值如下不同

1 String basepath=Thread.currentThread().getContextClassLoader().getResource("").toString();

  

Windows輸出

1 basepath=file:/D:/Workspaces/edu.bizoss.com/WebRoot/WEB-INF/classes/

 

Linux輸出

1 basepath=file:/Workspaces/edu.bizoss.com/WebRoot/WEB-INF/classes/

  

因此獲取絕對路徑的截取的也就不同

1 2 3 4 5 if(System.getProperty("file.separator").equals("\\")){ return basepath.substring(6,basepath.length()); }else{ return basepath.substring(5,basepath.length()); }

【Java】【繪圖】