Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-13-2007, 10:55 PM
Member
 
Join Date: Jul 2007
Posts: 6
Falcon1 is on a distinguished road
Help with custom component
Hi,
I am designing a card game application. The cards are a custom component subclassing JComponent.I would like info on how to place the cards on a JFrame(one next to another) and also how to set their size. What code is needed to be put in my class to achieve the above things?
Currently, I am using the add() method to add the cards to the frame but every card is placed on top of the others, despite the fact that I use the setBounds() function for each card.
Can you help or provide any tutorial about this?
Thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-13-2007, 11:10 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
Add a JPanel to your JFrame. Set layout manager of your panel to null so that you can place your cards and resize them easily inside this panel.

Then add your cards to this panel. setBounds() method should work now.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-13-2007, 11:34 PM
Member
 
Join Date: Jul 2007
Posts: 6
Falcon1 is on a distinguished road
Thanks for your reply.
I tried what you suggested, but with a call to setBounds at the constructor of the component nothing changes.Also, the component stretches over the whole window area. Should I place the call to setBounds somewhere else?
Thanks
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-13-2007, 11:38 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
Quote:
Should I place the call to setBounds somewhere else?
Yes i think so. You should call it after it is placed to the panel! Obviously, constructor is not called after it is placed into panel.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-14-2007, 12:08 AM
Member
 
Join Date: Jul 2007
Posts: 6
Falcon1 is on a distinguished road
omg you are right
Thanks!
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-14-2007, 12:56 AM
Member
 
Join Date: Jul 2007
Posts: 6
Falcon1 is on a distinguished road
Ermmm, an other question just arised
When I create more than one card objects, only the first one is drawn.
Each card should be drawn by the code in the paintComponent() function of each instance.
However, while the code in the function is executed for every object (I checked it), the graphics are not drawn.
Any help?
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-14-2007, 02:00 AM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
Add your code here inside [code] tag and let us see/try it.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 07-14-2007, 02:27 AM
Member
 
Join Date: Jul 2007
Posts: 6
Falcon1 is on a distinguished road
Here you go:

Code:
//Jmycard.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class JmyCard extends JComponent { private int x,y; private myCard card; private Image img,glow; private boolean mouseOver; public JmyCard(myCard card,int x,int y) { setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); this.card = card; setBounds(x,y,79,119); mouseOver=false; System.out.println("inside constructor..."); img = Toolkit.getDefaultToolkit().getImage(getImgName( this.card)); glow= Toolkit.getDefaultToolkit().getImage("glow.gif"); mouseHandling mh=new mouseHandling(); addMouseListener(mh); this.x=x; this.y=y; } // end constructor public String getImgName(myCard card) { return card.getColor()+"_"+card.getFace()+".gif"; } protected void paintComponent(Graphics g) { super.paintComponent(g); //setSize(79,119); //setBounds(x,y,79,119); if (mouseOver) { Graphics2D g2 = (Graphics2D)g; g2.drawImage(img,x+9,y+14,60,90,null); Composite c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f); g2.setComposite(c); //System.out.println("glowing drawing now..."+getBounds().width); g2.drawImage(glow,x,y,getWidth(),getHeight(),null); } else { g.drawOval(x,x,5,5); g.drawImage(img,x+9,y+14,60,90,Color.red,null); } System.out.println("drawing now..."+x); } private class mouseHandling implements MouseListener,MouseMotionListener { public void mouseEntered(MouseEvent event) { mouseOver=true; System.out.println("glow effect"+x); repaint(); } public void mouseExited(MouseEvent event) { mouseOver=false; System.out.println("no glow"); repaint(); } public void mouseClicked(MouseEvent event) { System.out.println("ads"); } public void mouseDragged(MouseEvent event) { } public void mouseMoved(MouseEvent event) { System.out.println(event.getX()); } public void mousePressed(MouseEvent event) { } public void mouseReleased(MouseEvent event) { } } // end mouseHandling } // end JmyCard
and the code for creating the two cards and adding them to the gui
Code:
jcard = new JmyCard(new myCard("blue","s"),3,3); jcard2 = new JmyCard(new myCard("red","0"),100,100); jpan.add(jcard); jpan.add(jcard2); add(jpan); jcard.setBounds(3,3,79,119); jcard2.setBounds(100,100,79,119);
jcard,jcard2 are JmyCard instances and jpan is a JPanel
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 07-21-2007, 02:39 PM
Member
 
Join Date: Jul 2007
Posts: 6
Falcon1 is on a distinguished road
Should I use an external draw function to draw all the cards at the same time?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom Avatar JavaBean Suggestions & Feedback 4 01-17-2008 02:28 AM
RCP Custom Look and Feel JavaForums Java Blogs 0 01-02-2008 08:12 PM
Custom tgs in JSP ravian New To Java 2 12-29-2007 07:05 PM
How to use <,>,== on custom classes Bojevnik Advanced Java 4 10-29-2007 07:00 PM
Same component on all JTabbedPane java_novice AWT / Swing 4 08-06-2007 11:09 AM


All times are GMT +3. The time now is 12:24 PM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org