Results 1 to 10 of 10
  1. #1
    imMadmartigan is offline Member
    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0

    Default Printing lines above JPanles

    Hi, i'm trying to a simple Applet where I need to draw some lines that pass above some JPanels objects with the click of the mouse. The program is articulated in one only class as follow:
    - one single main class extends JApplet (actually I've used the NetBeans GUI) and implements mouseListener.
    - paint(Graphics g) method calls the super paint method and print the lines stored in an ArrayList.

    The implementation of MouseClicked updates the Jpanel with a number and after it, repaint() is called.
    Now, I'm a beginner in Java, and I guess that the repaint() call the paint() method (implemented in the same class), which with super.paint() initialize the JPanels I've created with NetBeans drag&drop. So after super.paint() the following instructions draw all the lines with g.drawLines(p1.x, p1.y, p2.x, p2.y), where p1 and p2 are starting and end point.

    By doing in this way the lines pass behind the Jpanel but I cannot understand why, infact after the Jpanel is updated with a number I call immediately repaint() which only paints the lines.

    Do somebody know where is the problem?
    I cannot upload the code in this moment, but iI hope to have explained well this very simple program.

    Thanks!
    Tom

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

    Default Re: Printing lines above JPanles

    Is your paint method in the JApplet class itself? It shouldn't be. You should in fact be drawing in the paintComponent method of a JPanel class that is displayed by the JApplet as is well explained in the drawing with Swing tutorials..

    If you're still stuck, consider creating and posting an SSCCE.

  3. #3
    imMadmartigan is offline Member
    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0

    Default Re: Printing lines above JPanles

    Quote Originally Posted by Fubarable View Post
    Is your paint method in the JApplet class itself? It shouldn't be. You should in fact be drawing in the paintComponent method of a JPanel class that is displayed by the JApplet as is well explained in the drawing with Swing tutorials..

    If you're still stuck, consider creating and posting an SSCCE.
    Hi, thanks for replying so quickly.
    The paint method was in the JApplet class , you was right! So, I'm now implementing the paintComponent in the extended JPanel class. If I have problems again I'll come back.

    Thanks,
    Tom

  4. #4
    imMadmartigan is offline Member
    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0

    Default Re: Printing lines above JPanles

    Hi, the code is still not drawing the line above he Jpanel, I cannot understand why.
    Could somebody just give me an advise please, it is a very simple code actually.

    Thanks,
    Tom

    PHP Code:
    public class INVERTgameLev1 extends JApplet {
    
        tomographyPanel myPanel;
        raysInteract raysClick;
        int w, h;
    
        @Override
        public void init() {
            w = super.getSize().width;
            h = super.getSize().height;
            
            myPanel = new tomographyPanel();
            myPanel.setLayout(null);
            raysClick = new raysInteract(myPanel);
            try {
                java.awt.EventQueue.invokeAndWait(new Runnable() {
                    @Override
                    public void run() {
                        initComponent(myPanel);
                        add(myPanel);
                        myPanel.addMouseListener(raysClick);
                        repaint();
                    }
                });
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        
        private void initComponent(tomographyPanel p){
            Rectangle bounds1 =new Rectangle(Math.round(w/2)-100, Math.round(h/2)-100, 100, 100);
            Rectangle bounds2 =new Rectangle(Math.round(w/2)-100, Math.round(h/2), 100, 100);
            for(int i=0; i<=1; i++){
                bounds1.x = bounds1.x + i*100;
                bounds2.x = bounds2.x + i*100;
                JPanel panel1= new JPanel();
                p.add(panel1);
                panel1.setBounds(bounds1);
                panel1.setBorder(BorderFactory.createLineBorder(Color.black, 2));
                panel1.setBackground(Color.WHITE);
                JPanel panel2= new JPanel();
                p.add(panel2);
                panel2.setBounds(bounds2);
                panel2.setBorder(BorderFactory.createLineBorder(Color.black, 2));
                panel2.setBackground(Color.WHITE);
            }
            
        }
    }
    
    class tomographyPanel extends JPanel{
        @Override
        protected void paintComponent(Graphics g){
                super.paintComponent(g);
                g.drawLine(2, 5, 300, 300); //Example of a line drawn above a JPanel
        }
        
       
    }
    
    class raysInteract extends MouseInputAdapter {
    
        tomographyPanel panel;
        ArrayList start, end;
        int lev;
    
        raysInteract(tomographyPanel panel) {
            this.panel = panel;
            lev=-1;
            start = new ArrayList();
            end = new ArrayList();
        }
        
        @Override
        public void mouseClicked(MouseEvent e) {
            Component[] jPanel = panel.getComponents(); 
            int extLine = 20;
            int wd,ht;
    
            // Here I should save in start and end Arrays the starting points and end points of the lines. Not yet done
    
            panel.repaint();
            e.consume();
        }
    }

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Printing lines above JPanles

    Quote Originally Posted by imMadmartigan View Post
    PHP Code:
    public class INVERTgameLev1 extends JApplet {
    
        tomographyPanel myPanel;
        raysInteract raysClick;
        :
        :
    }
    
    class tomographyPanel extends JPanel{
    :
    :
    }
    
    class raysInteract extends MouseInputAdapter {
    :
    :
    }
    Learn and respect the Java coding conventions: class names start with an uppercase letter.
    Code Conventions for the Java Programming Language: Contents

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Printing lines above JPanles

    I ran your code (after refactoring it to conform to the conventions and to improve readability) and I see the line, partly covered by the contained JPanels.

    What do you see and what did you expect?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    imMadmartigan is offline Member
    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0

    Default Re: Printing lines above JPanles

    Hi thanks for replying. Any correction is very welcome.
    I expected a line passing above all the contained JPanels. But is not. I thought that, because the line is drawn as the last command, the line would have passed over everything, so I'm definitely stuck.

    Tom

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Printing lines above JPanles

    Read the API for JComponent#paint(...). What are the three methods it delegates to, and in what order?

    Now do you understand?

    Oh, and Java doesn't have commands. Java has statements.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    imMadmartigan is offline Member
    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0

    Default Re: Printing lines above JPanles

    So, it seems that paintComponent causes paint to be executed and thus: paintComponent(Graphics g), paintBorder(Graphics g) and paintChildren(Graphics g). The last one set the Panels I added in the object myPanel. So they are painted at the end. Am I right?
    So I should need a Layered Panes or a Glass Pane to order the depth of each component or to print over everything.

    Hoping it is right, thanks for driving me to the solution!! :)
    Tom

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

    Default Re: Printing lines above JPanles

    Quote Originally Posted by imMadmartigan View Post
    So, it seems that paintComponent causes paint to be executed
    No, it's quite the opposite. The paint method will call paintComponent, paintBorder, and paintChildren. paintComponent will not call paint. Note that repaint() will usually result in a call to paint, and perhaps that is what you were referring to.

    So I should need a Layered Panes or a Glass Pane to order the depth of each component or to print over everything.
    Not necessarily. If you're adding components to layers, then yes, this is helpful, but if you're just drawing, then this isn't necessary.

Similar Threads

  1. Printing values from Array on individual lines
    By Castle in forum New To Java
    Replies: 2
    Last Post: 11-28-2012, 01:43 AM
  2. Sorting and printing multiple lines of data
    By ManInTheMiddle in forum New To Java
    Replies: 1
    Last Post: 03-27-2012, 09:43 PM
  3. Printing to the screen specific lines from a file
    By stevenhaynes5 in forum New To Java
    Replies: 9
    Last Post: 08-15-2011, 05:28 PM
  4. reading lines from URL and printing backwards
    By luxurymode in forum Advanced Java
    Replies: 1
    Last Post: 03-30-2011, 08:18 AM
  5. SAME JComboBox in 2 JPanles?
    By ellias2007 in forum AWT / Swing
    Replies: 4
    Last Post: 08-18-2010, 04:13 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
  •