Results 1 to 12 of 12
  1. #1
    Dean29126 is offline Member
    Join Date
    Sep 2010
    Posts
    18
    Rep Power
    0

    Question JPanels and JFrames

    I have trouble displaying the Underlying class onto the main frame.
    It works fine with my DnD class, any solutions? Here are codes below

    DnD.java codes:

    Java Code:
    public class DnD extends JPanel {
        
    	
    	static String imageFile = "bgfinal.png";  //  @jve:decl-index=0:
    
    		
    	private static final long serialVersionUID = 1L;
    	private BufferedImage buffImg2;  //  @jve:decl-index=0:
    	private JLabel jLabelderivative = null;
    	private JLabel jLabelfutures = null;
    	private JLabel jLabellong = null;
    	private JLabel jLabeloptions = null;
    	private JLabel jLabelshort = null;
    
    	private JLabel jLabelsinglestockfutures = null;
    
    	private JLabel jLabelstockindex = null;
    
    	private JLabel jLabelstockindexfutures = null;
    
    
    	/**
    	 * This is the default constructor
    	 */
    	public DnD() {
    		super();
    		initialize();
    
    
    	}
    
    	/**
    	 * This method initializes this
    	 * @return 
    	 * @return 
    	 * 
    	 * @return void
    	 */
    	private void initialize() {
    		
    		String filePath = getClass().getResource("images").toString();
    		filePath = filePath.substring(6);
    		File f = new File(filePath + "/bgfinal" + ".png");
    		try {
    			buffImg2 = ImageIO.read(f);
    		} catch (IOException e2) {
    			e2.printStackTrace();
    			
    		}
                    this.setSize(720, 480);
    		this.setLayout(null);
    		this.setBackground(Color.white);
    }
    
    public void paintComponent(Graphics g) {
            super.paintComponent(g); 
            g.drawImage(buffImg2, 0, 0, null);
            
            
        }//end paintComponent
    	
    	
    
    }  //  @jve:decl-index=0:visual-constraint="16,7"
    MainFrame.java code:

    Java Code:
    public class MainFrame extends JFrame {
    
    	private static final long serialVersionUID = 1L;
    	private JPanel jContentPane = null;
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				MainFrame thisClass = new MainFrame();
    				thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				thisClass.setVisible(true);
    			}
    		});
    	}
    
    	/**
    	 * This is the default constructor
    	 */
    	public MainFrame() {
    		super();
    		initialize();
    		JPanel DnD = new DnD();
    		JPanel Underlying = new Underlying();
    		this.getContentPane().add(DnD, Underlying);
    		this.setVisible(true);
    		
    		
    	}
    
    	/**
    	 * This method initializes this
    	 * 
    	 * @return void
    	 */
    	private void initialize() {
    		this.setSize(720, 480);
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setContentPane(getJContentPane());
    		this.setTitle("Drag and Drop");
    		this.setVisible(true);
    	}
    
    	/**
    	 * This method initializes jContentPane
    	 * 
    	 * @return javax.swing.JPanel
    	 */
    	private JPanel getJContentPane() {
    		if (jContentPane == null) {
    			jContentPane = new JPanel();
    			jContentPane.setLayout(null);
    		}
    		return jContentPane;
    	}
    
    }
    Underlying.java code:


    Java Code:
    public class Underlying extends JPanel implements MouseListener,
    		MouseMotionListener {
    
    	static String imageFile = "underlyingtest.png"; // @jve:decl-index=0:
    	private static final int w = 84;
    	private static final int h = 15;
    
    	private BufferedImage buffImg2;
    
    	private boolean _canDrag = false;
    
    	private int _labelX = 505; // x coord - set from drag
    	private int _labelY = 77; // y coord - set from drag
    
    	private int _dragFromX = 0;
    	private int _dragFromY = 0;
     
    	public Underlying() {
    		super();
    		initialize();
    		this.addMouseListener(this); 
            this.addMouseMotionListener(this);
    	}
    	
    	private void initialize() {
    		String filePath = getClass().getResource("images").toString();
    		filePath = filePath.substring(6);
    		File f = new File(filePath + "/underlyingtest" + ".png");
    		try {
    			buffImg2 = ImageIO.read(f);
    		} catch (IOException e2) {
    			e2.printStackTrace();
    		}
    		
    	}
    	public void paintComponent(Graphics g) {
            super.paintComponent(g); // Required for background.
            g.drawImage(buffImg2, _labelX, _labelY, w, h, null);
                  
        }//end paintComponent
    	@Override
    	public void mouseDragged(MouseEvent e) {
    		 if (_canDrag) {   // True only if button was pressed inside label.
    	            //--- label pos from mouse and original click displacement
    	            _labelX = e.getX() - _dragFromX;
    	            _labelY = e.getY() - _dragFromY;
    	            
    	            //--- Don't move the label off the screen sides
    	            _labelX = Math.max(_labelX, 0);
    	            _labelX = Math.min(_labelX, getWidth() - w);
    	            
    	            //--- Don't move the label off top or bottom
    	            _labelY = Math.max(_labelY, 0);
    	            _labelY = Math.min(_labelY, getHeight() - h);
    	            
    	            this.repaint(); }
    	}
    
    	@Override
    	public void mouseMoved(MouseEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void mouseClicked(MouseEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void mouseEntered(MouseEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void mouseExited(MouseEvent e) {
    		_canDrag = false;
    
    	}
    
    	@Override
    	public void mousePressed(MouseEvent e) {
    		int x = e.getX();   // Save the x coord of the click
            int y = e.getY();   // Save the y coord of the click
            
            if (x >= _labelX && x <= (_labelX + w)
                    && y >= _labelY && y <= (_labelY + h)) {
                _canDrag = true;
                _dragFromX = x - _labelX;  // how far from left
                _dragFromY = y - _labelY;  // how far from top
            } else {
                _canDrag = false;
            }
    
    	}
    
    	@Override
    	public void mouseReleased(MouseEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    }

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

    Default

    Hopefully you'll get an answer soon, but if not, you may wish to place debugging statements in your code to try to isolate the problem, and then failing that, create a much smaller, compilable program that demonstrates the problem because you're currently asking us to go through a bit of code without showing us where to look. Luck.

  3. #3
    Dean29126 is offline Member
    Join Date
    Sep 2010
    Posts
    18
    Rep Power
    0

    Default

    Basically, it's under the mainframe for i cant get the image from underlying to show up. do i have to use a layeredpane? or is there any way i could overlap the 2 panels(DnD and underlying)?

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

    Default

    This confuses the heck out of me, and I'm surprised that it even compiles:

    Java Code:
    		JPanel DnD = new DnD();
    		JPanel Underlying = new Underlying();
    		this.getContentPane().add(DnD, Underlying);
    What are you trying to do here and where have you seen this done like this before?

  5. #5
    Dean29126 is offline Member
    Join Date
    Sep 2010
    Posts
    18
    Rep Power
    0

    Default

    it compiles, i was doing it by trial and error, how should i go about it then? since i'm not using array list to store the images, is there any way i could overlap these 2 panels and display them onto JFrame at the same time? the image i used in underlying.java is just a small rectangle. the image in DnD however is the background.

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

    Default

    Quote Originally Posted by Dean29126 View Post
    it compiles,

    Ah, I see now why it compiles. Container has an add method that takes a Component and an Object as parameters, the Object which should be a constraint of some sort for the add method. So the compiler thinks that your second parameter is the constraint, which it obviously is not.


    i was doing it by trial and error,
    This will never work when trying to code a complex library such as Swing. You need to study the tutorials and the API to see how things are done, not guess.


    how should i go about it then? since i'm not using array list to store the images, is there any way i could overlap these 2 panels and display them onto JFrame at the same time? the image i used in underlying.java is just a small rectangle. the image in DnD however is the background.
    First off, you must add components one at a time in your container, and the tutorials will explain this to you. One way to overlap JPanels by adding them to a JLayeredPane, another is by adding them to a container that uses null layout, and for both you'd need to specify the size and position of the components. I favor the former since JLayeredPanes have nice abilities for moving components about in the different layers.

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

    Default

    Edit: if all you want is to have a background image for your GUI, then ignore all of this as there are much simpler ways of doing this. Google JPanel background image or search this forum for many examples of this.

  8. #8
    Dean29126 is offline Member
    Join Date
    Sep 2010
    Posts
    18
    Rep Power
    0

    Default

    This will never work when trying to code a complex library such as Swing. You need to study the tutorials and the API to see how things are done, not guess.
    I felt quite restricted as well, my college project limits me 1 month to create an interactive platform with java. In addition, they have not given really little information on java GUI. My teammates and I have been doing our own research all these while. My bad for not learning the fundamentals right. However, it is interesting for me and i hope in the near future i could really get them right.

    First off, you must add components one at a time in your container, and the tutorials will explain this to you. One way to overlap JPanels by adding them to a JLayeredPane, another is by adding them to a container that uses null layout, and for both you'd need to specify the size and position of the components. I favor the former since JLayeredPanes have nice abilities for moving components about in the different layers.
    Thanks for advice, i'll work my way around

  9. #9
    Dean29126 is offline Member
    Join Date
    Sep 2010
    Posts
    18
    Rep Power
    0

    Default

    In addition, they have not given really little information on java GUI.
    ignore the "not"

  10. #10
    Dean29126 is offline Member
    Join Date
    Sep 2010
    Posts
    18
    Rep Power
    0

    Default

    Fubarable are you there? i need some help

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

    Default

    Just ask your question whether I'm here or not. But if it is a new question, you may wish to start a new thread.

  12. #12
    Dean29126 is offline Member
    Join Date
    Sep 2010
    Posts
    18
    Rep Power
    0

Similar Threads

  1. need help with jframes
    By glomic in forum New To Java
    Replies: 13
    Last Post: 01-08-2011, 11:16 AM
  2. JFrames and JPanels with ActionListeners
    By atomant in forum New To Java
    Replies: 3
    Last Post: 11-30-2010, 07:26 PM
  3. Problems regarding JPanels in JPanels
    By ColtonPhillips in forum AWT / Swing
    Replies: 2
    Last Post: 07-19-2010, 08:33 PM
  4. two JFrames
    By kirtesh4u in forum New To Java
    Replies: 0
    Last Post: 11-17-2008, 08:26 PM
  5. Adding JPanels to JFrames based on x-y co-ordinates
    By aneesahamedaa in forum AWT / Swing
    Replies: 4
    Last Post: 07-24-2008, 10:41 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
  •