Re: Graphics2D class of java
This Graphics2D class extends the Graphics class to provide more sophisticated control over geometry, coordinate transformations, color management, and text layout. This is the fundamental class for rendering 2-dimensional shapes, text and images on the Java platform. All coordinates passed to a Graphics2D object are specified in a device-independent coordinate system called User Space, which is used by applications.The Graphics2D object contains an AffineTransform object as part of its rendering state that defines how to convert coordinates from user space to device-dependent coordinates in Device Space.
Re: Graphics2D class of java
Hi,
Please refer following example of Graphics2D class of java programming:
Code:
import java.awt.Graphics2D;
import javax.swing.JFrame;
public class Graphics2DSample
{
public static void main(String[] afrgs) {
JFrame jf01 = new JFrame("Demo");
Container cp1 = jf01.getContentPane();
MyCanvas tl = new MyCanvas();
cp1.add(tl);
jf01.setSize(300, 200);
jf01.setVisible(true);
}}
class MyCanvas extends JComponent {
Shape shape;
public MyCanvas() {
shape1 = create();
}
protected Shape create() {
GeneralPath path = new GeneralPath();
path1.moveTo(60, 30);
path1.lineTo(50, 200);
return path;
}
public void paint(Graphics g1) {
Graphics2D g02 = (Graphics2D) g1;
g02.setPaint(Color.black);
g02.draw(shape);
}
}
Re: Graphics2D class of java
Hello friend,
If you want to know about the methods of Graphics2D class, then review following:
- translate(int xn, int yn)
- transform(AffineTransform Tx)
- setTransform(AffineTransform Txm)
- setRenderingHint(RenderingHints.Key hintKey, Object hintVal)
- setComposite(Composite comp)
- setBackground(Color clr)
Re: Graphics2D class of java
Example of Graphics2D class:
Code:
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class GPMainClass extends JPanel {
public void paint(Graphics gV) {
g.fillRect(0, 0, 30, 30);
Graphics2D gp2 = (Graphics2D) g;
gp2.translate(50, 50);
gp2.rotate(30.0 * Math.PI / 180.0);
gp2.scale(2.0, 2.0);
gV.setColor(Color.red);
gV.fillRect(0, 0, 30, 30);
}
public static void main(String[] arcgs) {
JFrame ZLP = new JFrame();
ZLP.getContentPane().add(new MainClass());
ZLP.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ZLP.setSize(200, 200);
ZLP.setVisible(true);
}
}
Re: Graphics2D class of java
Hi friend,
Below I have given some constructors of Graphics2D class, review it carefully:
- getDeviceConfiguration
- drawRenderedImage
- getFontRenderContext
- drawGlyphVector
- drawRenderableImage