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 10-10-2007, 10:39 PM
Member
 
Join Date: Jul 2007
Posts: 10
Riftwalker is on a distinguished road
JPanel Problems
I have a JFrame that has 2 JPanels contained in it. Both JPanels have graphics drawn on them. The coding for both JPanels to display the graphics is almost the same, they just have to display different information.

The first JPanel I have is fine and displays fine when it draws graphics outside of the viewport allowed by the JScrollpane it's in.

The second JPanel however has problems. The graphics won't draw outside of the area of the viewport and when I scroll, what graphics are there disappear. This panel is programmed the exact same way as the first one, but it has problems while the first one doesn't.

Does anyone know what would be causing this problem?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-10-2007, 10:52 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
Make sure that all drawing takes place (ie, the code is) inside or called from the paintComponent method in each JPanel. Using getGraphics inside event code is one thing that can cause these kind of problems.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-10-2007, 11:07 PM
Member
 
Join Date: Jul 2007
Posts: 10
Riftwalker is on a distinguished road
I'm pretty sure that this is the case, but I'll post the pertinent code for each.

JPanel 1 (the one that works)
Code:
public class displayPanel extends JPanel implements Scrollable { private mainWindow mainWin; private Graphics2D g2; private sequenceDisplayPanel sdPanel; private int contigNum = 0; private Contig[] contigArray; private interactiveRectangle[] rectArray; private displayPanel displaypanel; private Rectangle backbone; private String contigName; public displayPanel(mainWindow mw, sequenceDisplayPanel sdP) { mainWin = mw; sdPanel = sdP; displaypanel = this; setPreferredSize(new Dimension(1000,500)); setBackground(Color.WHITE); displaypanelMListener clickListener = new displaypanelMListener(); addMouseListener(clickListener); } public void paintComponent(Graphics g) { super.paintComponent(g); g2 = (Graphics2D)g; try { g2.setColor(Color.BLACK); g2.drawString(contigName, 40, 35); g2.fill(backbone); for(int j = 40; j < contigArray[contigNum].getContigLength(); j += 100) { g2.drawString(String.valueOf(j-40), j , 50); g2.drawLine(j, 55, j, 80); } for(int i = 0; i < rectArray.length; i++) { Rectangle tempRect = rectArray[i]; g2.setColor(Color.BLUE); g2.fill(tempRect); } } catch(NullPointerException nullPointerException){} }
JPanel 2 (the one with problems)
Code:
public class sequenceDisplayPanel extends JPanel implements Scrollable { private JFrame mainWin; private Graphics2D g2; private interactiveRectangle[] rectArray; private sequenceDisplayPanel sdPanel; private int paintY = 20; private int rectNum = 0; public sequenceDisplayPanel(JFrame mW) { mainWin = mW; sdPanel = this; setPreferredSize(new Dimension(1000,100)); setBackground(Color.WHITE); } public void paintComponent(Graphics g) { super.paintComponent(g); g2 = (Graphics2D)g; try { g2.drawString(rectArray[rectNum].getContigName(), 20, paintY); paintY += 15; String tempString = rectArray[rectNum].getContigSeq(); int xDistance = 20; for(int i = 0; i < tempString.length(); i++) { if(i < (rectArray[rectNum].getStartOfUsedRegion() -1) || i > (rectArray[rectNum].getEndOfUsedRegion()-1)) g2.setColor(Color.BLACK); else g2.setColor(Color.RED); g2.drawString(String.valueOf(tempString.charAt(i)), xDistance, paintY); xDistance += 8; } g2.setColor(Color.BLACK); paintY +=20; String totalLength = ("The total length is: " + String.valueOf(rectArray[rectNum].getTotalLength())); g2.drawString(totalLength, 20, paintY); paintY += 20; } catch(NullPointerException nullPointerException){} }
Now this isn't the complete code for each class but it's the only drawing I have in each, etc.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 10-10-2007, 11:36 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
Can't tell much by what you posted. The call to setPreferredSize is enough for a
parent scrollPane to work properly. If the text height and/or width is variable you could
override the getPreferredSize method and return a calculated (in paintComponent as
you go) size as the prefSize. This should allow you to skip the Scrollable implementation.
The other suggestion is to remove the member variable "g2" and keep it local inside
paintComponent.
Post some code that shows the trouble if you like.
Code:
public class sequenceDisplayPanel extends JPanel implements Scrollable { private JFrame mainWin; // Eliminate this member variable. // private Graphics2D g2; ... public void paintComponent(Graphics g) { super.paintComponent(g); // Keep the graphics context reference local. Graphics2D g2 = (Graphics2D)g;
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 10-12-2007, 11:30 PM
Member
 
Join Date: Jul 2007
Posts: 10
Riftwalker is on a distinguished road
I have redone a few things and decided to change up the way the panel I was having problems with was displayed. Now it pops up in a new JFrame, but I am having problems here as well. The graphics aren't displaying quite correctly and when I resize it everything disappears. Here is the code:

Code:
import javax.swing.*; import java.awt.*; import java.awt.geom.*; import javax.swing.border.*; import java.awt.event.*; public class seqDisplayFrame extends JFrame { private interactiveRectangle iRect; private seqDisplayFrame sdFrame; public seqDisplayFrame(interactiveRectangle iR) { sdFrame = this; iRect = iR; sdFrame.setTitle(iRect.getContigName()); seqDisplayPanel sdPanel = new seqDisplayPanel(iRect); sdFrame.add(sdPanel); sdFrame.pack(); } private class seqDisplayPanel extends JPanel { private interactiveRectangle intRect; private int paintY = 20; public seqDisplayPanel(interactiveRectangle iR) { intRect = iR; setPreferredSize(new Dimension(500,500)); setBackground(Color.WHITE); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; String tempString = intRect.getContigSeq(); int xDistance = 20; for(int i = 0; i < tempString.length(); i++) { if(i < (intRect.getStartOfUsedRegion() -1) || i > (intRect.getEndOfUsedRegion()-1)) g2.setColor(Color.BLACK); else g2.setColor(Color.RED); g2.drawString(String.valueOf(tempString.charAt(i)), xDistance, paintY); xDistance += 8; if(xDistance > 480) { xDistance = 20; paintY += 15; } } g2.setColor(Color.BLACK); paintY +=20; String totalLength = ("The total length is: " + String.valueOf(intRect.getTotalLength())); g2.drawString(totalLength, 20, paintY); } } }
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 10-13-2007, 02:42 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
when I resize it everything disappears
That doesn't happen with this.
You may have to change the name of InteractiveRectangle below to avoid name–clashing.
Or you could run this in another folder (separate from your current class files).
Code:
import java.awt.*; import javax.swing.*; public class SDF extends JFrame { private InteractiveRectangle iRect; private SDF sdFrame; public SDF(InteractiveRectangle iR) { sdFrame = this; iRect = iR; sdFrame.setTitle(iRect.getContigName()); SDisplayPanel sdPanel = new SDisplayPanel(iRect); sdFrame.add(sdPanel); sdFrame.pack(); } private class SDisplayPanel extends JPanel { private InteractiveRectangle intRect; private int paintY = 20; public SDisplayPanel(InteractiveRectangle iR) { intRect = iR; setPreferredSize(new Dimension(500,500)); setBackground(Color.WHITE); } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; String tempString = intRect.getContigSeq(); int xDistance = 20; for(int i = 0; i < tempString.length(); i++) { if(i < (intRect.getStartOfUsedRegion() -1) || i > (intRect.getEndOfUsedRegion()-1)) g2.setColor(Color.BLACK); else g2.setColor(Color.RED); g2.drawString(String.valueOf(tempString.charAt(i)), xDistance, paintY); xDistance += 8; if(xDistance > 480) { xDistance = 20; paintY += 15; } } g2.setColor(Color.BLACK); paintY +=20; String totalLength = ("The total length is: " + String.valueOf(intRect.getTotalLength())); g2.drawString(totalLength, 20, paintY); } } public static void main(String[] args) { SDF sdf = new SDF(new InteractiveRectangle("hello world")); sdf.setLocation(200,200); sdf.setVisible(true); } } class InteractiveRectangle { String contigName; public InteractiveRectangle(String name) { contigName = name; } public String getContigName() { return contigName; } public String getContigSeq() { return "hello"; } public int getStartOfUsedRegion() { return 0; } public int getEndOfUsedRegion() { return 4; } public int getTotalLength() { return 5; } }
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 10-16-2007, 12:16 AM
Member
 
Join Date: Jul 2007
Posts: 10
Riftwalker is on a distinguished road
Thanks for all your help. I have things working the way I want them now.
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
refresh JPanel olesja AWT / Swing 1 04-16-2008 04:58 PM
Genarate JPanel Bill AWT / Swing 6 03-24-2008 08:38 PM
Problems while loading a JPanel to JApplet... Ananth Chellathurai Java Applets 0 11-24-2007 11:47 AM
JPanel won't update ibanez270dx New To Java 2 11-19-2007 05:42 AM
Problem with JPanel ibanez270dx New To Java 2 11-09-2007 06:04 PM


All times are GMT +3. The time now is 09:43 AM.


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