Results 1 to 7 of 7
Thread: JPanel Problems
- 10-10-2007, 09:39 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 13
- Rep Power
- 0
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?
- 10-10-2007, 09:52 PM #2
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.
- 10-10-2007, 10:07 PM #3
Member
- Join Date
- Jul 2007
- Posts
- 13
- Rep Power
- 0
I'm pretty sure that this is the case, but I'll post the pertinent code for each.
JPanel 1 (the one that works)
JPanel 2 (the one with problems)Java 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){} }
Now this isn't the complete code for each class but it's the only drawing I have in each, etc.Java 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){} }
- 10-10-2007, 10:36 PM #4
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.
Java 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;
- 10-12-2007, 10:30 PM #5
Member
- Join Date
- Jul 2007
- Posts
- 13
- Rep Power
- 0
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:
Java 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); } } }
- 10-13-2007, 01:42 AM #6
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).
Java 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; } }
- 10-15-2007, 11:16 PM #7
Member
- Join Date
- Jul 2007
- Posts
- 13
- Rep Power
- 0
Similar Threads
-
JPanel won't update
By ibanez270dx in forum New To JavaReplies: 3Last Post: 01-06-2009, 08:59 PM -
refresh JPanel
By olesja in forum AWT / SwingReplies: 1Last Post: 04-16-2008, 03:58 PM -
Genarate JPanel
By Bill in forum AWT / SwingReplies: 6Last Post: 03-24-2008, 07:38 PM -
Problems while loading a JPanel to JApplet...
By Ananth Chellathurai in forum Java AppletsReplies: 0Last Post: 11-24-2007, 10:47 AM -
Problem with JPanel
By ibanez270dx in forum New To JavaReplies: 2Last Post: 11-09-2007, 05:04 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks