Results 1 to 9 of 9
  1. #1
    Falcon1 is offline Member
    Join Date
    Jul 2007
    Posts
    6
    Rep Power
    0

    Default 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

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    10

    Default

    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.

  3. #3
    Falcon1 is offline Member
    Join Date
    Jul 2007
    Posts
    6
    Rep Power
    0

    Default

    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

  4. #4
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    10

    Default

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

  5. #5
    Falcon1 is offline Member
    Join Date
    Jul 2007
    Posts
    6
    Rep Power
    0

    Default

    omg you are right :)
    Thanks!

  6. #6
    Falcon1 is offline Member
    Join Date
    Jul 2007
    Posts
    6
    Rep Power
    0

    Default

    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?

  7. #7
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    10

    Default

    Add your code here inside [code] tag and let us see/try it.

  8. #8
    Falcon1 is offline Member
    Join Date
    Jul 2007
    Posts
    6
    Rep Power
    0

    Default

    Here you go:

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

  9. #9
    Falcon1 is offline Member
    Join Date
    Jul 2007
    Posts
    6
    Rep Power
    0

    Default

    Should I use an external draw function to draw all the cards at the same time?

Similar Threads

  1. Custom Avatar
    By JavaBean in forum Suggestions & Feedback
    Replies: 4
    Last Post: 01-17-2008, 12:28 AM
  2. Custom tgs in JSP
    By ravian in forum New To Java
    Replies: 2
    Last Post: 12-29-2007, 05:05 PM
  3. How to use <,>,== on custom classes
    By Bojevnik in forum Advanced Java
    Replies: 4
    Last Post: 10-29-2007, 05:00 PM
  4. Same component on all JTabbedPane
    By java_novice in forum AWT / Swing
    Replies: 4
    Last Post: 08-06-2007, 09:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •