JAVA圖形設(shè)計(jì)卷的知識點(diǎn)有哪些-創(chuàng)新互聯(lián)

本篇內(nèi)容介紹了“JAVA圖形設(shè)計(jì)卷的知識點(diǎn)有哪些”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

在靜寧等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站設(shè)計(jì) 網(wǎng)站設(shè)計(jì)制作按需定制,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站制作,成都全網(wǎng)營銷,成都外貿(mào)網(wǎng)站制作,靜寧網(wǎng)站建設(shè)費(fèi)用合理。

注意1:在AWT中提供的用戶接口構(gòu)件(如按鈕、列表、菜單、對話框等)不包含一些類似的純粹的繪制圖形的對象(如Line或Circle類)

詳細(xì)意思:由于原始的AWT在設(shè)計(jì)時(shí)不允許純粹的繪制圖形的對象,那么Rectangle、Polygon和Point沒有任何繪制圖形的能力。換句話說,
Rectangle、Polygon和Point不具備draw方法。您可做的,僅僅是可以設(shè)置和得到它們代表的幾何實(shí)體的信息。
為了代替那些純粹的、可繪制圖形的對象,AWT使用了一種簡單的——盡管不夠靈活并且不易擴(kuò)展——模式:
每個(gè)AWT構(gòu)件完全來自于它自己的java.awt.Graphics對象,盡管其中的一些圖形操作可以在與之相關(guān)的構(gòu)件中實(shí)現(xiàn)。
Graphics也可以向種各樣的輸出設(shè)備中繪制,像畫面外緩沖器和打印機(jī)——參見第24章“雙緩沖技術(shù)”和18.4節(jié)“打印”中的相關(guān)內(nèi)容。

請先看兩張表(都忽略了java.awt.peer方法):

        表3-1 傳遞一個(gè)對Graphics的引用的jdk方法
  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
   軟件包     類         方 法
  ─────────────────────────────────
  java.awt   Canvas       paint(Graphics g)
         Component     paint(Graphics g)
         Component     paintAll(Graphics g)
         Component     print(Graphics g)
         Component     printAll(Graphics g)
         Component     update(Graphics g)
         Container     paint(Graphics g)
         Container     paintComponents(Graphics g)
         Container     print(Graphics g)
         Container     printComponents(Graphics g)
         ScrollPane     printComponents(Graphics g)
  java.beans  Property_Enditor paintValue(Graphics g,Rectangle r)
         Property_EnditorSupport paintValue(Graphics g,Rectangle r)
  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

         表3-2 返回Graphics 引用的JDK方法
  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
   軟件包     類         方 法
  ─────────────────────────────────
  java.awt  Component     getGraphics()---(為最常使用)
        Image       getGraphics()
        PaintJob      getGraphics()
        Graphics      create()
        Graphics      create(int x,int y,int w,int h)
  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
1.java.awt.Graphics 作用:定義一個(gè)真正的工具,用來接受圖形操作(顯示圖像和文本、繪制和填充開關(guān)、剪貼圖像操作等)
幾乎在所有的applet程序(和應(yīng)用程序)中,都使用了AWT處理Graphics來為圖像服務(wù)。
某applet程序片斷:
public void paint(Graphics g){
g.drawString("Hello Graphics Java World",75,100);
...}
另外,在構(gòu)件內(nèi)部執(zhí)行圖像操作時(shí),每個(gè)Graphics中都保持了下面的圖形屬性:
·用來繪制和填充形狀的顏色。
·用來描述文本的字體。
·剪貼矩形。
·繪制模式(XOR或Paint)。
·用于顯示和剪貼坐標(biāo)的平移原點(diǎn)。
2.Graphics參數(shù)
Graphics類的兩個(gè)主要的職責(zé):
(1)設(shè)置和獲取圖形參數(shù)
(2)在輸出設(shè)備中執(zhí)行圖形操作(為主)

Graphics類中使用的四個(gè)參數(shù)
 表3-3
  | 方法
-----------------------|---------------------------------------------------------------------------- 
(a)顏色 void setColoer(Color color)
 Color getColor()
-----------------------|----------------------------------------------------------------------------
(b)字體 void setFont(Font f)
 和  Font getFont()
 -------------------------------------------------------------------
字體度量(屬性為只讀) FontMetrics getFontMetrics() 返回的字體度量,和Graphics當(dāng)前的字體結(jié)合在一起
 FontMetrics getFontMetrics(Font f) 返回的字體尺度和指定的字體結(jié)合在一起
