Results 1 to 5 of 5
- 01-20-2011, 03:43 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 10
- Rep Power
- 0
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):
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 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; } }
so, what's my problem here?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); } }
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
-
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.
-
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.
- 01-20-2011, 05:02 PM #4
Member
- Join Date
- Jan 2011
- Posts
- 10
- Rep Power
- 0
hmmm that's new for me. thank you for that.
- 01-21-2011, 04:12 PM #5
Member
- Join Date
- Jan 2011
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
how to add 2 panels in a panel inside a frame
By clydedoris in forum New To JavaReplies: 4Last Post: 07-31-2010, 09:11 AM -
Adding panels into a panel
By kcakir in forum AWT / SwingReplies: 7Last Post: 12-07-2009, 05:19 PM -
problem in displaying text inside the item label - JFREECHART
By chittora in forum Java 2DReplies: 9Last Post: 07-21-2009, 02:41 AM -
Spliting the Panel to three panels
By suraw in forum New To JavaReplies: 0Last Post: 03-25-2009, 06:05 PM -
How to add more components to main panel
By aneesahamedaa in forum AWT / SwingReplies: 4Last Post: 08-21-2008, 06:31 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks