Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-31-2007, 05:29 AM
Member
 
Join Date: Jul 2007
Posts: 35
carl is on a distinguished road
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

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

Thanks.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-31-2007, 07:56 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
Here you are drawing on the image
Code:
Graphics2D g2 = (Graphics2D)image.getGraphics(); g2.drawString(textString, lastX, lastY);
To draw on the JPanel, use the panels graphic context,
not the graphic context of the image as you did above.
Code:
protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.drawString(textString, lastX, lastY);
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Trying to use Graphics draw methods Lang SWT / JFace 5 03-26-2008 06:49 AM
Draw an arrow Albert SWT / JFace 3 02-01-2008 09:11 AM
help me draw... please... kureikougaiji New To Java 1 01-28-2008 01:22 PM
how to draw in Java Heather AWT / Swing 2 07-12-2007 12:01 PM
How to draw a thick line johnt Java 2D 1 05-31-2007 05:27 PM


All times are GMT +3. The time now is 10:19 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org