-----------------------|----------------------------------------------------------------------------
(c)剪貼矩形 void setClip(int x,int y,int w,int h)
   Rectangle getClipBounds()
   void setClip(Shape) Shape接口是java 2D api的一部分,Shape所表示的形狀可以是非矩形的,
 所以它可以為輸出設(shè)備定義一個(gè)非矩形的剪貼矩形
 Shape getClip()
 void clipRect(int x,int y,int w,int h) 計(jì)算一個(gè)新的剪貼矩形,該剪貼矩形是原先剪貼矩形和
 方法中參數(shù)指定的剪貼矩形的交集
-----------------------|----------------------------------------------------------------------------
(d)圖形模式(屬性為只寫) void setPaintMode()(默認(rèn)模式) 設(shè)置paint圖形模式,意味著后面的著色操作將改寫現(xiàn)有的圖形
 void set setXORMode() 允許繪制和擦掉現(xiàn)在圖形而不干擾其下面的圖形
-----------------------|-------------------------------------------------------------------------------------------
簡單的applet例子 卻要注意:
例1.
import java.applet.Applet;
import java.awt.*;
public class RectTest extends Applet{
public void paint(Graphics g){
g.drawRect(2,2,4,4);//*****行A
}
}
file://隨便一個(gè)HTML文件如001.html,

Sample Applet




file://命令行下輸入:appletviewer 001.html,
所繪制的坐標(biāo)路徑如下所示:為5像素單位見方的矩形
(2,2)→(6,2)→(6,6)→(2,6)→(2,2) 畫筆所采用的顏色則是通過調(diào)用Graphics.setColor(Color)來指定的。
注意:
通過調(diào)用Graphics.drawRect()繪制矩形時(shí),其結(jié)果將會在矩形的右邊和下邊各存在一個(gè)額外的像素行。這是因?yàn)閭鬟f到
Graphics.drawRect()中的參數(shù)定義的是畫筆遵循的路徑,而不是矩形自身的尺寸。由于畫筆是沿上面所講坐標(biāo)路徑繪制
矩形的,所以g.drawRect(2,2,4,4)實(shí)際上繪制出來的矩形的寬度和高度是5個(gè)像素單位——而不是你所想象的4個(gè)像素單位
技巧1:《圖形坐標(biāo)位于像素之間》,而不是在它們之上。指定坐標(biāo)的圖形方法指定畫筆路徑——通常是單位像素見方——的移動。
畫筆繪制像素的路徑是起點(diǎn)→右→下→左→起點(diǎn)。結(jié)果,繪制圖形狀外形的圖形方法將在矩形的右邊和下邊各存在一個(gè)額外的像素行
技巧1的使用:繪制構(gòu)件的四周邊界時(shí)

Public void paint(Graphics g){
Dimension size=getSize();
g.drawRect(0,0,size.width-1,size.height-1);//不減一的話,右邊緣和底邊緣邊界將被繪制在構(gòu)件的外面而看不到該部分。
}
把例1中的//*****行A---&gtg.drawRect(2,2,4,4);換成g.fillRect(2,2,4,4);
注意:此時(shí)傳遞給fillRect()的參數(shù)指定的坐標(biāo)路徑與在前面調(diào)用drawRect()時(shí)指定的坐標(biāo)路徑相同。但是,填充外形的Graphics
  方法將填充路徑的內(nèi)部,所以填充的矩形是4個(gè)像素寬和4個(gè)像素高
3.Graphics引用 
有兩種方法要用到對構(gòu)件的Graphics的引用。這兩種方法就是:覆蓋前面表3-1中的方法(傳遞一個(gè)對Graphics的引用),或調(diào)用表3-2
列出的方法(將返回對Graphics的引用)。值得注意的是從Component、Image和PrintJob中的getGraphics()方法返回的Graphics引
用并不是一個(gè)對Graphics引用的引用,而是返回一個(gè)初始Graphics的副本。在下面的章節(jié)中,你將會發(fā)現(xiàn)這是一個(gè)很重要的概念。

又一個(gè)簡單的applet例子:再強(qiáng)調(diào)一遍 Graphics引用引用和構(gòu)件相關(guān)的真實(shí)Graphics的副本
import java.applet.Applet;
import java.awt.*;
public class CopyTest extends Applet {
public void paint(Graphics g) {
setForeground(Color.yellow);//設(shè)置其前景顏色為黃色
g.drawLine(0,0,getSize().width-1, getSize().height-1);
}
}
結(jié)果可能不像你所期待那樣:在開始畫線時(shí),線的顏色可能不是黃的(黑色一閃,用IE看吧。)
原因:調(diào)用Component.setForeground(),改變構(gòu)件中Graphics的當(dāng)前顏色——在本例中,
該顏色被改變成黃色。SetFroeground()影響applet的Graphics,但并不影響傳遞給paint()
的Graphics的副本。因此,當(dāng)?shù)谝淮握{(diào)用paint()時(shí),線條的顏色并沒有變成黃色。當(dāng)調(diào)用
drawLine()時(shí),傳遞給paint()的Graphics和實(shí)際的Graphics并不同步。后來調(diào)用paint()時(shí),
傳遞給paint()的是applet的Graphics的一個(gè)新副本,因此setForeground()的調(diào)用結(jié)果可以
將applet的Graphics的當(dāng)前顏色改變成黃色。
如果將該applet程序改一下:
import java.applet.Applet;
import java.awt.*;
public class CopyTest2 extends Applet {
public void paint(Graphics g) {
setForeground(Color.yellow);// the next line would do just as well as the following
 // g.setColor(Color.yellow);
Graphics copy = getGraphics();
try {
System.out.println("g=" + g.getColor() +" copy=" + copy.getColor());
copy.drawLine(0,0,getSize().width-1, getSize().height-1);
}
finally {
copy.dispose();
}
}
} file://那么一開始畫出的線就是黃色的,DOS下打印出:
g=java.awt.Color[r=0,g=0,b=0] copy=java.awt.Color[r=255,g=255,b=0]
g=java.awt.Color[r=255,g=255,b=0] copy=java.awt.Color[r=255,g=255,b=0]
g=java.awt.Color[r=255,g=255,b=0] copy=java.awt.Color[r=255,g=255,b=0]

解釋:傳遞給paint()的Graphics將被忽略掉,通過調(diào)用getGraphics()方法得到一個(gè)新的Graphics,
應(yīng)用這個(gè)新的Graphics繪制直線。因?yàn)樵谡{(diào)用setForeground()之后獲取Graphics,所以Graphics
當(dāng)前的顏色,也就是線條的顏色,將變成黃色。
4.Graphics引用的壽命
注意:除了引用真實(shí)的副本外,傳遞給paint()和update()等方法的Graphics引用僅僅在方法的執(zhí)行過程中才有效。
一旦方法返回,引用將不再有效(強(qiáng)行重新載入如刷新等可重繪該applet)
例子:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class HoldRef extends Applet {
private Graphics oldg;
private boolean first = true;
public void paint(Graphics g) {
if(first) {
oldg = g;
first = false;
}
oldg.drawLine(0,0,getSize().width-1, getSize().height-1);
}
}
可以看出:傳遞給方法的Graphics引用壽命很短,因?yàn)樗鼈兪潜仨毺幚淼挠邢拶Y源,每個(gè)Graphics表示的圖像環(huán)境
是由本地的窗口系統(tǒng)提供的。圖像環(huán)境通常被設(shè)定在一個(gè)有限的數(shù)量內(nèi)可用,而且當(dāng)調(diào)用返回時(shí)傳遞Graphics引用的
調(diào)用者會很仔細(xì)地處理掉它。例如,當(dāng)調(diào)用Component.Paint()返回時(shí),調(diào)用者處理掉傳遞給paint()的Graphics。

5.雖然Java帶有垃圾收集,但是在AWT中仍有兩個(gè)地方供開發(fā)者處理有限的系統(tǒng)資源,處理Graphics就是其中之一
(注:窗口和對話框也必須被處理,請參見第16章“窗口、框架和對話框”)。在處理Graphics有兩個(gè)要注意的事項(xiàng),
即:什么時(shí)候需要處理和怎樣處理
處理Graphics規(guī)則:如果調(diào)用表3-2列出的getGraphics方法中的一個(gè)得到一個(gè)對Graphics的引用,
 或者通過Graphics.create()創(chuàng)建一個(gè)Graphics,那么就有責(zé)任對它們進(jìn)行處理
