How to create a Graphics2D instance?

19,428

Solution 1

You could use a BufferedImage:

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics2D = image.createGraphics();

Solution 2

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;
   ...
}

Solution 3

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
    }
}
Share:
19,428
necromancer
Author by

necromancer

Updated on June 19, 2022

Comments

  • necromancer
    necromancer almost 2 years

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

  • necromancer
    necromancer almost 11 years
    my program simply terminates after creating the jpanel
  • necromancer
    necromancer almost 11 years
    i like the answer - how to display the image?
  • necromancer
    necromancer almost 11 years
    tried @greedybuddha's similar and slightly more complete solution below but my program exits immediately after creating it.
  • Hovercraft Full Of Eels
    Hovercraft Full Of Eels almost 11 years
    In an ImageIcon displayed by a JLabel. 1+
  • Hovercraft Full Of Eels
    Hovercraft Full Of Eels almost 11 years
    @randomstring: then you're doing something wrong. What? -- we have no idea since you don't show code. 1+
  • Hovercraft Full Of Eels
    Hovercraft Full Of Eels almost 11 years
    @randomstring: Are you putting it in a JFrame and displaying the JFrame? Have you gone through any Swing tutorials at all?
  • necromancer
    necromancer almost 11 years
    nope, newbie. but i figured it out to add it in a JFrame. thanks!
  • Hovercraft Full Of Eels
    Hovercraft Full Of Eels almost 11 years
    @randomstring: Google the Swing tutorials and go through. Don't guess at this stuff as it never works.
  • necromancer
    necromancer almost 11 years
    thanks, i needed to add it in a JFrame. i should have been clearer in my question how much of a newbie i am :)
  • necromancer
    necromancer almost 11 years
    @HovercraftFullOfEels one reason i am hesitant to go through 90's tutorials is because i am starting on Java SE 7 and i want to know the most modern way of creating a point. i realize there is backward compatibility so the old casty ways will work but if there is a modern alternative in java se 7 i am all ears for any tutorial links
  • Hovercraft Full Of Eels
    Hovercraft Full Of Eels almost 11 years
    @randomstring: so, you're better off throwing code at the wall and seeing what sticks, eh? Much luck with that.
  • necromancer
    necromancer almost 11 years
    @HovercraftFullOfEels absent a java se 7 specific tutorial there is the API documentation and a couple of whats-new tutorials from Oracle.
  • necromancer
    necromancer almost 11 years
    difficult choice which answer to accept, i picked this one for its ultra-simplicity and ultra-portability. it will work on server side as well as on a desktop. the only flaw with this approach is that it is not ideal for the desktop because you cannot trap mouse events which you could use to debug. but that's not what i asked for. thanks for the great answer!
  • necromancer
    necromancer almost 11 years
    @greedybuddha thanks for the answer. it was difficult to pick between yours and the bufferedimage one. i chose the other one because it is truly the simplest and works server-side too and helps somebody avoid all the gotchas of swing. in my case i need to trap mouse events so i will end up using a JComponent similar to your answer but i felt it was fair to accept the other answer because i did not mention the need for mouse events in the question and instead focussed on simplest possible way.
  • greedybuddha
    greedybuddha almost 11 years
    No need to explain randomstring! If the answer works best for you then that's the answer you should pick :)
  • BLuFeNiX
    BLuFeNiX almost 11 years
    @randomstring, Cool! This is my first accepted answer on stackoverflow (I just joined recently). Thanks :)
  • necromancer
    necromancer almost 11 years
    @BLuFeNiX i know - you won out over some tough competition too! thanks for helping out :)