Results 1 to 2 of 2
Thread: Draw on JPanel, Help
- 07-31-2007, 04:29 AM #1
Member
- Join Date
- Jul 2007
- Posts
- 35
- Rep Power
- 0
Draw on JPanel, Help
Hi, I'm trying to draw using Graphics2D on a Java JPanel. Actually its a ImagePanel - a class I've extended from a JPanel, which just loads a jpeg into the panel. This part works fine....but then I want to draw on top of the JPanel, and its not showing what I'm drawing. Here is the code for the paint in the jpanel
Its definitely getting into the if(text) clause, and I've tried writing directly to the g Graphics, but that doesn't work either. "image" is just a BufferedImage storing the JPEG and whatever else I want to have drawn.Java Code:protected void paintComponent(Graphics g) { System.out.println("in paintstuff"); super.paintComponent(g); if(text) { System.out.println("Here"); Graphics2D g2 = (Graphics2D)image.getGraphics(); g2.drawString(textString, lastX, lastY); text = false; } if (image == null) return; switch (style) { case TILED: drawTiled(g); break; case SCALED: Dimension d = getSize(); g.drawImage(image, 0, 0, d.width, d.height, null); break; case ACTUAL: drawActual(g); break; } }
Thanks.
- 07-31-2007, 06:56 AM #2
Here you are drawing on the image
To draw on the JPanel, use the panels graphic context,Java Code:Graphics2D g2 = (Graphics2D)image.getGraphics(); g2.drawString(textString, lastX, lastY);
not the graphic context of the image as you did above.
Java Code:protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.drawString(textString, lastX, lastY);
Similar Threads
-
Trying to use Graphics draw methods
By Lang in forum SWT / JFaceReplies: 5Last Post: 03-26-2008, 05:49 AM -
Draw an arrow
By Albert in forum SWT / JFaceReplies: 3Last Post: 02-01-2008, 08:11 AM -
help me draw... please...
By kureikougaiji in forum New To JavaReplies: 1Last Post: 01-28-2008, 12:22 PM -
how to draw in Java
By Heather in forum AWT / SwingReplies: 2Last Post: 07-12-2007, 11:01 AM -
How to draw a thick line
By johnt in forum Java 2DReplies: 1Last Post: 05-31-2007, 04:27 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks