Results 1 to 2 of 2
  1. #1
    CirKuT is offline Member
    Join Date
    Sep 2008
    Posts
    53
    Rep Power
    0

    Default Adding a Polyline drawing to a tab

    Hello everyone,

    I have a quick question. Im practicinf with 2D graphics and Netbeans and I have a quick question. I have created a simple GUI with 5 tabs. Nothing special at all. I also have a spiral drawing that I created with Polyline.
    I want to put the spiral into the fourth tab but Im unsure how to do this. Any suggestions? Posted below are my 3 classes in this order. 1)Launcher 2)The GUI 3)The spiral drawing

    package JTabbedPaneFrame;

    import javax.swing.JFrame;
    /**
    *
    * @author Morgan W. Leavitt, (2008).
    */
    public class JTabbedPaneDemo
    {

    public static void main(String args[])
    {

    JTabbedPaneFrame tabbedPaneFrame = new JTabbedPaneFrame();
    tabbedPaneFrame.setDefaultCloseOperation(JFrame.EX IT_ON_CLOSE);
    tabbedPaneFrame.setSize(380, 200);
    tabbedPaneFrame.setVisible(true);

    }// end method main
    }//end class JTabbedPaneDemo





    package JTabbedPaneFrame;

    import java.awt.BorderLayout;
    import java.awt.Color;
    import javax.swing.JFrame;
    import javax.swing.JTabbedPane;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import javax.swing.SwingConstants;

    /**
    *
    * @author Morgan W. Leavitt (2008)
    */
    public class JTabbedPaneFrame extends JFrame
    {
    //set up GUI
    public JTabbedPaneFrame()
    {

    super ( "JTabbedFrame Demo" );

    //create JTabbed Pane
    JTabbedPane tabbedPane = new JTabbedPane();

    //set up panel and add it to JTabbedPane
    JLabel label1 = new JLabel("panel one", SwingConstants.CENTER);

    //creates first panel
    JPanel panel1 = new JPanel();

    //add label to panel
    panel1.add( label1 );
    tabbedPane.addTab( "Tab One", null, panel1, "First Panel");

    //sets up the second panel
    JLabel label2 = new JLabel("panel two", SwingConstants.CENTER);

    //create second panel
    JPanel panel2 = new JPanel();

    //set background color to yellow
    panel2.setBackground(Color.YELLOW);

    //addd label to panel
    panel2.add(label2);
    tabbedPane.addTab ("Tab Two", null, panel2, "Second Panel");

    //sets up third panel
    JLabel label3 = new JLabel( "panel three", SwingConstants.CENTER);

    //creates third panel
    JPanel panel3 = new JPanel();

    //use border layout
    panel3.setLayout(new BorderLayout());
    panel3.add(new JButton("North"), BorderLayout.NORTH);
    panel3.add(new JButton("West"), BorderLayout.WEST);
    panel3.add(new JButton("East"), BorderLayout.EAST);
    panel3.add(new JButton("South"), BorderLayout.SOUTH);
    panel3.add(label3, BorderLayout.CENTER);
    tabbedPane.addTab("Tab Three", null, panel3, "Third Panel");

    //set up panel and add it to JTabbedPane
    JLabel label4 = new JLabel("panel four", SwingConstants.CENTER);

    //create fourth panel
    JPanel panel4 = new JPanel();

    //add label to panel
    panel4.add( label4 );
    tabbedPane.addTab( "Tab Four", null, panel4, "Fourth Panel");
    SpiralDrawing spiralDrawing = new SpiralDrawing();
    panel4.add(spiralDrawing);
    //set up panel and add it to JTabbedPane
    JLabel label5 = new JLabel("panel five", SwingConstants.CENTER);

    //create fourth panel
    JPanel panel5 = new JPanel();

    //add label to panel
    panel5.add( label5 );
    tabbedPane.addTab( "Tab Five", null, panel5, "Fith Panel");


    //add JTabbedPane to Frame
    add(tabbedPane);

    }//end JTabbedPaneFrame constructor
    }//end class JTabbedPaneFrame





    /*
    * Simple application that draws a spiral using the Polyline
    */

    package JTabbedPaneFrame;

    import java.awt.Graphics;
    import java.awt.Polygon;
    import javax.swing.JPanel;

    /**
    *
    * @author Morgan W. Leavitt (2008)
    */
    public class SpiralDrawing extends JPanel{

    public void paintComponent( Graphics g){
    super.paintComponent (g);

    //all x coordinates
    int xValues[] = {120, 130,140, 140, 130,120, 110, 110, 120, 135, 150, 150,
    135, 115, 100, 100, 115, 135};

    //all y coordinates
    int yValues[] = {120, 120, 110, 100, 90, 90, 100, 120, 130, 130, 115, 95,
    80, 80, 95, 125, 140, 140};


    //draws the spiral with 18 points by using the 18 values in the two arrays
    g.drawPolyline(xValues, yValues, 18);

    }//end method paintComponent

    }//end class SpiralJPanel

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

    Default

    Have a look at the various layouts available at the Sun Swing layout tutorial as they will allow you to add multiple components to a JPanel and help you position them where you want to. One in particular that is useful here is the BorderLayout which will solve your problem like so:

    Java Code:
        JLabel label4 = new JLabel("panel four", SwingConstants.CENTER);
        JPanel panel4 = new JPanel(new BorderLayout()); // add borderlayout here
        panel4.add(label4, BorderLayout.NORTH);
        tabbedPane.addTab("Tab Four", null, panel4, "Fourth Panel");
        SpiralDrawing spiralDrawing = new SpiralDrawing();
        panel4.add(spiralDrawing, BorderLayout.CENTER);
    You can find the tutorial on this here: How to Use BorderLayout (The Java™ Tutorials > Creating a GUI with JFC/Swing > Laying Out Components Within a Container)
    Last edited by Fubarable; 11-19-2008 at 08:39 PM.

Similar Threads

  1. drawing window
    By BlitzA in forum New To Java
    Replies: 1
    Last Post: 01-15-2009, 12:55 PM
  2. Drawing a map
    By Karp in forum AWT / Swing
    Replies: 4
    Last Post: 11-07-2008, 12:26 PM
  3. Help with 2-D Drawing
    By Deathmonger in forum New To Java
    Replies: 4
    Last Post: 06-18-2008, 02:23 AM
  4. Drawing on aJPanel
    By Djangolo in forum AWT / Swing
    Replies: 1
    Last Post: 02-17-2008, 01:01 AM
  5. Drawing outside paintComponent()
    By DarkSide1 in forum Java 2D
    Replies: 2
    Last Post: 11-08-2007, 10:36 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
  •