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
0
votes
1 answer

How to draw portions of circles based on percentages in Graphics2D?

I am working on a circular health bar for a top-down 2d shooter and would like some opinions of the best way to do this in only graphics2D. Wondering if drawing many arcs or just using images would be best.
piechesse
  • 39
  • 1
  • 7
0
votes
0 answers

Java for loop and graphics issue

My code: import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Polygon; import java.awt.RenderingHints; import java.util.ArrayList; import…
user2649681
  • 750
  • 1
  • 6
  • 23
0
votes
0 answers

How to make a Swing main menu in a java 2d game?

I have been working on a simple 2D Java top down shooter for a very long time. Until now I have had a graphics2D main menu that is just rendered until the user presses the enter key. I would like to have a working menu system where three clickable…
piechesse
  • 39
  • 1
  • 7
0
votes
3 answers

super.paint() not visible over image

I'm building a JFrame with an image as a background. I'm overriding the paint() method to draw that image in the JFrame, but when I start the application in Eclipse, none of the JComponents I added are visible. Here's my SSCCE: public class foo…
celloplayer
  • 31
  • 1
  • 8
0
votes
1 answer

AWT - rotate the entire Swing panel

I'm experiencing a problem with AWT in rotating the graphics in a panel by 90 degrees. I can rotate the graphics in a panel by casting to Graphics2D and applying a transform. The problem with this is that if the panel area is rectangular then part…
0
votes
0 answers

Java Line2D not drawing

i am trying to draw lines according to a list. here is my code. i added an "if" to see if line is draw for(int m = 0; m < routecounter; m++){ if(route[m] != null){ if(route[m].routeType == 2){ …
gesirdekatwork
  • 87
  • 2
  • 11
0
votes
0 answers

Java: difference between drawing over brand new image vs over loaded image

There was a PHP application that's purpose was to print text over a number of pre-defined PNG images. It worked without issues and printed antialiased text in any given font and color. What are the special considerations when printing over the…
ajeh
  • 2,652
  • 2
  • 34
  • 65
0
votes
2 answers

Rotating several images - Java

I am creating a flights radar simulator in Java, for a class project. So far, I've been able to show many small airplane images moving across the radar with a given direction and different speed. My problem is how can i rotate each airplane image to…
sandiego
  • 151
  • 1
  • 4
  • 10
0
votes
4 answers

Java - Open JScrollPane with JScrollBar set to the right

I'm looking to try and load up a JScrollPane with the horizontal JScrollBar set all the way to the right, i.e. like in this image: https://i.stack.imgur.com/02BKi.png However, no matter what I do, I end up with this…
Keshava Murthy
  • 119
  • 1
  • 2
  • 12
0
votes
3 answers

How to find the dimensions of a string that you are drawing with graphics2d?

I am trying to paint a caret at the end of a string but I do not know how to retrieve the dimensions of the given string I am painting with graphics2d.drawString. Or if there is a "shape" that is actually a string with dimensions, that would help…
Manny P
  • 33
  • 6
0
votes
1 answer

Java: Null Pointer exception while setting a Text.Attribute Object to it's default value, why?

So I am working with different TextAttribute objects and some of their default values are null, like FOREGROUND (In the TextAttribute part of the API it says their different keys, values, Pricipal Constants and Default Values). In this code I use…
Manny P
  • 33
  • 6
0
votes
1 answer

Rotate shapes using Graphics2D

When I add the function g2d.rotate(some Number) the screen shows no shape at all, and without this function everything is working. What is the problem? public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d =…
0
votes
1 answer

Why does this print out twice?

I've noticed that when i use paintComponent in java if I use System.out.println(); things will print out 2, 3, and sometimes 4 times. I know that when you use extends JPanel it will automatically be called, but why more then once. Here is some code…
ndfkj asdkfh
  • 316
  • 1
  • 5
  • 15
0
votes
1 answer

How to see if a something in Graphics2D is off the frame

I have a paint method that I have draw a rectangle and move it across the screen and what I want it to do I when the rectangle goes off the screen to move it back to the start of the JFrame. I assume it would be something like…
ndfkj asdkfh
  • 316
  • 1
  • 5
  • 15
0
votes
1 answer

Graphics2D.drawString very slow

I'm using Java Graphics2D to draw some lines and labels into a JPanel. The drawing code is running extremely slowly (drawString() seems the worst, but I no longer think it is the culprit, just the busiest call). The execution environment is OSX…
Clyde
  • 7,389
  • 5
  • 31
  • 57