|
|
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.
|
|

07-13-2007, 10:55 PM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 6
|
|
|
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, 11:10 PM
|
 |
Moderator
|
|
Join Date: May 2007
Posts: 1,272
|
|
|
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, 11:34 PM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 6
|
|
|
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, 11:38 PM
|
 |
Moderator
|
|
Join Date: May 2007
Posts: 1,272
|
|
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. 
|
|

07-14-2007, 12:08 AM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 6
|
|
omg you are right 
Thanks!
|
|

07-14-2007, 12:56 AM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 6
|
|
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, 02:00 AM
|
 |
Moderator
|
|
Join Date: May 2007
Posts: 1,272
|
|
|
Add your code here inside [code] tag and let us see/try it.
|
|

07-14-2007, 02:27 AM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 6
|
|
Here you go:
//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
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
|
|

07-21-2007, 02:39 PM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 6
|
|
|
Should I use an external draw function to draw all the cards at the same time?
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|