Results 1 to 2 of 2
  1. #1
    prseifer is offline Member
    Join Date
    Mar 2009
    Posts
    2
    Rep Power
    0

    Default Drawing a polyline within a JTabbedPane

    I am having an issue drawing lines within a tab of a JTabbedPane. The tab calls Bracket() which extends a JPanel. I can draw both instances of BracketBuilder but, the instance that is added to the North section of the BorderLayout is cut off. In other words the Layout Manager is not expanding to include the entire polyline. Two questions. 1. What am I doing wrong to get this cut off image? 2. Is there a better way to draw these images? I believe the JTabbedPane requires the extension of JPanel. See code below.

    Java Code:
    package cwbracketsystem;
    
    import java.awt.*;
    import javax.swing.*;
    
    /**
     *
     * @author Seifert
     */
    public class Bracket extends JPanel{
    
        private int[] x = {0, 60, 60, 0};
        private int[] y = {0, 0, 20, 20};
        private int pts = 4;
        private int[] xa = {100, 160, 160, 100};
        private int[] ya = {100, 100, 120, 120};
        private int ptsa = 4;
    
        private JPanel brack1;
        private JPanel brack2;
        
        public Bracket(){
        BracketBuilder br1 = new BracketBuilder();
        br1.buildbracket(x, y, pts);
        
        BracketBuilder br2 = new BracketBuilder();
        br2.buildbracket(xa, ya, ptsa);
        
        brack1 = new JPanel();
        brack2 = new JPanel();
        brack1.setLayout(new BorderLayout());
        brack2.setLayout(new BorderLayout());
        setLayout(new BorderLayout());
        
        brack1.add(br1, BorderLayout.NORTH);
        brack2.add(br2);
        add(brack1, BorderLayout.NORTH);
        add(brack2, BorderLayout.CENTER);
    
        }
    
    }//end class Bracket
    Java Code:
    package cwbracketsystem;
    import java.awt.*;
    import javax.swing.*;
    
    public class BracketBuilder extends JPanel{
        
        private int[] xv;
        private int[] yv;
        private int ptsv;
    
    public BracketBuilder(){
    
    }
    public void paint(Graphics g){
    super.paintComponent (g);
    //all x coordinates
    int xValues[] = xv;
    
    //all y coordinates
    int yValues[] = yv;
    int pts = ptsv;
    
    //draws the matchup bracket
    g.drawPolyline(xValues, yValues, pts);
    
    }//end method paintComponent
    
    public void buildbracket(int[] xvals, int[] yvals, int pts){
    
    xv = xvals;
    yv = yvals;
    ptsv = pts;
    }//end method
    
    }//end class BracketBuilder
    Thanks for any help/comments.

  2. #2
    prseifer is offline Member
    Join Date
    Mar 2009
    Posts
    2
    Rep Power
    0

    Default

    ahh i figured it out... I just needed to use the getPrefferedSize

    Java Code:
    package cwbracketsystem;
    
    import java.awt.*;
    import javax.swing.*;
    
    /**
     *
     * @author Seifert
     */
    public class Bracket extends JPanel{
    
        private int[] x = {0, 60, 60, 0};
        private int[] y = {0, 0, 20, 20};
        private int pts = 4;
        private int[] xa = {100, 160, 160, 100};
        private int[] ya = {100, 100, 120, 120};
        private int ptsa = 4;
    
        private JPanel brack1;
        private JPanel brack2;
    
        public Bracket(){
    
        BracketBuilder br1 = new BracketBuilder();
        br1.buildbracket(x, y, pts);
        
        BracketBuilder br2 = new BracketBuilder();
        br2.buildbracket(xa, ya, ptsa);
        
        brack1 = new JPanel();
        brack2 = new JPanel();
        brack1.setLayout(new BorderLayout());
        brack2.setLayout(new BorderLayout());
        setLayout(new BorderLayout());
        
        brack1.add(br1, BorderLayout.NORTH);
        brack2.add(br2);
        add(brack1, BorderLayout.NORTH);
        add(brack2, BorderLayout.CENTER);
    
        }
    
    }//end class Bracket
    Java Code:
    package cwbracketsystem;
    import java.awt.*;
    import javax.swing.*;
    
    public class BracketBuilder extends JPanel{
        
        private int[] xv;
        private int[] yv;
        private int ptsv;
    
    public BracketBuilder(){
        setBackground(Color.WHITE);
    
    }
    public Dimension getPreferredSize() {
            return new Dimension(80,40);
        }
    
    public void paintComponent(Graphics g){
    super.paintComponent (g);
    //all x coordinates
    int xValues[] = xv;
    
    //all y coordinates
    int yValues[] = yv;
    int pts = ptsv;
    
    //draws the matchup bracket
    g.setColor(Color.BLACK);
    g.drawPolyline(xValues, yValues, pts);
    
    
    
    }//end method paintComponent
    
    public void buildbracket(int[] xvals, int[] yvals, int pts){
    
    xv = xvals;
    yv = yvals;
    ptsv = pts;
    }//end method
    
    }//end class BracketBuilder

Similar Threads

  1. Traversing a JTabbedPane
    By Inks in forum AWT / Swing
    Replies: 12
    Last Post: 03-11-2009, 06:15 AM
  2. Adding a Polyline drawing to a tab
    By CirKuT in forum New To Java
    Replies: 1
    Last Post: 11-19-2008, 09:32 PM
  3. How to use JTabbedPane
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 08:50 PM
  4. Help with JTabbedPane
    By lenny in forum SWT / JFace
    Replies: 1
    Last Post: 08-07-2007, 07:18 AM
  5. Same component on all JTabbedPane
    By java_novice in forum AWT / Swing
    Replies: 4
    Last Post: 08-06-2007, 10:09 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
  •