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

Java: Why doesn't this JPanel paint properly?

I have a 2D array. I want each pixel to be represented by a total of four in the actual image. I've tried various piece of code but none seem to work and I don't really understand how it works either. So far I have: panel = new JPanel() { …
mark
  • 2,841
  • 4
  • 25
  • 23
0
votes
2 answers

Displaying/implementing a System.Graphics.DrawEllipse as a DirectX 3D Surface?! c#

I have developed currently an application that draws several ellipses using System.Graphics.DrawEllipse which works fine in c#. Now I want to integrate this in order to display certain ellipses to different eyes using stereo imagining (3D) by…
Bogdan Goie
  • 1,257
  • 1
  • 13
  • 21
0
votes
2 answers

How can I map pixel position i,j to a color?

I am trying to make it so that the position of a pixel in an image (int i, int j) determines the color of that pixel. This is for an explosion effect in a java2d game I want to be extra cool by making the colors of the explosion depend on the…
arynaq
  • 6,710
  • 9
  • 44
  • 74
0
votes
1 answer

Concurrency Issue with Grahpics2D Draw and JPanel repaint

So, I have a map, that I want to be able to draw rectangles on to highlight an area. When the mouse is released a permanent rectangle is drawn on the map that persists until the mouse is dragged again to start the creation of a new rectangle. …
NolanPower
  • 409
  • 3
  • 11
0
votes
2 answers

what happens to the value returned by 'createBufferStrategy'?

I'm learning about BufferStrategy and I'm kind of confused with the creation of BS. my code looks like this... public class Game extends Canvas{ /*code not relevant to this topic.. */ public void…
Space Ghost
  • 765
  • 2
  • 13
  • 26
0
votes
0 answers

Splash Screen Painting Slow in Java

I am a Beginner in Java GUI, I am trying to show the user a splashScreen which will show him/her the Progress done, I tried doing it with Graphics2D, but the problem is that the class paints the screen very slowly, I did it using a for loop for…
MMujtabaRoohani
  • 483
  • 4
  • 19
0
votes
1 answer

A lot of lag when using g2d.drawImage - Java

I'm using Graphics2D to draw a map on my game. In the loop it calls this: for(int i = 0; i < tiles.length; i++){ for(int j = 0; j < tiles[i].length; j++){ if(tiles[i][j]==1){ //GRASS …
Frostsoft
  • 46
  • 8
0
votes
1 answer

Images do not appear correctly on MouseEvent in swing

I have a 3x3 check board-like image rendered on a JPanel which is added onto a JFrame. Then I have 9 more JPanels (1 on top of each square) and on click something needs to be drawn on the corresponding square. My problem is that it only works for…
Anon855
  • 17
  • 4
0
votes
1 answer

Does Graphics2D reset the composititon the next time paintComponent() is called? And why?

Does Graphics2D reset the composition the every time paintComponent() is called? And why? For example, public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D)g; …
Mong H. Ng
  • 900
  • 10
  • 15
0
votes
2 answers

Java Paint Program

I have started a Java Paint program that seems to be working fine... There is just one problem. In my program I have it set up so that it repaint()'s ovals using MouseListener methods and overrides paintComponent(Graphics g). The problem is when I…
user1959349
0
votes
1 answer

How can I use BasicStroke Method while using Java Graphics for an Applet?

This is what I have done so far. import java.applet.Applet; import java.awt.*; public class myFirstAppletRun extends Applet {public void paint (Graphics page) { page.drawLine(100, 0, 100, 600); page.fillArc(7, 234, 115, 100, -20, 180); } } How…
user2465406
  • 169
  • 1
  • 1
  • 7
0
votes
1 answer

AffineTansform and Graphicsd2D drawing location

Code: AffineTransform at = new AffineTransform(); at.scale(2, 1); at.rotate(Math.toRadians(45)); at = new AffineTransform(at); at.translate(-img.getWidth()/2, -img.getHeight()/2); g2D.setTransform(at); g2D.drawImage(img, 0, 0,…
gamesaucer
  • 113
  • 1
  • 10
0
votes
1 answer

Multithreaded rendering in swing

I am just toying around with the idea of creating a multithreaded renderer in swing for my java2d games where each thread is responsible for rendering its own swingcomponent and came up with a simple program to try and and achieve this. *I am aware…
arynaq
  • 6,710
  • 9
  • 44
  • 74
0
votes
2 answers

Coloring parts of an image

I have a png image of a floor plan and I want to overlay certain regions of it with different transparencies of the color red. Some Complexities: The regions do not follow the lines on the floor plan strictly. Some regions cover 2-3 rooms, some…
nknj
  • 2,436
  • 5
  • 31
  • 45
0
votes
1 answer

Create function which checks if I pressed inside of element of Graphics2D and link this with ActionListener

I would like ask what is the best way of creating such a function in java? I have my JPanel and this picture below as ImageIcon with Rectangle2D objects (black frames). Now I just want to simply check if I pressed inside of one of frames and use…
tmq
  • 51
  • 1
  • 1
  • 7
1 2 3
99
100