Results 1 to 6 of 6
  1. #1
    Andross561 is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default 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);
    	    }
    	}

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default 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.

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

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Getting a JPanel to appear above another one

    Quote Originally Posted by DarrylBurke View Post
    Um, doWhile, wrong link?
    Good point...I was thinking in 3 dimensions. I guess it depends what 'above' and 'below' actually mean in the original post.

  5. #5
    Andross561 is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default Re: Getting a JPanel to appear above another one

    Quote Originally Posted by doWhile View Post
    Good point...I was thinking in 3 dimensions. I guess it depends what 'above' and 'below' actually mean in the original post.
    I have two images, one is a campus map, while the other is a pie chart, I need the pie chart to be on top of the campus map so both images are visible.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default 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?

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

Similar Threads

  1. Replies: 1
    Last Post: 12-13-2011, 03:38 AM
  2. Change JPanel text of Parent JPanel from JDialog
    By bikashlama in forum AWT / Swing
    Replies: 7
    Last Post: 12-09-2011, 04:47 AM
  3. Adding a jpanel to a customized Jpanel Class
    By trishtren in forum AWT / Swing
    Replies: 7
    Last Post: 04-05-2011, 07:52 PM
  4. Placing a new JPanel over a paint overriden JPanel
    By Tanshaydar in forum AWT / Swing
    Replies: 4
    Last Post: 12-08-2010, 07:00 PM
  5. Replies: 3
    Last Post: 04-21-2009, 12:31 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
  •