Results 1 to 2 of 2
- 11-19-2008, 07:53 PM #1
Member
- Join Date
- Sep 2008
- Posts
- 53
- Rep Power
- 0
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
-
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:
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)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);Last edited by Fubarable; 11-19-2008 at 08:39 PM.
Similar Threads
-
drawing window
By BlitzA in forum New To JavaReplies: 1Last Post: 01-15-2009, 12:55 PM -
Drawing a map
By Karp in forum AWT / SwingReplies: 4Last Post: 11-07-2008, 12:26 PM -
Help with 2-D Drawing
By Deathmonger in forum New To JavaReplies: 4Last Post: 06-18-2008, 02:23 AM -
Drawing on aJPanel
By Djangolo in forum AWT / SwingReplies: 1Last Post: 02-17-2008, 01:01 AM -
Drawing outside paintComponent()
By DarkSide1 in forum Java 2DReplies: 2Last Post: 11-08-2007, 10:36 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks