Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By KevinWorkman

Thread: Minimized app all drawings gone

  1. #1
    taptaptill is offline Member
    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    0

    Default Minimized app all drawings gone

    Hi all,
    So here is my problem I'm trying to draw in a JPanel but when I reduced the application all drawings are gone except the last one.
    I looked everywhere but no solution!
    So here's my code :
    paintComponent () :
    Java Code:
    public class GraphicWorld extends JPanel
        {
                
         @Override
       public void paintComponent (Graphics g)
          {
                  
                  
           
           super.paintComponent(g);
           Graphics2D gfx = (Graphics2D)g;
           
           System.out.println(valuesId[0]);    
          if (!"0".equals(valuesId[0]))
          {
             
           if(valuesId[0].contains("Port number"))
            {
                
             gfx.drawString("(Port)", 30, 20);
             gfx.drawString(valuesId[1],30, 140);
            }
           if(valuesId[0].contains("Port Extension"))
            {
                gfx.drawString("(EXT)", 100, 20);
                gfx.drawString("("+valuesId[1]+" )", 100, 140);
              //  draw(g);
            }
           if(valuesId[0].contains("Forward Extension"))
            {
                
              //  draw(g);
            }
           if(valuesId[0].contains("Port Activity"))
            {
              if(valuesId[1].contains("ON-HOOK"))
              {
                  
               gfx.drawImage(imgOnhook,87, 70, null);
               gfx.dispose();
              }
              if(valuesId[1].contains("OFF-HOOK"))
              {
               gfx.drawImage(imgOffhook, 87, 70, null);
               }
            }
           if (valuesId[0].contains("Forward Staus"))
           {
               System.out.println("paaassss ");
               gfx.drawString("(FW)", 236, 20);
           }
           if(valuesId[0].contains("Forward Extension"))
            {
                    
                    gfx.drawLine(135,87,235,87);
                    gfx.drawLine(230, 91, 235, 87);
                    gfx.drawLine(230,83,235,87);
                    
                    gfx.drawOval(135,84,6,6);
                    gfx.setColor(Color.BLACK);
                    gfx.fillOval(135, 84, 6, 6);
                    
                if(valuesId[1].contains("NONE"))
                {
                   gfx.drawString("(NF)", 236 , 87);
                   
                }
                else
                {
                   
                   gfx.drawImage(imgOnhook,236,70, null);
                   
                
                }
            }
           if(valuesId[0].contains("Dialing"))
            {
                gfx.drawString("("+ valuesId[1]+" )", 100, 180);
                //draw(g);
            }
               
                
          }
        }
    
    }

    DocumentListener() :
    Java Code:
    protected class MyDocumentListener implements javax.swing.event.DocumentListener {
           
        
        
            @Override
       public void changedUpdate(javax.swing.event.DocumentEvent e) {
                // text has been altered in the textarea
                 
                }
            @Override
       public void insertUpdate(javax.swing.event.DocumentEvent e) 
            {
                    // text has been added to the textarea 
                      
                try { 
                   //if not prompt Line
                    if (!Traffic.getText(Traffic.getLineStartOffset(Traffic.getLineCount()-1),
                      Traffic.getLineEndOffset(Traffic.getLineCount()-1)-Traffic.getLineStartOffset(Traffic.getLineCount()-1)).contains(">>"))
                         {//if a line after a replace has been inserted
                  
                         if (Traffic.getLineCount()  == (lastreplace + 2) ) 
                  {
                           System.err.println(Traffic.getText(Traffic.getLineStartOffset(lastreplace),
                                                                 Traffic.getLineEndOffset(lastreplace) - 
                                                                    Traffic.getLineStartOffset(lastreplace)));
                     //  PortArchitecture (Traffic.getText(Traffic.getLineStartOffset(lastreplace),
                          //          Traffic.getLineEndOffset(lastreplace) - 
                           //                 Traffic.getLineStartOffset(lastreplace)));
               
                       
                      String line = Traffic.getText(Traffic.getLineStartOffset(lastreplace),Traffic.getLineEndOffset(lastreplace) -  Traffic.getLineStartOffset(lastreplace));
                     
                       //repaint only with a valid line
                       if (line.indexOf(":") != (-1)) 
                       {
                          valuesId[0] = line.substring(0,line.indexOf(":"));
                          valuesId[1] = line.substring(line.indexOf(":")+1);
                          
                          i+=1;
                          System.out.println(" i =  " + i + " :  "+ valuesId[0]);
                        
                          if (i==2)
                          {
                          
                              graphicPanel.repaint(30,10,100,180);//Port Number
                          
                          }
                          if(i==4)
                          {
                              graphicPanel.repaint(90,10,80,180);//Port Extension 
                          }
                        
                          if (i==5)
                          {
                              graphicPanel.repaint(87,70,50,35);//Port Activity
                          }
                          if (i==6)
                          {
                              graphicPanel.repaint(236,10,25,20);//(FW)
                          }
                          if (i==7)
                          {
                              graphicPanel.repaint(135,75,130,24);//FW EXT
                              
                          }
                          
                       } 
                       lastreplace +=1;
                  }
     
                    else//last line when it's not detected by the previous condition it'll be consumed here
                  {
                                //                     LAST       LINE
                  }
              }
                Traffic.setCaretPosition(Traffic.getDocument().getLength());
                
              
                } catch (BadLocationException ex) {
                    Logger.getLogger(TrafficSerialPort.class.getName()).log(Level.SEVERE, null, ex);
                }
    
            }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: Minimized app all drawings gone

    If you want help, you'll have to provide an SSCCE that we can copy and paste and run.

    When do you change the values held by the valuesId array? What are their values when you repaint successfully? What about when you repaint and don't get the drawings you expect? If you're unsure, step through your code with a debugger, or at least add some print statements to find out.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    taptaptill is offline Member
    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    0

    Default Re: Minimized app all drawings gone

    well I'll tell what i get :
    Everytime a line is inserted into a jTextarea I do the tests and a drawing appears in the JPanel like this line :
    Java Code:
    graphicPanel.repaint(30,10,100,180);//Port Number
    here I get from this line :
    Java Code:
    if(valuesId[0].contains("Port number"))
            {
                 
             gfx.drawString("(Port)", 30, 20);
             gfx.drawString(valuesId[1],30, 140);
            }
    2 strings in the JPanel : (Port) & (44) ==> if the port number is 44 but when I minimize the application all drawings are gone. As u see I'm overriding paintComponent(g) ,and when I'm repainting in the Document Listener I repaint only the rectangles that I need to repaint so the other paintings will not be gone.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: Minimized app all drawings gone

    It's probably overkill to only repaint certain portions of the window. Plus, you have no control over when the whole thing is repainted- such as when you minimize and restore your window. It sounds like you have some kind of logic clearing out the paintings after they are drawn, which probably isn't the way to go, since you have no control over when things are painted. Try resizing your window as well, that should trigger a repaint too.
    taptaptill likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    taptaptill is offline Member
    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    0

    Default Re: Minimized app all drawings gone

    yes when I resize it all drawings are gone but I didn't find another way to paint graphics into JPanel as the values are flowing.is there another way?

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: Minimized app all drawings gone

    You're doing the painting part right, by extending JPanel and overriding the paintComponent() method. The problem is that you don't have any control over when things are repainted- it could be many times a second; it's completely up to the OS. So you shouldn't do things like clearing drawings out after you've drawn them, otherwise they'll no longer be visible the next time it's repainted, which again you have no control over. Seems like you're going to have to rethink your logic.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 1
    Last Post: 11-08-2012, 03:33 AM
  2. JFrame minimized
    By ebubekird in forum AWT / Swing
    Replies: 2
    Last Post: 12-29-2011, 10:28 PM
  3. Print whole canvas drawings
    By mitra in forum AWT / Swing
    Replies: 0
    Last Post: 09-30-2011, 09:03 AM
  4. Need help for my application: Stroke Class & Free hand drawings
    By JavaIsChallenging in forum AWT / Swing
    Replies: 0
    Last Post: 09-23-2011, 06:17 PM
  5. Help using Buttons/Actions with Drawings
    By Deathmonger in forum New To Java
    Replies: 0
    Last Post: 04-08-2008, 03:11 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
  •