Results 1 to 9 of 9
Thread: Help with custom component
- 07-13-2007, 08:55 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 6
- Rep Power
- 0
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
- 07-13-2007, 09:10 PM #2
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.
- 07-13-2007, 09:34 PM #3
Member
- Join Date
- Jul 2007
- Posts
- 6
- Rep Power
- 0
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
- 07-13-2007, 09:38 PM #4
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. ;)Should I place the call to setBounds somewhere else?
- 07-13-2007, 10:08 PM #5
Member
- Join Date
- Jul 2007
- Posts
- 6
- Rep Power
- 0
omg you are right :)
Thanks!
- 07-13-2007, 10:56 PM #6
Member
- Join Date
- Jul 2007
- Posts
- 6
- Rep Power
- 0
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?
- 07-14-2007, 12:00 AM #7
Add your code here inside [code] tag and let us see/try it.
- 07-14-2007, 12:27 AM #8
Member
- Join Date
- Jul 2007
- Posts
- 6
- Rep Power
- 0
Here you go:
and the code for creating the two cards and adding them to the guiJava 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
jcard,jcard2 are JmyCard instances and jpan is a JPanelJava 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);
- 07-21-2007, 12:39 PM #9
Member
- Join Date
- Jul 2007
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
Custom Avatar
By JavaBean in forum Suggestions & FeedbackReplies: 4Last Post: 01-17-2008, 12:28 AM -
Custom tgs in JSP
By ravian in forum New To JavaReplies: 2Last Post: 12-29-2007, 05:05 PM -
How to use <,>,== on custom classes
By Bojevnik in forum Advanced JavaReplies: 4Last Post: 10-29-2007, 05:00 PM -
Same component on all JTabbedPane
By java_novice in forum AWT / SwingReplies: 4Last Post: 08-06-2007, 09:09 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks