它是java.awt 套件中的一個抽象類,它擴展了java.lang 套件中的Object 類,充當所有圖形上下文的超類,允許在應用程式中繪製各種元件,這些元件可以輕鬆地實現到各種設備上或許多真實圖像。
廣告 該類別中的熱門課程 JAVA 掌握 - 專業化 | 78 課程系列 | 15 次模擬測驗開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
Graphics類別的每個物件都是實現小程式基本功能所需的所有方法的完整封裝,因此它的狀態包含了與要繪製的組件、當前剪輯、當前顏色、XOR 替代顏色、字體或來源翻譯。
帶參數的語法
public abstract class Graphics extends Object
Graphics類別是一個抽象類別;因此,我們無法製造它的物件;相反,我們需要使用它的子類別之一,例如 DebugGraphics、Graphics2D。而且還是公開課;可以使用任何類別來存取它。
它擴展了java.lang套件中的Object類,從而擴展了它的所有功能,例如clone、equals等
Graphics 類別用於在螢幕上繪製不同的視覺化元件,螢幕被視為由輸出裝置像素之間的無數像素組成的繪圖板。作為函數參數給出的所有座標都被視為相對於觸發該方法之前已平移的原點。以下是我們在任何方法中給出不同點時的過程:-
可以執行的所有操作都會修改指定形狀內的像素,並使用 Graphics 類別的物件進行控制。該區域稱為使用者剪輯,只能使用 setClip 和 ClipReact 方法進行修改。
指定主剪輯區域的裝置剪輯與上述使用者剪輯相結合定義了一個複合剪輯,該複合剪輯定義了最終剪輯的區域。所有繪圖或文字均以當前顏色、當前字體並使用當前繪畫模式完成。
此函數用於建立一個圓角矩形,其中
a1 – 此參數表示要繪製的矩形左上角的 x 座標。
b1 – 此參數表示要繪製的矩形左上角的 y 座標。
width – 此參數表示要繪製的矩形的寬度。
height – 此參數表示要繪製的矩形的高度。
horArc – 此參數表示要繪製的矩形所有角的圓弧的水平直徑。
vertArc – 此參數表示要繪製的矩形所有角的圓弧的垂直直徑。
左邊緣 = x 右邊緣 = x+寬度 -1
頂部邊緣 = y 與底部邊緣 = y+高度 -1
此方法用於以指定顏色作為目前顏色填滿圓角矩形。參數解釋與drawRoundRect()方法中給出的相同。
此方法用於將目前剪輯與矩形的規格相交。如果目前剪輯區域為空,則指定的矩形將設定為新剪輯,可以使用 setClip 方法對其進行修改。這些操作不會影響剪切區域的外部。
This method is used to paint a 3-D highlighted rectangle filled with the color specified using the setColor method. To give a 3D look to the figure, edges will be beveled to some extent and highlighted from the top left corner.
Parameters:
a1 –This argument denotes the x coordinate of the rectangle’s top-left corner to be drawn.
b1 -This argument denotes the y coordinate of the rectangle’s top-left corner to be drawn.
width – This argument denotes the width of the rectangle to be drawn.
height – This argument denotes the height of the rectangle to be drawn.
leveled – a boolean value, if it Is true – rectangle made will be shown as leveled above the surface; otherwise, it will be shown on the same level of the surface.
This method is used to draw the empty oval in the boundaries of the rectangle whose dimensions have been specified. The area of this oval extends upto width+1 pixels and height+1 pixels.
Parameters:
a –This argument denotes the x coordinate of the top left corner of the oval.
b – This argument denotes the y coordinate of the top left corner of the oval.
width1 –This argument denotes the width of the oval.
height1 –This argument denotes the height of the oval.
This method is used to set the current color for the graphics object. It takes the final variable as the value of color from the Color class in java.awt package. All the operations following this line will use this particular color.
Parameters:
c – the new color.
This method is used to draw a line, using the current color, between the points (a1, b1) and (a2, b2) in this graphics context’s coordinate system.
Parameters:
a1 –x coordinate of the starting point of the line.
b1 – y coordinate of the starting point of the line
a2 – x coordinate of the ending point of the line.
b2 – y coordinate of the ending point of the line.
Different examples are mentioned below:
Let’s draw a simple applet in java
Code:
import java.awt.*; import java.awt.event.*; import <u>java.awt.geom</u>.*; public class <u>Demo </u>extends Frame { public Demo(){ prepareWindow(); } @Override public void paint(Graphics g) { g.setColor(Color.GRAY); Font currentFont = new Font("Berlin Sans FB Demi",Font.ITALIC, 24); g.setFont(currentFont); g.setColor(Color.BLUE); g.drawString("Welcome to Graphics Class", 100, 150); g.setColor(Color.GREEN); g.drawLine(100, 200, 400, 200); } public static void main(String[] args){ Demo awtGraphicsDemo = new Demo(); awtGraphicsDemo.setVisible(true); } private void prepareWindow(){ setSize(450,400); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent){ System.exit(0); } }); } }
Output:
Code:
import java.awt.*; import java.awt.event.*; import <u>java.awt.geom</u>.*; public class <u>Demo </u>extends Frame { public Demo(){ super("Java AWT Examples"); prepareWindow(); } public static void main(String[] args){ Demo awtGraphicsDemo = new Demo(); awtGraphicsDemo.setVisible(true); } private void prepareWindow(){ setSize(450,400); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent){ System.exit(0); } }); } @Override public void paint(Graphics g) { g.setColor(Color.GRAY); Font currentFont = new Font("Berlin Sans FB Demi", Font.ITALIC, 24); g.setFont(currentFont); g.drawString("Welcome to Graphics Class", 100, 150); g.setColor(Color.magenta); g.drawRoundRect(150, 400, 100, 150, 20, 20); g.setColor(Color.CYAN); g.fillRoundRect(400, 400, 150, 120, 20,10); Font newFont1 = new Font("ALGERIAN", Font.ITALIC, 30); g.setFont(newFont1); g.setColor(Color.orange); g.fill3DRect(600, 400, 500, 120, false); g.setColor(Color.blue); g.drawString("Welcome to Graphics Class", 1000, 700); g.drawOval(600,200,400,100); g.drawLine(100, 170, 500, 170); } }
Output:
Graphics class provides all the basic operations required to create the visualizing objects on the screen and all information related to its state or font properties and modifying them. However, since it’s an abstract class thus, its instance cannot be created directly, thus called using its subclasses.
以上是Java 中的圖形類的詳細內容。更多資訊請關注PHP中文網其他相關文章!