它是 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中文网其他相关文章!