通過調(diào)用Graphics.dispose()處理Graphics,請看某程序片斷:
public void someMethodInAComponent(){//code fragment
Graphics g=getGraphics();
if(g!=null){
try{
file://do something with g- if an exception is thrown,
file://the finally block will be executed
}
finally{
g.dispose()//crucial至關(guān)重要,因?yàn)楹雎圆蛔鰧?dǎo)致窗口系統(tǒng)用完圖形環(huán)境,這在大部分操作上會引起問題
}
}
}
file://在這里要注意的是對g不做null檢測并不是無根據(jù)的,如果在構(gòu)件的同位體創(chuàng)建之前調(diào)用getGraphics()則它確實(shí)返回null
對Graphics.dispose()的調(diào)用被安置在finally塊中,而對Graphics的控制則在相應(yīng)的try塊中執(zhí)行。這保證調(diào)用dispose()
將被放在事件內(nèi),例外的情況則會被從try塊中拋出。
技巧2:《傳遞圖形引用的方法引用副本》
技巧2的使用:
Graphics表示本地圖形環(huán)境,是一個(gè)典型的有限資源。所以,被返回的Graphics引用必須通過調(diào)用Graphics.dispose()方法處理。
傳遞Graphics引用的方法,像Component.paint(),是一種不需要手工設(shè)置處理的方法——當(dāng)調(diào)用返回時(shí),方法的調(diào)用者必須處理 
大家累了吧?我也是。努努力!加加油!
6.繪制和填充形狀(直線(Lines)折線(Polylines)矩形(Rectangles)弧(Arcs)橢圓(Ovals)多邊形(Polygons)文本(Text)圖像(Images))
6.1畫直線 使用方法:Graphics.drawLine(int x,int y,int x2,int y2)
AWT不能畫不固定寬度的直線,使用圖形筆所畫的直線,其寬度一般是一個(gè)像素。
線經(jīng)常被繪制成實(shí)線——沒有規(guī)定線的模式,如點(diǎn)線或虛線。但是,在Java 2D API中,為不同的線類型和圖形筆尺寸提供廣泛的支持。
隨機(jī)直線例子:起點(diǎn),長度、方向和顏色都隨機(jī)
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class PickupSticks extends Applet {
private static Color[] colors = {
Color.white, Color.black, Color.blue, Color.red,
Color.yellow, Color.orange, Color.cyan, Color.pink,
Color.magenta, Color.green };
public void init() {
Button button = new Button("scatter");
add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
repaint();
}
});
}
public void paint(Graphics g) {
for(int i=0; i < 500; ++i) {
int x = (int)(Math.random()*100);
int y = (int)(Math.random()*100);
int deltax = (int)(Math.random()*100);
int deltay = (int)(Math.random()*100);
g.setColor(colors[(int)(Math.random()*10)]);
g.drawLine(x,y,x + deltax, y + deltay);
}
}
}
6.2畫折線 使用方法:drawPolyline(int[] xPoints,int[] yPoints,int numPoints)
 一個(gè)數(shù)組指定每個(gè)點(diǎn)的x坐標(biāo)值,另一個(gè)數(shù)組指定點(diǎn)的y坐標(biāo)值,要畫的折線的點(diǎn)數(shù)
 點(diǎn)數(shù)等于線段數(shù)+1,如果起始點(diǎn)和終點(diǎn)不重合,所畫的折線是不封閉的
折線例子:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Polylines extends Applet {
private static Color[] colors = {
Color.white, Color.black, Color.blue, Color.red,
Color.yellow, Color.orange, Color.cyan, Color.pink,
Color.magenta, Color.green };
public void init() {
Button button = new Button("repaint");
add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
Polylines.this.repaint();
}
});
}
public void paint(Graphics g) {
file://int arraySize = ((int)(Math.random()*100));//原例子
int arraySize =4;//線數(shù)過多,故我設(shè)為4
int[] xPoints = new int[arraySize];
int[] yPoints = new int[arraySize];//xPoints和yPoints要相等
file://int[] yPoints = new int[arraySize-1];//不相等,編譯時(shí)無錯(cuò),運(yùn)行時(shí)給你個(gè)驚喜:)。
for(int i=0; i < xPoints.length; ++i) {
xPoints[i] = ((int)(Math.random()*200));
yPoints[i] = ((int)(Math.random()*200));
}
g.setColor(colors[(int)(Math.random()*10)]);
g.drawPolyline(xPoints, yPoints, arraySize);
showStatus(arraySize + " points");
}

6.3繪制矩形:
矩形有3種:
(1)實(shí)體的(solid)
(2)圓角的(rounded)
(3)3D
繪制方法如下:
void clearRect(int x,int y,int w,int h)
void drawRect(int x,int y,int w,int h)
void drawRoundRect(int x,int y,int w,int h,int arcWidth,int arcHeight)
增加的參數(shù)arcWidth用來設(shè)置弧的水平直徑和arcHeight用來設(shè)置豎直方向上的直徑
void draw3DRect(int x,int y,int w,int h,boolean raise)
增加的參數(shù)raise用來指定3D效果是凸的還是凹的,ture時(shí),3D效果是凸的,false時(shí)3D效果是凹的
void fillRoundRect(int x,int y,int w,int h,int arcWidth,int arcHeight)
void fillRect(int x,int y,int w,int h)
void fill3DRect(int x,int y,int w,int h,boolean raise)
繪制矩形的例2:
file://applet程序中配備有三個(gè)按鈕和一個(gè)復(fù)選框,繪制矩形的代碼被封裝在applet的paint方法中
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class RandomRectangles extends Applet {
private static Color[] colors = {
Color.white, Color.black, Color.blue, Color.red,
Color.yellow, Color.orange, Color.cyan, Color.pink,
Color.magenta, Color.green };
private int numRects = 10;
private boolean fill = false,//復(fù)選框用于指定的矩形是否被填充
raise = false,
round = false,
threeD = false;//變量不能以數(shù)字開頭如3D樣
public void init() {//每個(gè)按鈕代表被繪制的矩形的類型
Button rectsButton = new Button("矩形");
Button roundbutton = new Button("圓角矩形");
Button threeDButton = new Button("3D矩形");
Checkbox fillCheckbox = new Checkbox("填充");
add(rectsButton);
add(roundButton);
add(threeDButton);
add(fillCheckbox);
rectsButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
round = false;
threeD = false;
repaint();
}
});
roundButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
round = true;
threeD = false;
repaint();
}
});
threeDButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
threeD = true;
round = false;
repaint();
}
});
fillCheckbox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
fill = ((Checkbox)(event.getsource())).getState();
}
});
}
file://繪制所有的矩形時(shí),采用的都是1像素寬的圖形筆,因?yàn)樵贏WT中,只允許這個(gè)尺寸
public void paint(Graphics g) {
for(int i=0; i < numRects; i++) {
Point lhc = randomPoint(); // left hand corner
Dimension size = randomDimension();
g.setColor(colors[(int)(Math.random()*10)]);
if(round) {
if(fill)
g.fillRoundRect(lhc.x,lhc.y,size.width,size.height,
(int)(Math.random()*250),
(int)(Math.random()*250));
else
g.drawRoundRect(lhc.x,lhc.y,size.width,size.height,
(int)(Math.random()*250),
(int)(Math.random()*250));
}
else if(threeD) {
file://繪制3D矩形前,Graphics的顏色被設(shè)置成亮灰色,才可以看見其3D效果
g.setColor(Color.lightGray);
if(fill)
g.fill3DRect(
lhc.x,lhc.y,size.width,size.height,raise);
else
g.draw3DRect(
lhc.x,lhc.y,size.width,size.height,raise);
}
else {
if(fill)
g.fillRect(lhc.x,lhc.y,size.width,size.height);
else
g.drawRect(lhc.x,lhc.y,size.width,size.height);
}
raise = raise ? false : true;
}
}
private Dimension randomDimension() {
return new Dimension((int)(Math.random()*250),
(int)(Math.random()*250));
}
private Point randomPoint() {
return new Point((int)(Math.random()*250),
(int)(Math.random()*250));
}
}
6.4畫?。夯∈俏ㄒ坏囊环N非封閉的、但可以填充的圖形
 繪制方法:void drawArc(int x,int y,int w,int h,int startAngle,int endAngle)
 void fillArc(int x,int y,int w,int h,int startAngle,int endAngle)
 參數(shù)意義:x,y,w(寬度)y(高度)弧指定坐標(biāo)路徑。最后兩個(gè)是弧的開始角度和結(jié)束角度
簡單例子:
import java.applet.Applet;
import java.awt.*;
public class DrawArc extends Applet {
public void paint(Graphics g) {
g.setColor(Color.blue);
g.drawArc(10,10,150,100,0,210);//寬度和高度分別是151和101個(gè)像素,知道為什么呢吧?
g.setColor(Color.yellow);
g.fillArc(50,50,100,200,90,270);//寬100,高200個(gè)像素,因?yàn)槭翘畛洹?br/>}
}
6.5繪制橢圓(not 圓角矩形)
繪制方法://寬高相同,所繪制的圖形是一個(gè)圓
void drawOval(int x,int y,int w,int h)
void fillOval(int x,int y,int w,int h)
參數(shù)意義:外框與橢圓相內(nèi)切的矩形框
聰明的你一定知道drawOval()所繪畫的橢圓,適合的矩形是w+1個(gè)像素寬和h+1個(gè)像素高
6.6繪制多邊形
繪制方法://與折線類似,另:初始點(diǎn)和結(jié)束點(diǎn)不是同一個(gè)點(diǎn)多邊形將自動閉合
void drawPloygon(int[] xPoints,int[] yPoints,int[] numPoints)
void drawPolygon(Polygon polygon)
void fillPloygon(int xPoints,int[] yPoints,int[] numPoints)
void fillPolygon(Polygon polygon)  
注意:盡管在AWT中提供非圖形Polygon和Rectangle類,但Graphics類不提供
  drawRect(Rectangle)方法,盡管存在一個(gè)drawPolygon(Polygon)方法。
6.7繪制文本
繪制方法:
void drawString(String s,int x,int y)
void drawChars(char[],int offset,int length,int x,int y)
void drawBytes(byte[],int offset,int length,int x,int y)
參數(shù)注意:所繪制文本的x,y位置對應(yīng)的是文本基線,不是文本的左上角,和矩形不一樣
 可參看windows的SDK編程。JDK與windows的SDK編程很相似,同時(shí)學(xué)習(xí)可互為注腳,互相提高。
簡單的一句話:字符以其底線為始,如A以大約左腳處為始。
技巧3:
如果字符串和矩形被繪制在同一個(gè)位置,則字符串將顯示在矩形的上方
offset和length參數(shù)分別用來規(guī)定開始位置在數(shù)組中的偏移量和繪制的字符數(shù),(Graphics類不提供旋轉(zhuǎn)文本的能力)
6.8轉(zhuǎn)換坐標(biāo)系原點(diǎn):如果不指定,則在Graphics坐標(biāo)系中,原點(diǎn)一般設(shè)置在左上角
轉(zhuǎn)換坐標(biāo)方法:Graphics.translate()
兩個(gè)整數(shù)參數(shù)值:描繪在原先的坐標(biāo)系統(tǒng)中的點(diǎn),在轉(zhuǎn)換后的坐標(biāo)系統(tǒng)中將成為新的原點(diǎn)
轉(zhuǎn)換坐標(biāo)的原因:一個(gè)原因是容器中沒有滾動條而又要滾動其內(nèi)容,如下例便是(點(diǎn)擊鼠標(biāo)可以滾動該圖形)
轉(zhuǎn)換坐標(biāo)系的例子:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class TranslateTest extends Applet {
Image image;
Point pressed = new Point(), lastTranslate = new Point();
public void init() {
image = getImage(getCodeBase(), "Rabbit.gif");//在IE緩存中隨便找一個(gè).gif文件即可,
try {
MediaTracker mt = new MediaTracker(this);
mt.addImage(image, 0);
mt.waitForID(0);
}
catch(InterruptedException e) {
e.printStackTrace();
}
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
java.awt.Point loc = e.getPoint();
file://Point loc = e.getPoint();//是原版,但編譯時(shí)說
file://--TranslateTest.java:19: incompatible t
file://--found  : java.awt.Point
file://--required: Point
file://--Point loc = e.getPoint(); 
file://--  ^
file://-----故加了java.awt.于前。什么原因呢?THINK IN JAVA中說Point有幾個(gè)類(awt中有,2D中有)。故...
// adjust mouse pressed location for
// translation ...
pressed.x = loc.x - lastTranslate.x;
pressed.y = loc.y - lastTranslate.y;
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
java.awt.Point loc = e.getPoint();//加了java.awt
Point translate = new Point(loc.x - pressed.x,
loc.y - pressed.y);
Graphics g = getGraphics();
try {
g.clearRect(0,0,
getSize().width,getSize().height);
g.translate(translate.x, translate.y);
showStatus("Translating Graphics: " +
translate);
g.drawImage(image, 0, 0, TranslateTest.this);
}
finally {
g.dispose();
}
lastTranslate = translate;
}
});
}
public void paint(Graphics g) {
g.drawImage(image, 0, 0, this);
}
}

6.9剪貼:每個(gè)Graphics都有一個(gè)關(guān)聯(lián)的剪貼矩形。剪貼矩形之所以這樣命名是因?yàn)樗鼈儚?fù)制它們表示的矩形。另外,
 隨著Java 2D API的發(fā)展,剪貼區(qū)域的開關(guān)可以被設(shè)置為任意的開關(guān),而不只局限于矩形
繪制方法:同表3-3
void setClip(int x,int y,int w,int) file://設(shè)置所要剪貼的區(qū)域是一個(gè)矩形區(qū)域
void setClip(Shape) file://設(shè)置所要剪貼的區(qū)域是任意形狀
Rectangle getClipBounds() file://返回剪貼區(qū)域是一個(gè)矩形區(qū)域
Shape getClip() file://返回剪貼區(qū)域是任意形狀
void clipRect(int x,int y,int w,int h) file://將剪貼矩形設(shè)置為當(dāng)前剪貼矩形和方法中變無指定的矩形的交集
本節(jié)例子請參看例2。
技巧4:《向Component.paint()傳遞被剪貼的Graphics》
對于AWT的初學(xué)者來講,經(jīng)常向paint方法傳遞那些剪貼矩形小于構(gòu)件的Graphics。結(jié)果,底層覆蓋的paint方法不干
擾它接受的Graphics的剪貼矩形。當(dāng)paint()繪制構(gòu)件的內(nèi)容時(shí),著色操作仍被執(zhí)行。在一些實(shí)例(如雙緩沖和動畫設(shè)計(jì))
中,以只更新被剪貼的區(qū)域代替繪制完整的內(nèi)容和依靠剪貼修復(fù)被損壞的區(qū)域,可以得到更好的效果
6.10圖形模式:參看表3-3
XOR模式最一般的用途是在現(xiàn)有的圖形上使用橡皮帶生成法(在制圖程序中和選擇多樣的對象時(shí)是一種很普通的用法)
面所有的例子都被設(shè)置成paint模式,所以在本節(jié)中我們將重點(diǎn)講述XOR模式
文檔中對XOR模式做了如下的描述的:Graphics.setXORMode(Color)
When drawing operations are performed,pixels which are the current color are changed to the
specify color,and vice versa。Pixels that are of colors other than those two color are changed
in an unpredictable but reversible manner;if the figure is drawn twice,then all pixels are
restored to their original values。
第二句中說明:如果在XOR模式連續(xù)兩次執(zhí)行復(fù)制圖像操作,其下面的圖形是不受影響的
使用XOR模式橡皮帶生成法的一個(gè)例子:在一個(gè)圖像上拉,生成一個(gè)矩形
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class xortest extends Applet {
java.awt.Point pressed, last;
Image image;
boolean firstRect;
public void init() {
image = getImage(getCodeBase(), "Rabbit.gif");
try {
MediaTracker mt = new MediaTracker(this);
mt.addImage(image, 0);
mt.waitForID(0);
}
catch(InterruptedException e) {
e.printStackTrace();
}
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
firstRect = true;
pressed = e.getPoint();
}
public void mouseReleased(MouseEvent e) {
if(pressed != null) {
java.awt.Point released = e.getPoint();
Rectangle clip = new Rectangle();
Graphics g = getGraphics();
Dimension size = getSize();
try {
clip.x = pressed.x;
clip.y = pressed.y;
clip.width =Math.abs(released.x - pressed.x);
clip.height =Math.abs(released.y - pressed.y);
g.clearRect(0,0,size.width,size.height);
g.setClip(clip);
g.drawImage(image, 0, 0, xortest.this);
}
finally {
g.dispose();
}
}
}
public void mouseClicked(MouseEvent e) {
repaint();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
java.awt.Point loc = e.getPoint();
Graphics g = getGraphics();
try {
g.setXORMode(getBackground());
if(firstRect) {
firstRect = false;
}
else {
g.drawRect(pressed.x, pressed.y,Math.abs(pressed.x - last.x),Math.abs(pressed.y - last.y));
}
g.drawRect(pressed.x, pressed.y,Math.abs(loc.x - pressed.x),Math.abs(loc.y - pressed.y));
last = e.getPoint();
}
finally {
g.dispose();
}
}
});
}
public void paint(Graphics g) {
g.drawImage(image, 0, 0, this);
}
}
6.11創(chuàng)建圖形
當(dāng)實(shí)現(xiàn)傳遞Graphics引用的方法時(shí),最好是保證方法的結(jié)果不會引起Graphics的變化。換句話說,當(dāng)方法返回時(shí),
Graphics和調(diào)用前的情況應(yīng)當(dāng)是相同的。在該規(guī)則中當(dāng)然也有例外,我們可以相當(dāng)有把握地說,當(dāng)調(diào)用paint()返
回時(shí),paint(Graphics)的調(diào)用者僅僅處理Graphics。因此,可以改變傳遞給paint()的Graphics,而忽視維持
它的初始狀態(tài)。但是,在另外的情況下,是否Graphics一定保留它的初始狀態(tài)并沒這么清楚。在這樣的情況下,
最好是接受安全的步驟并保證Graphics不被改變,有下面的兩個(gè)方法可以實(shí)現(xiàn)。
方法之一是:Graphics的所有的初始的特征可以被存儲在本地,然后在方法返回之前重新設(shè)置:
file://code fragment
public void notSurelfGraphicsShouldChangeState(Graphics g){
Color oldColor=g.getColor();
Font oldFont=g.getFont();
file://modify g's color and font and perfoRM graphical operations
g.setColor(oldColor);//restore old color
g.setfont(oldFont)//restore old font
很明顯,一兩個(gè)屬性更改無所謂,大量的屬性被修改后并被重新恢復(fù)就麻煩了
方法之二:那么創(chuàng)建Graphics的一個(gè)副本并用它代替?zhèn)鬟f給方法的Graphics是更方便的辦法
public void notSurelfGraphicsShouldChangeState(Graphics g){
Graphics copy=g.create();
try{
file://use copy for rendering
}
finally{
g.dispose();//crucial
}
}
對于4.Graphics引用的壽命一節(jié)中的例子,該applet程序保留一個(gè)Graphics引用,由于傳遞
給paint()的Graphics是由調(diào)用者來進(jìn)行處理的,所以Graphics引用僅在調(diào)用paint()期間有效 
現(xiàn)在我們創(chuàng)建一個(gè)Graphics的副本,該副本在調(diào)用paint()之后將不被處理,如下面
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class HoldRef2 extends Applet {
private Graphics copy;
private boolean first = true;
public void paint(Graphics g) {
if(first) {
// note: copy is never disposed off
file://致命錯(cuò)誤,即沒有處理Graphics的副本
file://在實(shí)際中,許可制造出一個(gè)類成員的副本引用,并在另一個(gè)方法中在適當(dāng)電動機(jī)及時(shí)處理它
copy = g.create();
copy.setColor(Color.red);
copy.setFont(new Font("Times Roman", Font.BOLD, 14));
first = false;
}
copy.drawString("Red Text", 10, 10);
}
}
創(chuàng)建Graphics的方法:
Graphics create()//創(chuàng)建的是一個(gè)精確的Graphics副本
Graphics create(int x,int y,int w,int h)//也是個(gè)副本,但返回的Graphics的原點(diǎn)被轉(zhuǎn)換為
(x,y)坐標(biāo),剪貼矩形轉(zhuǎn)換為原剪貼矩形和指定矩形的交集
例子:
import java.applet.Applet;
import java.awt.*;
public class CreateTest extends Applet {
private Image image;
public void init() {
MediaTracker mt = new MediaTracker(this);
image = getImage(getCodeBase(), "Rabbit.gif");
try {
mt.addImage(image, 0);
mt.waitForID(0);
}
catch(Exception e) {
e.printStackTrace();
}
}
public void paint(Graphics g) {
Graphics copy = g.create(image.getWidth(this),0,100,100);
try {
System.out.println("g: " + g.getClip().toString());
System.out.println("copy: " +copy.getClip().toString());
g.drawImage(image, 0, 0, this);
copy.drawImage(image, 0, 0, this);
}
finally {
copy.dispose();
}
}
}

加載圖像并通過applet的paint方法創(chuàng)建一個(gè)要傳遞的Graphics的副本,
副本的原點(diǎn)變換到(imw,0),imw是指圖形的寬度
副本也有它的剪貼矩形,設(shè)置為初始Graphics的剪貼矩形和由(image.getWidth(this),0,100,100)指定
的矩形的交集。因?yàn)槌跏糋raphics的剪貼矩形覆蓋由applet程序占領(lǐng)的區(qū)域,所以兩個(gè)矩形的交集由
(image.getWidth(this),0,100,100)決定。
提示:復(fù)制的Graphics已經(jīng)被轉(zhuǎn)換,因?yàn)檎{(diào)用drawImage()在(0,0)繪制圖像
啊!快結(jié)束了!略結(jié)一下:
(1)圖形操作的坐標(biāo)系被設(shè)定在設(shè)備的左上角,x和y軸的增長方向分別是向下和向右,坐標(biāo)位于像素之間,
Graphics方法中繪制形狀外形是通過設(shè)定圖形的坐標(biāo)而不是像素。圖形筆在坐標(biāo)路徑右邊和下邊移動,
因此形狀的外形導(dǎo)致在圖形右邊和下邊各有一個(gè)額外
的像素行。在另一方面,形狀填充圖形內(nèi)部,填充的尺寸和坐標(biāo)路徑一樣
(2)AWT不提供支持特殊形狀的類,如Line和Circle類.java.awt.Graphics提供大量的方法繪制和
填充圖形、繪制文本、設(shè)置圖形參數(shù).但有限制,故要深入請學(xué)Java 2D API
(3)每個(gè)Graphics聯(lián)系一個(gè)本機(jī)潛在的窗口系統(tǒng)中的圖形環(huán)境。因此,Graphics描述的是必須被手工處理的
有限資源。如果通過調(diào)用一個(gè)方法返回一個(gè)Graphics引用以得到一個(gè)Graphics,則Graphics.dispose()
必須被調(diào)用為Graphics釋放系統(tǒng)資源。另一方面,傳遞Graphics引用的方法通常不必處理Graphics。
一般地,這些方法調(diào)用者負(fù)責(zé)處理Graphics

“JAVA圖形設(shè)計(jì)卷的知識點(diǎn)有哪些”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

網(wǎng)站名稱:JAVA圖形設(shè)計(jì)卷的知識點(diǎn)有哪些-創(chuàng)新互聯(lián)
路徑分享:http://muchs.cn/article30/dhooso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃網(wǎng)站維護(hù)、自適應(yīng)網(wǎng)站、品牌網(wǎng)站制作網(wǎng)站營銷、ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)