Results 1 to 7 of 7
  1. #1
    joeyea is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Unhappy Help with paintComponent!

    Hiya,

    I've got a rather large program, a catalogue system. It all works fine, except I have a frame that shows a list of order states. ANyway i want to display graphics on that frame for loading, but I cannot seem to get any of it working. I've used graphics on classes on there own so I understand how the graphics work, but for some reason I cannot get it working in this frame. I have a panel created called paintPanel so far. Anyway pointers on how to draw inside this panel would be greatly appreciated

    Here are my class codes
    Class DisplayGUI

    Java Code:
    package Clients;
    
    import java.awt.*;
    import java.util.List;
    import java.util.Map;
    import java.util.Collection;
    import javax.swing.*;
    import Middle.*;
    import java.util.Set;
    import java.util.HashMap;
    
    
    
    
    class DisplayGUI extends JFrame
    {
      private enum State { process, checked }
      private static final int H = 600;       // Height of window pixels
      private static final int W = 600;       // Width  of window pixels
      private OrderProcessing theOrder     = null;
     
      private JLabel      waiting  = new JLabel();
      private JLabel   beingProcessed  = new JLabel();
      private JLabel     waitingForCollection  = new JLabel();
      private JTextArea   theWaiting  = new JTextArea();
      private JTextArea  theBeingProcessed  = new JTextArea();
      private JTextArea   theWaitingForCollection  = new JTextArea();
      private JPanel    paintPanel = new JPanel();
      
    
      
      public DisplayGUI(  RootPaneContainer rpc, MiddleFactory mf  )
      {
     try                                           //
        {     
          theOrder = mf.makeOrderProcessing();        // Process order
        } catch ( Exception e )
        {
          System.out.println("Exception: " + e.getMessage() );
        }
    
        Container cp         = rpc.getContentPane();    // Content Pane
        Container rootWindow = (Container) rpc;         // Root Window
        cp.setLayout(null);                             // No layout manager
        rootWindow.setSize( W, H );                     // Size of Window
     
        Font f = new Font("Monospaced",Font.PLAIN,12);  // Font f is
    
        waiting.setBounds( 12, 35 , 170, 20 );       // Message area
        waiting.setText( "Waiting" );                      
        cp.add(BorderLayout.EAST, waiting );
        
        theWaiting.setBounds( 10, 65 , 170, 150 );       // Message area
        cp.add(BorderLayout.EAST, theWaiting );                            //  Add to canvas
        
        beingProcessed.setBounds( 202, 35 , 170, 20 );       // Message area
        beingProcessed.setText( "Being Processed" );                      
        cp.add(beingProcessed );
        
        paintPanel.setBounds( 10, 240 , 170, 150 );       // Message area         #
        paintPanel.setBackground(Color.white);
        cp.add(paintPanel );
        
        
        theBeingProcessed.setBounds( 200, 65 , 170, 150 );       // Message area                    
        cp.add( theBeingProcessed );                            //  Add to canvas
        
        waitingForCollection.setBounds( 392, 35 , 170, 20 );       // Message area
        waitingForCollection.setText( "Waiting For Collection" );                      
        cp.add(waitingForCollection );
        
        
        theWaitingForCollection.setBounds( 390, 65 , 170, 150 );       // Message area                      
        cp.add( theWaitingForCollection );                            //  Add to canvas
        
     
        
          rootWindow.setVisible( true );                  // Make visible
          
          backgroundDisplay bd = new backgroundDisplay();
          bd.start();
          
          
        
    
    } 
    
    
    
    
    class backgroundDisplay extends Thread {
    
     public void run() {
          
          while ( true )
          {
            try
            {
             Processing.Order order = new Processing.Order();
           
              Map res = theOrder.getOrderState();
              System.out.println(res);
              
            {
                StringBuffer sb =   new StringBuffer();
                     
                    Object waitingList = res.get("Waiting");
                    String waitingListString = waitingList.toString();
                    int n = waitingListString.length();
                    for(int i=0; i < n; i++)
                    {
                        if(!((waitingListString.charAt(i) == '[')||(waitingListString.charAt(i) == ']')))
                        {
                            sb.append(waitingListString.charAt(i));
                        }
                        else
                        {
                            
                        }
                    }
                    String finalWaiting = sb.toString();
                    theWaiting.setText( finalWaiting );  
                    
                    StringBuffer sc =   new StringBuffer();
                    
                    Object beingPickedList = res.get("BeingPicked");
                    String beingPickedString = beingPickedList.toString();
                       int a = waitingListString.length();
                    for(int i=0; i < a; i++)
                    {
                        if(!((beingPickedString.charAt(i) == '[')||(beingPickedString.charAt(i) == ']')))
                        {
                            sc.append(beingPickedString.charAt(i));
                        }
                        else
                        {
                            
                        }
                    }
                    String finalProcessed = sc.toString();
                    theBeingProcessed.setText( finalProcessed );  
                    
                    Object toBeCollectedList = res.get("ToBeCollected");
                    String beingCollectedString = toBeCollectedList.toString();
                    theWaitingForCollection.setText( beingCollectedString );   
               
                }
              sleep(5000);                             // delay
            } catch ( Exception err )
            {
            }
          }
        }
       
    
            
        }
            }
    Class DisplayCLient
    Java Code:
    package Clients;
    
    import javax.swing.*;
    import java.awt.*;
    import Middle.*;
    
    /**
     * The Display Client.
     * @author  Michael Alexander Smith
     * @version 2.0
     */
    
    
    public class DisplayClient
    {
       public static void main (String args[])
       {
         String stockURL = args.length < 1     // URL of stock RW
                         ? Names.STOCK_RW      //  default  location
                         : args[0];            //  supplied location
         String orderURL = args.length < 2     // URL of order
                         ? Names.ORDER         //  default  location
                         : args[1];            //  supplied location
         
        RemoteMiddleFactory mrf = new RemoteMiddleFactory();
        mrf.setStockRWInfo( stockURL );
        mrf.setOrderInfo  ( orderURL );        //
        displayGUI(mrf);                       // Create GUI
      }
      
      public static void displayGUI(MiddleFactory mf)
      {     
        JFrame  window = new JFrame();
         
        window.setTitle( "Display Client (RMI)");
        window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        new DisplayGUI( window, mf );
      }
    
    }

    Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You state you need help with paintComponent, yet you're not showing your paintComponent code, and that confuses me. Shouldn't your paintPanel override this method?

  3. #3
    joeyea is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    I have this :

    public void paintComponent(Graphics paintFrame) {
    int width = getWidth();
    int height = getHeight();
    paintFrame.setColor(Color.black);
    paintFrame.drawOval(0, 0, width, height);
    }

    which I have placed :

    Java Code:
    package Clients;
    
    import java.awt.*;
    import java.util.List;
    import java.util.Map;
    import java.util.Collection;
    import javax.swing.*;
    import Middle.*;
    import java.util.Set;
    import java.util.HashMap;
    
    
    
    
    class DisplayGUI extends JFrame
    {
      private enum State { process, checked }
      private static final int H = 600;       // Height of window pixels
      private static final int W = 600;       // Width  of window pixels
      private OrderProcessing theOrder     = null;
     
      private JLabel      waiting  = new JLabel();
      private JLabel   beingProcessed  = new JLabel();
      private JLabel     waitingForCollection  = new JLabel();
      private JTextArea   theWaiting  = new JTextArea();
      private JTextArea  theBeingProcessed  = new JTextArea();
      private JTextArea   theWaitingForCollection  = new JTextArea();
      private JPanel    paintFrame = new JPanel();
      
    [B][COLOR="Red"]//HERE//[/COLOR][/B]
      
     public void paintComponent(Graphics paintFrame) {
        int width = getWidth();
        int height = getHeight();
        paintFrame.setColor(Color.black);
        paintFrame.drawOval(0, 0, width, height);
      }
      
      public DisplayGUI(  RootPaneContainer rpc, MiddleFactory mf  )
      {
     try                                           //
        {     
          theOrder = mf.makeOrderProcessing();        // Process order
        } catch ( Exception e )
        {
          System.out.println("Exception: " + e.getMessage() );
        }
    
        Container cp         = rpc.getContentPane();    // Content Pane
        Container rootWindow = (Container) rpc;         // Root Window
        cp.setLayout(null);                             // No layout manager
        rootWindow.setSize( W, H );                     // Size of Window
     
        Font f = new Font("Monospaced",Font.PLAIN,12);  // Font f is
    
        waiting.setBounds( 12, 35 , 170, 20 );       // Message area
        waiting.setText( "Waiting" );                      
        cp.add(BorderLayout.EAST, waiting );
        
        theWaiting.setBounds( 10, 65 , 170, 150 );       // Message area
        cp.add(BorderLayout.EAST, theWaiting );                            //  Add to canvas
        
        beingProcessed.setBounds( 202, 35 , 170, 20 );       // Message area
        beingProcessed.setText( "Being Processed" );                      
        cp.add(beingProcessed );
        
        paintFrame.setBounds( 10, 240 , 170, 150 );       // Message area         #
        paintFrame.setBackground(Color.white);
        cp.add(paintFrame );
        
        
        theBeingProcessed.setBounds( 200, 65 , 170, 150 );       // Message area                    
        cp.add( theBeingProcessed );                            //  Add to canvas
        
        waitingForCollection.setBounds( 392, 35 , 170, 20 );       // Message area
        waitingForCollection.setText( "Waiting For Collection" );                      
        cp.add(waitingForCollection );
        
        
        theWaitingForCollection.setBounds( 390, 65 , 170, 150 );       // Message area                      
        cp.add( theWaitingForCollection );                            //  Add to canvas
        
     
        
          rootWindow.setVisible( true );                  // Make visible
          
          backgroundDisplay bd = new backgroundDisplay();
          bd.start();
          
          
        
    
    } 
    
    
    
    
    class backgroundDisplay extends Thread {
    
     public void run() {
          
          while ( true )
          {
            try
            {
             Processing.Order order = new Processing.Order();
           
              Map res = theOrder.getOrderState();
              System.out.println(res);
              
            {
                StringBuffer sb =   new StringBuffer();
                     
                    Object waitingList = res.get("Waiting");
                    String waitingListString = waitingList.toString();
                    int n = waitingListString.length();
                    for(int i=0; i < n; i++)
                    {
                        if(!((waitingListString.charAt(i) == '[')||(waitingListString.charAt(i) == ']')))
                        {
                            sb.append(waitingListString.charAt(i));
                        }
                        else
                        {
                            
                        }
                    }
                    String finalWaiting = sb.toString();
                    theWaiting.setText( finalWaiting );  
                    
                    StringBuffer sc =   new StringBuffer();
                    
                    Object beingPickedList = res.get("BeingPicked");
                    String beingPickedString = beingPickedList.toString();
                       int a = waitingListString.length();
                    for(int i=0; i < a; i++)
                    {
                        if(!((beingPickedString.charAt(i) == '[')||(beingPickedString.charAt(i) == ']')))
                        {
                            sc.append(beingPickedString.charAt(i));
                        }
                        else
                        {
                            
                        }
                    }
                    String finalProcessed = sc.toString();
                    theBeingProcessed.setText( finalProcessed );  
                    
                    Object toBeCollectedList = res.get("ToBeCollected");
                    String beingCollectedString = toBeCollectedList.toString();
                    theWaitingForCollection.setText( beingCollectedString );   
               
                }
              sleep(5000);                             // delay
            } catch ( Exception err )
            {
            }
          }
        }
       
    
            
        }
            }
    But obviously I'm not doing it properly as it's not drawing anything in the JPanel, I'd take a guess at I havn't pointed it to paint that specific panel?

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default

    A JFrame isn't a JComponent so it can never override a paintComponent( ... ) method and it will never be called. A JFrame has a content pane that is painted in the area of the JFrame. The content pane is a JComponent (a JPanel perhaps?). Read the API documentation of the JFrame class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    joeyea is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    Thnaks for the reply, I have declared and added a JPanel called paintFrame (sorry for the confusing name!) to my content pane.
    taken from my code :

    Java Code:
    private JPanel    paintFrame = new JPanel();
    
     paintFrame.setBounds( 10, 240 , 170, 150 );       
     paintFrame.setBackground(Color.white);
     cp.add(paintFrame );
    and my code to paint on this so far is :

    Java Code:
     public void paintComponent(Graphics paintFrame) {
        int width = getWidth();
        int height = getHeight();
        paintFrame.setColor(Color.black);
        paintFrame.drawOval(0, 0, width, height);
      }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Please read the tutorials on painting in Swing as need to unlearn some assumptions. You must extend JPanel or other JComponent to be able to override and use its paintComponent method. Again, I strongly urge you to study the tutorials.

    Edit: an example of extending a JPanel and overriding its paintComponent is as Jos is showing below.
    Last edited by Fubarable; 12-27-2010 at 03:02 PM.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by joeyea View Post
    Thnaks for the reply, I have declared and added a JPanel called paintFrame (sorry for the confusing name!) to my content pane.
    taken from my code :

    Java Code:
    private JPanel    paintFrame = new JPanel();
    
     paintFrame.setBounds( 10, 240 , 170, 150 );       
     paintFrame.setBackground(Color.white);
     cp.add(paintFrame );
    and my code to paint on this so far is :

    Java Code:
     public void paintComponent(Graphics paintFrame) {
        int width = getWidth();
        int height = getHeight();
        paintFrame.setColor(Color.black);
        paintFrame.drawOval(0, 0, width, height);
      }
    You can't just define a paintComponent( ... ) method anywhere and expect it to be called when an unrelated JPanel needs to be repainted. You have to extend the JPanel class and override that method in that class so it'll be called whenever the object needs to be repainted, so your paintPanel has to be of this class:

    Java Code:
    public class PaintPanel extends JPanel {
       ...
       public void paintComponent(Graphics g) {
          // do your graphics here
       }
    }
    and, of course your have to define your paintPanel variable like this:

    Java Code:
    PaintPanel paintPanel= new PaintPanel();
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. JPanel PaintComponent
    By capiono in forum AWT / Swing
    Replies: 5
    Last Post: 10-31-2010, 03:36 AM
  2. Working around paintcomponent
    By sahhhm in forum New To Java
    Replies: 2
    Last Post: 05-16-2008, 03:43 AM
  3. Problem going outside paintComponent
    By Thez in forum Java 2D
    Replies: 9
    Last Post: 12-08-2007, 05:59 PM
  4. Drawing outside paintComponent()
    By DarkSide1 in forum Java 2D
    Replies: 2
    Last Post: 11-08-2007, 11:36 PM
  5. paint() and paintComponent()
    By goldhouse in forum Java 2D
    Replies: 1
    Last Post: 07-17-2007, 04:43 AM

Posting Permissions

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