Results 1 to 7 of 7

Thread: JPanel Problems

  1. #1
    Riftwalker is offline Member
    Join Date
    Jul 2007
    Posts
    13
    Rep Power
    0

    Default 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?

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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.

  3. #3
    Riftwalker is offline Member
    Join Date
    Jul 2007
    Posts
    13
    Rep Power
    0

    Default

    I'm pretty sure that this is the case, but I'll post the pertinent code for each.

    JPanel 1 (the one that works)
    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){}
    	}
    JPanel 2 (the one with problems)
    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){}		
    	}
    Now this isn't the complete code for each class but it's the only drawing I have in each, etc.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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;

  5. #5
    Riftwalker is offline Member
    Join Date
    Jul 2007
    Posts
    13
    Rep Power
    0

    Default

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

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

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

  7. #7
    Riftwalker is offline Member
    Join Date
    Jul 2007
    Posts
    13
    Rep Power
    0

    Default

    Thanks for all your help. I have things working the way I want them now.

Similar Threads

  1. JPanel won't update
    By ibanez270dx in forum New To Java
    Replies: 3
    Last Post: 01-06-2009, 09:59 PM
  2. refresh JPanel
    By olesja in forum AWT / Swing
    Replies: 1
    Last Post: 04-16-2008, 04:58 PM
  3. Genarate JPanel
    By Bill in forum AWT / Swing
    Replies: 6
    Last Post: 03-24-2008, 08:38 PM
  4. Problems while loading a JPanel to JApplet...
    By Ananth Chellathurai in forum Java Applets
    Replies: 0
    Last Post: 11-24-2007, 11:47 AM
  5. Problem with JPanel
    By ibanez270dx in forum New To Java
    Replies: 2
    Last Post: 11-09-2007, 06:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •