How to create a Graphics2D instance? - java

What's the easiest way in Java SE 7 to obtain an instance just to plot a few points for debugging? Desktop environment.

You could use a BufferedImage:
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics2D = image.createGraphics();

The easiest and safest way is to use to cast the Graphics reference in paintComponent and cast it as needed. That way the Object is correctly initialized. This reference can be passed to other custom painting methods as required.
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
...
}

You should probably just create a JPanel and paint on it.
public class MyPanel extends JPanel {
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
.... // my painting
}
}

Related

Java, not applicable for the arguments,affine transform?

public void paint(Graphics g) {
Rectangle rectangle = new Rectangle(100,100,100,100);
Graphics2D g2d = (Graphics2D) g;
AffineTransform transform = new AffineTransform();
transform.rotate(
Math.toRadians(45), rectangle.getX() + rectangle.width/2,
rectangle.getY() + rectangle.height/2
);
g2d.draw(transform);
}
I am trying to rotate a rectangle around a center, but its not working.
I am getting this error:
The method draw(Shape) in the type Graphics2D is not applicable for the arguments (AffineTransform)
The error indicates that you cannot call this method with transform.
You should try to call setTransform first and then draw the rectangle.
public void paint(Graphics g) {
Rectangle rectangle = new Rectangle(100,100,100,100);
Graphics2D g2d = (Graphics2D) g;
AffineTransform transform = new AffineTransform();
transform.rotate(
Math.toRadians(45), rectangle.getX() + rectangle.width/2,
rectangle.getY() + rectangle.height/2
);
g2d.setTransform(transform);
g2d.draw(rectangle);
}

Reacquiring previous Graphics2D instance of a BufferedImage

I create a BufferedImage in a parent class method and use Graphics2D to set options (Paint, RenderingHints, etc.). Another class inherits this base class and overrides the method but calls the superclass method to acquire the BufferedImage instance.
How do I keep the Graphics2D settings from the superclass method?
class Parent {
BufferedImage draw() {
BufferedImage image = new BufferedImage(...)
Graphics2D g2 = (Graphics2D) image.getGraphics()
g2.setColor(new Color(255, 0, 0))
return image;
}
}
class Child extends Parent {
#Override
BufferedImage draw() {
BufferedImage image = super.draw()
Graphics2D g2 = (Graphics2D) image.getGraphics()
System.out.println(g2.getColor()) // 255, 255, 255
return image;
}
}

Graphics2D is returning "NULL" always

graphics2D is returning "NULL" always in below code. Due to that putPixel() method is not being called. I am calling PictureBox from form design.
public class PictureBox extends JPanel {
Graphics2D graphics2D;
static BufferedImage image;
int imageSize = 300;
public PictureBox(){
setDoubleBuffered(false);
this.setBorder(UIManager.getBorder("ComboBox.border"));
this.repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
if(image == null){
image = new BufferedImage(imageSize, imageSize, BufferedImage.TYPE_INT_RGB);
graphics2D = (Graphics2D)image.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
clear();
}
Graphics2D g2D = (Graphics2D) g;
g2D.drawImage(image, 0, 0, this);
repaint();
}
public final void putPixel(int x, int y, Color color) {
if(graphics2D != null){
graphics2D.setColor(color);
graphics2D.drawLine(x, y, x, y);
repaint();
}
}
public void clear() {
graphics2D.setPaint(Color.WHITE);
graphics2D.fillRect(0, 0, imageSize,imageSize);
repaint();
}
}
putPixel method is being called from main where i have (x,y) coordinate stored in Point2D array.
Since you have called putPixel from outside the class and you have not initialised the graphics2D and image in the constructor it may be that when you all calling putPixel method the class may not have been displayed. So you are getting graphics2D to be null as it initialises only when the paintComponent is called and it is called when this class gets displayed.
The solution could be that you shift the initialisation code for the image and graphics2D to the constructor so that you do not encounter null while calling putPixel.
NOTE
You have indiscriminately called the method repaint(). You should keep in mind that repaint() calls paint() method which in turn calls paintComponent() method. So if you call repaint() inside paintComponent() method, you run into the risk of creating an infinite loop. Here you have called it twice once in paintComponent and again in clear method which is called by paintComponent.

Use graphics2d object outside paintComponent method?

My code:
public class YesOrNo{
Graphics g;
public void createAndShowGui() {
JPanel x=new JPanel(){
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
}
}
}
public void drawARectangle(){
g.drawRect(10, 10, 50, 50);
}
}
I know I can call a method inside paintComponent and pass g to it as parameter, but can you do it without calling that method inside paintComponent?

Transparency in Java

I've been using the new com.sun.awt.AWTUtilities class and am intrigued. I got com.sun.awt.AWTUtilities/setWindowOpacity(java.awt.Window window, float f) to work perfectly, but am now wondering if there is any way to change the opacity of an individual component, such as a javax.swing.JInternalFrame or javax.swing.JButton.
Try this:
class TransparentButton extends JButton {
public TransparentButton(String text) {
super(text);
setOpaque(false);
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
super.paint(g2);
g2.dispose();
}
}

Categories