How to create a Graphics2D instance?
Asked Answered
E

3

11

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

Empiric answered 13/5, 2013 at 23:18 Comment(0)
N
19

You could use a BufferedImage:

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics2D = image.createGraphics();
Nebuchadnezzar answered 13/5, 2013 at 23:27 Comment(5)
i like the answer - how to display the image?Empiric
In an ImageIcon displayed by a JLabel. 1+Heartbroken
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!Empiric
@randomstring, Cool! This is my first accepted answer on stackoverflow (I just joined recently). Thanks :)Nebuchadnezzar
@Nebuchadnezzar i know - you won out over some tough competition too! thanks for helping out :)Empiric
M
6

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;
   ...
}
Misgiving answered 13/5, 2013 at 23:23 Comment(3)
tried @greedybuddha's similar and slightly more complete solution below but my program exits immediately after creating it.Empiric
@randomstring: then you're doing something wrong. What? -- we have no idea since you don't show code. 1+Heartbroken
thanks, i needed to add it in a JFrame. i should have been clearer in my question how much of a newbie i am :)Empiric
T
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
    }
}
Tracheid answered 13/5, 2013 at 23:26 Comment(9)
my program simply terminates after creating the jpanelEmpiric
@randomstring: Are you putting it in a JFrame and displaying the JFrame? Have you gone through any Swing tutorials at all?Heartbroken
nope, newbie. but i figured it out to add it in a JFrame. thanks!Empiric
@randomstring: Google the Swing tutorials and go through. Don't guess at this stuff as it never works.Heartbroken
@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 linksEmpiric
@randomstring: so, you're better off throwing code at the wall and seeing what sticks, eh? Much luck with that.Heartbroken
@HovercraftFullOfEels absent a java se 7 specific tutorial there is the API documentation and a couple of whats-new tutorials from Oracle.Empiric
@Tracheid 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.Empiric
No need to explain randomstring! If the answer works best for you then that's the answer you should pick :)Tracheid

© 2022 - 2024 — McMap. All rights reserved.