What is the use of Graphics class?
Hello friends,
I just started to study advanced java programming language. Basically I have knowledge about the C++ programming, but it's totally different from advanced java programming. In which the I am not able getting the use of Graphics class of java. But have to use this class in my java project. So please something about the this Graphics class of java. I would greatly appreciate your any help over this.
Re: What is the use of Graphics class?
The Graphics class is the abstract base class for all graphics contexts that allow an application to draw onto components that are realized on various devices, as well as onto off-screen images. A Graphics object encapsulates state information needed for the basic rendering operations that Java supports. This state information includes the various properties. Coordinates are infinitely thin and lie between the pixels of the output device.
Re: What is the use of Graphics class?
Hi friend,
Following example of Graphics class will depict you the use of Graphics class of java:
Code:
import java.awt.Graphics;
import javax.swing.JFrame;
public class GraphicsDemo1 extends JFrame
{
public static void main(String[] abt)
{
MainClass fB = new MainClass();
fB.setSize(300,300);
fB.setVisible(true);
}
public void paint(Graphics gB)
{
gB.drawRect(10, 10, 60, 50);
gB.fillRect(100, 10, 60, 50);
gB.drawRoundRect(190, 10, 60, 50, 15, 15);
gB.fillRoundRect(70, 90, 140, 100, 30, 40);
}
}
Re: What is the use of Graphics class?
If you want to use the properties of Graphics class, then need to be aware about the below it's methods:
- setClip(int xh, int yh, int width1, int height1)
- hitClip(int xh, int yh, int width1, int height1)
- getFontMetrics(Font fnt)
- getClipBounds(Rectangle rct)
- fillRoundRect(int xh, int yh, int width1, int height1, int arcWidth1, int arcHeight1)
Re: What is the use of Graphics class?
Hi,
In below java code I have used the methods of Graphics class along with different parameters:
Code:
import java.awt.Graphics;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JFrame;
public class GraphicsDM extends JFrame {
Shape shps[] = new Shape[5];
public static void main(String aprgs[]) {
Draw2DObjects app = new Draw2DObjects();
}
public Draw2DObjects() {
add("Center", new MyCanvas());
shps[0] = new Line2D.Double(0.0, 0.0, 100.0, 100.0);
shps[1] = new Rectangle2D.Double(10.0, 100.0, 200.0, 200.0);
shps[2] = new Ellipse2D.Double(20.0, 200.0, 100.0, 100.0);
GeneralPath path = new GeneralPath(new Line2D.Double(300.0, 100.0, 400.0, 150.0));
path.append(new Line2D.Double(25.0, 175.0, 300.0, 100.0), true);
shps[3] = path;
shps[4] = new RoundRectangle2D.Double(350.0, 250, 200.0, 100.0, 50.0, 25.0);
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
class MyCanvas extends Canvas {
public void paint(Graphics graphics) {
Graphics2D g1 = (Graphics2D) graphics;
for (int i1 = 0; i1 < shps.length; ++i1) {
if (shps[i1] != null)
g1.draw(shps[i]);
}
} }
}
Re: What is the use of Graphics class?
The graphics class is used for the encapsulation of 'Graphics object'. Following are some properties which are associated with this object:
- current text font
- translation origin used for clipping as well as rendering the coordinates.
- current XOR alternation color
- Component object
- current color