Results 1 to 6 of 6
- 05-10-2012, 04:52 PM #1
Member
- Join Date
- May 2012
- Posts
- 2
- Rep Power
- 0
Getting a JPanel to appear above another one
I have a frame, that has two components. A JComponent and a JLabel. The JLabel takes up the entire frame, but I want the JComponent to appear above it. Right now it is hiding beneath the JLabel. Any help would be appreciated! here is the code \
Java Code:import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JLabel; public class Main extends JPanel{ private static int frameWidth; private static int frameHeight; private static BufferedImage img = null; /** * @param args */ private static BufferedImage createImage(){ try { img = ImageIO.read(new File("Ithaca College Campus Map.PNG")); // System.out.println(img); return img; } catch (IOException e) { } return null; } public void paint(Graphics g) { g.drawImage(img, 0, 0, frameWidth, frameHeight, null); } public static void main(String[] args) { JFrame frame = new JFrame(); MyComponent component = new MyComponent(); createImage(); frameWidth = img.getWidth(); frameHeight = img.getHeight(); JLabel picLabel = new JLabel(new ImageIcon( img )); frame.getContentPane().add(component); //picLabel.add(new MyComponent()); frame.getContentPane().add(picLabel); //picLabel.add(new MyComponent()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(frameWidth, frameHeight); frame.setVisible(true); frame.repaint(); } } class MyComponent extends JComponent { // slices is an array of values that represent the size of each slice. public void drawPie(Graphics2D g, Rectangle area, PieValue[] slices) { // Get total value of all slices double total = 0.0D; for (int i=0; i<slices.length; i++) { total += slices[i].value; } // Draw each pie slice double curValue = 0.0D; int startAngle = 0; for (int i=0; i<slices.length; i++) { // Compute the start and stop angles startAngle = (int)(curValue * 360 / total); int arcAngle = (int)(slices[i].value * 360 / total); // Ensure that rounding errors do not leave a gap between the first and last slice if (i == slices.length-1) { arcAngle = 360 - startAngle; } // Set the color and draw a filled arc g.setColor(slices[i].color); g.fillArc(area.x, area.y, area.width, area.height, startAngle, arcAngle); curValue += slices[i].value; } } PieValue[] slices = new PieValue[4]; MyComponent() { slices[0] = new PieValue(25, Color.red); slices[1] = new PieValue(33, Color.green); slices[2] = new PieValue(20, Color.pink); slices[3] = new PieValue(15, Color.blue); } // This method is called whenever the contents needs to be painted public void paint(Graphics g) { // Draw the pie drawPie((Graphics2D)g, new Rectangle(0,0, 400, 400), slices); } }
- 05-10-2012, 05:50 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,604
- Rep Power
- 5
Re: Getting a JPanel to appear above another one
- 05-10-2012, 07:31 PM #3
Re: Getting a JPanel to appear above another one
Um, doWhile, wrong link?
Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)
A JFrame's contentPane has a BorderLayout by default.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-10-2012, 08:52 PM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,604
- Rep Power
- 5
- 05-11-2012, 04:27 PM #5
Member
- Join Date
- May 2012
- Posts
- 2
- Rep Power
- 0
- 05-11-2012, 07:34 PM #6
Re: Getting a JPanel to appear above another one
You've been given links to two tutorials, at least one of which is sure to be relevant to your still vague requirement. So, are you going to buckle down and learn something from the tutorials, or are you going to hang around here stating and re-stating your requirement in the same ambiguous terms?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Using an ActionListener To move from One JPanel in a JtabbedPane to another JPanel...
By Ermm in forum AWT / SwingReplies: 1Last Post: 12-13-2011, 02:38 AM -
Change JPanel text of Parent JPanel from JDialog
By bikashlama in forum AWT / SwingReplies: 7Last Post: 12-09-2011, 03:47 AM -
Adding a jpanel to a customized Jpanel Class
By trishtren in forum AWT / SwingReplies: 7Last Post: 04-05-2011, 06:52 PM -
Placing a new JPanel over a paint overriden JPanel
By Tanshaydar in forum AWT / SwingReplies: 4Last Post: 12-08-2010, 06:00 PM -
calling JPanel's JTextField from another JPanel class
By k2k in forum AWT / SwingReplies: 3Last Post: 04-20-2009, 11:31 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks