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
20
votes
6 answers

Triangle Draw Method

I have trouble drawing a triangle with the draw(Graphics g) method in Java. I can draw a rectangle like so: public void draw(Graphics g) { g.setColor(colorFill); g.fillRect(p.x, p.y, width, height); g.setColor(colorBorder); …
Jon Snow
  • 703
  • 3
  • 7
  • 10
16
votes
1 answer

Can you increase line thickness when using Java Graphics for an applet? I don't believe that BasicStroke works

I am having trouble adjusting line thickness. Can I do that in Graphics or do i have to do it in Graphics2D? If so, how do I alter the program to make it run? Thanks! import java.applet.Applet; import java.awt.*; public class myAppletNumberOne…
user2465406
  • 169
  • 1
  • 1
  • 7
15
votes
1 answer

Passing current Date

Seems like as not possible to fix Graphics2D lack in code, there I must to set fix size for animations, otherwise some Start outside Rectangle 490 x 490 freeze or shaking on the screen my SSCCE import java.awt.*; import…
mKorbel
  • 109,525
  • 20
  • 134
  • 319
15
votes
2 answers

Why does Firefox treat Helvetica differently from Chrome?

The vertical position of text rendered in Helvetica and the size of its content area differ between Firefox and Chrome for Mac. For example, in Chrome, the descenders are clipped if the line-height is identical to font-size. (I’ve adjusted the…
Adam
  • 696
  • 6
  • 16
15
votes
4 answers

Drawing Filled Rectangle over a BufferedImage

So I am attempting to create an application that can black-out sections of a survey that contains sensitive information. However I've run into a bit of a problem. What I want to do is draw filled black rectangles over a BufferedImage given x, y,…
John Fox
  • 171
  • 1
  • 1
  • 6
14
votes
2 answers

how to save panel as image in swing?

Hi i want to convert panel which contains components like label and buttons to image file. I have done the following code. The image was saved. but the content of the panel not visible or saved. Can anyone tell me how to save the panel with its…
Babu R
  • 1,025
  • 8
  • 20
  • 40
13
votes
2 answers

How does one print total number of pages in a JTextPane footer?

I have searched high and low for this answer and have come up blank. I have a requirement to print the contents of a JTextPane with a footer that says "Page of pages". It appears to impossible to do this simple function in Java. I can set…
Banjo
  • 161
  • 4
13
votes
7 answers

Swing HTML drawString

I'm trying to create some special component for a specific purpose, on that component I need to draw a HTML string, here's a sample code: public class MyComponent extends JComponent{ public MyComponent(){ super(); } …
George Casttrey
  • 427
  • 1
  • 6
  • 16
13
votes
2 answers

Why is java.awt.Graphics.drawLine exceptionally slow?

I am trying to achieve the following 'grid' layout. The class is extending java.awt.Canvas, and drawing these shapes (or lines) in the paint function. Why Canvas? Check here, trying to do something similar inititally. Updated MCVE Code for getting…
almightyGOSU
  • 3,731
  • 6
  • 31
  • 41
13
votes
1 answer

Where's Polygon.Double in Java?

Once again I'm doing Java graphics (Graphics2D) but I noticed there is no Polygon.Double or Polygon.Float classes whereas there is Rectangle2D.Float and Rectangle2D.Double class. Does anyone know why this is? I just need to draw a triangle using…
openidsucks
  • 245
  • 1
  • 4
  • 9
13
votes
1 answer

Understand BufferStrategy

I'm kind of new to java. I want to make a game. After a lot of research, I can't understand how bufferstrategy works.. I know the basics.. it creates an off-screen image that you can later put into your windows object.. I got this public class Marco…
Charlie Villy
  • 151
  • 1
  • 1
  • 7
12
votes
2 answers

Java: Graphics or Graphics2D?

I am new to Java and have been trying to make some simple games in it with lots of images on screen. Since then I have been using the 'Graphics' class to draw these images, strings and shapes, but I recently came across Graphics2D which seems to be…
Humphrey
  • 579
  • 3
  • 7
  • 17
12
votes
2 answers

How to handle huge data/images in RAM in Java?

Summary I am reading a large binary file which contains image data. Cumulative Count Cut analysis is performed on data [It requires another array with same size as the image]. The data is stretched between 0 to 255 stored in BufferedImage pixel by…
Harshita
  • 133
  • 10
12
votes
6 answers

Getting string size in java (without having a Graphics object available)

I'm trying to write application which need to draw many strings using Graphics2D class in Java. I need to get sizes of each String object (to calculate exact position of each string). There are so many strings that it should be done before the…
Lukasz Spas
  • 595
  • 2
  • 9
  • 23
12
votes
1 answer

Java2D: interaction between XWindows events and frame rate

I'm experiencing an unexpected interaction between system events and the window refresh rate in simple Java2D applications on Linux/XWindows. It is best demonstrated with the small example below. This program creates a small window in which a…
Thomas
  • 17,016
  • 4
  • 46
  • 70