Questions tagged [graphics2d]

Graphics2D is the part of the Java 2D API related to two-dimensional graphics, text, and imaging capabilities. Unlike the older Graphics class, Graphics2D supports coordinate transforms. It also gives better control over geometry, colors and text layout.

The java.awt.Graphics2D Java class is part of the Abstract Windowing Toolkit () library and was first included in Java Standard Edition version 1.4. It supersedes the java.awt.Graphics class from earlier Java versions. Graphics and Graphics2D are subsequently used by .

Unlike Graphics, Graphics2D supports coordinate transforms, uses floats rather than ints for better control over geometry, also has improved color management and text layout control. Many system library methods that return, expect or receive a Graphics object are actually working with Graphics2D object that can be casted:

@Override
protected void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    ....
}

This allows some restricted Java implementations (like early versions of GNU Classpath) to opt not to implement Graphics2D and still formally support the complete API.

Graphics2D is used to display shapes, text and images in user interfaces components and includes methods for settings the style and transformations of the graphics output, and for controlling the rendering of the graphics.

1667 questions
54
votes
3 answers

Problems with newline in Graphics2D.drawString

g2 is an instance of the class Graphics2D. I'd like to be able to draw multi-line text, but that requires a newline character. The following code renders in one line. String newline = System.getProperty("line.separator"); g2.drawString("part1\r\n" +…
pkinsky
  • 1,718
  • 2
  • 23
  • 28
46
votes
1 answer

Using Graphics2D to overlay text on a BufferedImage and return a BufferedImage

I have checked similarly named questions, but they don't answer this use case. Basically, I was to overlay some text (text) at a given coordinate (x,y) I have the below function in a package; protected BufferedImage Process2(BufferedImage image){ …
Bolster
  • 7,460
  • 13
  • 61
  • 96
43
votes
4 answers

How can I center Graphics.drawString() in Java?

I'm currently working on the menu system for my Java game, and I wonder how I can center the text from Graphics.drawString(), so that if I want to draw a text whose center point is at X: 50 and Y: 50, and the text is 30 pixels wide and 10 pixels…
Daniel Kvist
  • 3,032
  • 5
  • 26
  • 51
29
votes
6 answers

Drawing a simple line graph in Java

In my program I want to draw a simple score line graph. I have a text file and on each line is an integer score, which I read in and want to pass as argument to my graph class. I'm having some trouble implementing the graph class and all the…
user1058210
  • 1,639
  • 7
  • 29
  • 49
28
votes
5 answers

Flip Image with Graphics2D

I've been trying to figure out how to flip an image for a while, but haven't figured out yet. I'm using Graphics2D to draw an Image with g2d.drawImage(image, x, y, null) I just need a way to flip the image on the horizontal or vertical axis. If…
Fuze
  • 423
  • 1
  • 6
  • 11
28
votes
4 answers

plotting a curve around a set of points

I have a set of points on a plane. They are partitioned into subsets. I want to plot a closed curve around points that belong to the same subset, so that points that belong to a subset will be inside the curve, and those that aren't will be outside.…
amit
  • 3,332
  • 6
  • 24
  • 32
27
votes
5 answers

Set BufferedImage to be a color in Java

I need to create a rectangular BufferedImage with a specified background color, draw some pattern on the background and save it to file. I don't know how to create the background. I am using a nested loop: BufferedImage b_img = ... for every row for…
Lily
  • 5,872
  • 19
  • 56
  • 75
25
votes
4 answers

How to rotate text with Graphics2D in Java?

I want to rotate text on a JPanel using Graphics2D.. My code is this: double paso=d.width/numeroBarras; double alto=datos[i].valor; Font fBarras=new Font("Serif", Font.PLAIN, 15); g2.setFont(fBarras); Rectangle2D…
Rafael Carrillo
  • 2,772
  • 9
  • 43
  • 64
23
votes
8 answers

Java: Rotating Images

I need to be able to rotate images individually(in java). The only thing I have found so far is g2d.drawImage(image, affinetransform, ImageObserver ). Unfortunately, I need to draw the image at a specific point, and there is no method with an…
Jimmt
  • 852
  • 2
  • 11
  • 29
23
votes
7 answers

Java Graphics2D transparent background

I have a Graphics2D object and I want to set up the background of the object. It has a setBackground method, which has a Color parameter. This way I can set the color of the background. My question is: how can I set the transparency of the…
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
22
votes
1 answer

Drawing Transparent Images In Java Graphics2D

I want to draw a PARTIALLY transparent image on top of another (Making shadows over things). I am currently using java's Graphics2D class to render, I've been told to set the composite to AlphaComposite, but that only sets it completely…
blazingkin
  • 552
  • 1
  • 4
  • 17
21
votes
4 answers

Rotating BufferedImage instances

I am having trouble getting a rotated BufferedImage to display. I think the rotation is working just fine, but I can't actually draw it to the screen. My code: Class extends JPanel { BufferedImage img; int rotation = 0; public void…
user577304
20
votes
4 answers

Rotate a Java Graphics2D Rectangle?

I have searched everywhere and I just cant find the answer. How do I rotate a Rectangle in java? Here is some of my code: package net.chrypthic.Space; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Space extends…
chrypthic
  • 579
  • 1
  • 6
  • 15
20
votes
5 answers

Convert a Graphics2D to an Image or BufferedImage

I have a little problem here. I have an applet, where user can "draw" inside it. To do that, I use the java.awt.Graphics2D. But, how can I do to save the user draw image as a JPEG image, or at least, convert it to a BufferedImage or something? I…
caarlos0
  • 20,020
  • 27
  • 85
  • 160
20
votes
2 answers

Java 8 graphics glitch when stroking sub-pixel coordinates on Linux

It seems that stroking on sub-pixel coordinates became broken in Java 8. I have three sets of cases, shown on screenshots (columns represent cases, rows represent different stroke widths) : Java 7u51 (400% scale) Java 8u60 (400% scale) Fill and…
1
2 3
99 100