Results 1 to 5 of 5
  1. #1
    Sneaky Fox is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default Displaying panels inside a main panel

    i'm having trouble displaying panels that are components to another panel (which is the main panel i.e. the contentPane of the main frame).

    here's some code (just part of it):
    Java Code:
    public class MainFrame extends JFrame implements MouseListener{
    
    	private MainPanel _mainPanel=new MainPanel();
    	private ArrayList<CardPanel> _panels=new ArrayList<CardPanel>();
    	private ArrayList<CardPanel> _trick=new ArrayList<CardPanel>();
    	
    	public MainFrame(String s) throws InterruptedException{
    		super(s);
    		setSize(1366,768);
    		setLocationRelativeTo(null);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		for(int i=0;i<13;++i){
    			_panels.add(new CardPanel());
    		}
    		for(int i=0;i<4;++i){
    			_trick.add(new CardPanel());
    		}
    		for(int i=0;i<4;++i){
    			CardPanel panel=_trick.get(i);
    			panel.addMouseListener(this);
    		}
    		_trick.get(0).setBounds(647,454,72,96);
    		_trick.get(1).setBounds(557,340,72,96);
    		_trick.get(2).setBounds(647,226,72,96);
    		_trick.get(3).setBounds(737,340,72,96);
    		for(int i=0;i<13;++i){
    			CardPanel panel=_panels.get(i);
    			panel.addMouseListener(this);
    			panel.setBounds(203+i*74,600,72,96);
    		}
    		for(CardPanel panel:_panels){
    			_mainPanel.add(panel);
    		}
    		for(CardPanel panel:_trick){
    			_mainPanel.add(panel);
    		}
    		setContentPane(_mainPanel);
    		setVisible(false);
    		setVisible(true);
    		gameCycle();
    	}
    	
    	public void gameCycle() throws InterruptedException{
    		while(_playerName.equals("Type your name and press Enter")){
    			_playerName=_mainPanel.getName();
    			Thread.currentThread().sleep(1000);
    		}
    		_arbiter=new Arbiter(_playerName,"BOT1","BOT2","BOT3");
    		for(int i=0;i<6;++i){
    			_arbiter.newGame();
    			printGame();
    			printHand();
    			for(int j=0;j<13;++j){
    				play();
    				_arbiter.newTrick();
    				printHand();
    			}
    		}
    	}
    	
    	private void printGame(){
    		String game=_arbiter.getRuler().getState().getClass().getName();
    		game=game.replaceAll("core.","");
    		_mainPanel.setGame(game);
    	}
    	
    	private void printHand(){
    		Iterator<Card> cards=_arbiter.getPlayer(0).getHand().getIterator();
    		int i=0;
    		while(cards.hasNext()){
    			Card c=cards.next();
    			int id=c.getId();
    			_panels.get(i).setCard(id);
    			++i;
    		}
    	}
    the program does its thing until the part where it calls printHand (in the gameCycle method). the problem is that when it does (in the printHand method) _panels.get(i).setCard(id) it will set the card in the CardPanel acording to the method setCard in the CardPanel class:
    Java Code:
    public class CardPanel extends JPanel{
    
    	private Image _image=null;
    	private PathNameGenerator _pathGen=new PathNameGenerator();
    	private Toolkit _toolkit=Toolkit.getDefaultToolkit();
    	private int _id=-1;
    	
    	public CardPanel(){
    		setLayout(null);
    		setSize(72,96);
    		setOpaque(true);
    	}
    	
    	public void setCard(int id){
    		_image=_toolkit.getImage(_pathGen.getPathName(id));
    		_id=id;
    		repaint();
    	}
    	
    	public void removeCard(){
    		_id=-1;
    		_image=null;
    		repaint();
    	}
    	
    	public int getId(){
    		return _id;
    	}
    	
    	public void paintComponent(Graphics g){
    		g.drawImage(_image,0,0,null);
    	}
    	
    }
    so, what's my problem here?
    the CardPanels won't repaint, or at least they do not show up when I run the program.
    so i'm guessing that what i'm doing wrong is the part where you force your "subpanels" to repaint. How can you do that? Or if you think the problem is another one please help me find it out.
    thanks in advance

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

    Default

    Again, it's tough to see what your program is doing, but one problem I see is that your JPanel's paintComponent method doesn't call the super method on its first line.

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

    Default

    Let me also add, that an easier way to swap images is to display them as ImageIcons in a JLabel (which can be held in a JPanel if needed) and then change images by simply setting the JLabel's icon to a new one via its setIcon method.

  4. #4
    Sneaky Fox is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default

    hmmm that's new for me. thank you for that.

  5. #5
    Sneaky Fox is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default

    problem solved by calling the super.paintComponent method and by putting this as the 4th argument of drawImage method.
    thx for helping

Similar Threads

  1. how to add 2 panels in a panel inside a frame
    By clydedoris in forum New To Java
    Replies: 4
    Last Post: 07-31-2010, 09:11 AM
  2. Adding panels into a panel
    By kcakir in forum AWT / Swing
    Replies: 7
    Last Post: 12-07-2009, 05:19 PM
  3. Replies: 9
    Last Post: 07-21-2009, 02:41 AM
  4. Spliting the Panel to three panels
    By suraw in forum New To Java
    Replies: 0
    Last Post: 03-25-2009, 06:05 PM
  5. How to add more components to main panel
    By aneesahamedaa in forum AWT / Swing
    Replies: 4
    Last Post: 08-21-2008, 06:31 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
  •