Making a card game

Hi! i’m doing a subject at university that is about java application development and i’d like to make a card game (something like solitaire) but i don’t know what api use?

Can someone suggest me one? (im using the netbeans ide and i dont need to have an excelent performance and the game will not be graphics intensitive, its a card game)

just use java2d, it’s more than enough. I assume you will go for swing and passive rendering.

Thank you, i have to download anything else that the jdk 1.6 with netbeans to use that?

and… what is passive rendering?

everything you need is JRE already
well… search forums and find your answers, there are plenty of discussions about active / passive rendering

He’s just making a cardgame… he’s got way more important stuff to worry about than active/passive rendering.

panel.repaint() with an overriden paintComponent(Graphics) is more than adequate.

any suggestion on what should i use to make the cards appear in the jFrame? i was thinking about a panel but i’ve started to think it isnt a good idea…

well… that is passive rendering and I’m sure he would found out very fast as there isn’t much complication about passive or active rendering. Passive is almost always painting on swing EDT.

no suggestions?

I don’t quite understand what you mean by “make cards appear”, but for drawing JPanel is fine… draw in it’s paintComponent()

I’ve discovered that i have a program to make an online chess game but i still cannot make a card appear in the playfield :frowning: i need help.

From now i want to make appear a card in a JFrame when a JButton is pressed, looking to an example on this forums i decided to make an especial JPanel class named CartaGrafica (GraphicCard self explanatory ::))

Here i attach the code, someone help me?

The class code


public class CartaGrafica extends JPanel{
    
    /** Creates a new instance of CartaGrafica */
    public CartaGrafica(int x, int y) {
        super();
        this.setBounds(x,y,100,139);
    }
    public void pintaCarta(Graphics g)
    {
        super.paintComponent(g);
        g.drawImage(Toolkit.getDefaultToolkit().getImage("C:\\carta.bmp"),0,0,100,139,null);
    }
}

code of the event


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
        try
            {
                cg1.pintaCarta(ImageIO.read(new File("C:\\carta.jpg")).getGraphics()); //cg1 is an instance of CartaGrafica
            }
            catch (IOException e)
            {
                System.out.println("Excepcion al intentar leer la carta de archivo");
            }
}

let’s do some debuging…
first, does your jButton1ActionPerformed(java.awt.event.ActionEvent evt) gets called? Put a println somewhere so you can determine that. When using swing, when pressing a JButton, it’s actionPerformed() gets called, but then maybe you call your method from it, we can’t tell.

second, if you do manage to succeed, every time card is painted program would reload image from the disk - very slow and intensive for the disk
what you need to do is preload an image and use same image every time, like:


// in card class constructor:
private BufferedImage card = ImageIO.read(...)

// in pintaCarta(Graphics g)
g.drawImage(card, ....)

third, you don’t even need JPanel for card… you can just load an image of a card into BufferedImage and use Graphics2D.drawImage(image, x,y,w,h,null). This is what I said in second section, just amplified on methodology.

forth, if you want to use swing components for displaying your cards, you need to add them to container, override it’s paintComponent() for drawing, and use setVisible(true) if needed. What you are doing now isn’t related to swing, you just store your image data in a object that is extending JPanel, but you don’t actually use JPanel functionality anywhere (or maybe you do add it somewhere, but then it won’t ever get painted with your code, only as JPanel, because you don’t override it’s paintComponent() ).

Hope it’s not much confusing. Good luck.

I’ve been trying those things you’ve said to me with no success, maybe i’ve forgotten to say that the event is in a class named MesaGrafica that is a JFrame, does this change the things? :S

JFrame created automatically by the NetBeans IDE, not by me.

I believe that i found a serious question to ask: What graphics Class must i pass as parameter of the paintComponents method of the JPanel?

I would suggest you read AWT/Swing painting tutorial, it’s somewhere on official java domain.

you still didn’t answered does it even gets called. Test that first.
It doesn’t matter if event handle code is in JFrame, as long as that class that is in implements ActionHandler(?). Actually, for start, you can just create new handler class where you add it, here is what I mean. I forgot exact names of classes and methodes, but it’s something close to this:


some_button.addActionHandler(new ActionHandler() {
    public void actionPerformed(ActionEvent evt) {
        my_frame.add(card);
        card.setVisible(true);
        my_frame.repaint();
    });
....
// if you have written action methodes (implemented them) in same class:
some_button.addActionHandler(this);

Sounds like you don’t have any control over it ;D It’s all text code, you can do whatever you want.

not paintComponentS(), just paintComponent()… and you pass Graphics. For your drawing, you can convert it to Graphics2D later. You should really read that tutorial I mentioned.

the tutorial with the example is usefull ^^

The event is in fact called, but it does nothing that i want '^^

i’ll take a loot at that actionhandler

the IDE automatically blocks some code, it’s true that you can edit the code directly with the notepad but i don’t use those workarounds, yet :-[

i know i know, its paintComponent(Graphics g)

it doesn’t let you edit it? jeez… I’m using eclipse, only once used netbeans but didn’t noticed that.

hehe, I wanted to make some work and remember a few things, here is a very simple example:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class CardGameSwingExample extends JFrame {
    Card card;
    
    public static void main(String[] args) {
        new CardGameSwingExample();
    }
    
    public CardGameSwingExample() {
        super("Card Show Example");
        this.setBounds(200,200,400,300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null); 
        
        JButton button = new JButton("SHOW card");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JButton temp = (JButton)e.getSource();
                if (temp.getText().equals("SHOW card")) {
                    temp.setText("HIDE card");
                    card.setVisible(true);
                } else {
                    temp.setText("SHOW card");
                    card.setVisible(false);
                }
                
            }
        });
        button.setBounds(this.getWidth()/2-50, 10, 100, 20);
        this.add(button);
        
        card = new Card();
        card.setLocation(this.getWidth()/2-card.getWidth()/2, this.getHeight()/2-card.getHeight()/2);
        
        this.add(card, BorderLayout.CENTER);
        
        this.validate();
        this.setVisible(true);
    }

}

class Card extends JPanel {
    public Card() {
        super();
        this.setLayout(new BorderLayout());
        this.setBackground(Color.GREEN);
        this.setSize(50, 100);
        JLabel queen = new JLabel("QUEEN");
        this.add(queen, BorderLayout.CENTER);
        this.setVisible(false);
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D)g;
        g2d.drawRect(0, 0, 50, 100);
        g2d.fillOval(3, 3, 10, 10);
        g2d.fillOval(this.getWidth()-13, 3, 10, 10);
        g2d.fillOval(3, this.getHeight()-13, 10, 10);
        g2d.fillOval(this.getWidth()-13, this.getHeight()-13, 10, 10);
    }
}
    

EDIT: oh yeah, null layout (no layout) is generally bad, for big projects it’s better to use some layout

finally yesterday i managed to print the card in a jpanel, thanks to the example in the tutorial you recomendend, thank you!, now i will review your code.

Summering up, that does the layout class do?

I have a new problem, when rotating a image 90 degrees it gets cut some pixels.

The rotated image has some of the upper and lower parts of the original image missing, does anyone know why it happens?

((Graphics2D)g).setTransform(java.awt.geom.AffineTransform.getRotateInstance(Math.toRadians(90),this.getWidth()/2,this.getHeight()/2));
g.drawImage((Image)ImageIO.read(new File("C:\\" + rutaImagen.toString() + ".jpg")),(int)this.getAlignmentX(),(int)this.getAlignmentY(),this.getWidth(),this.getHeight(),null);

I’ve been tweaking with the code and it seems that the component holding the image does not draw the full image or the component is taller than wide when the image is wider than tall

I’ll go to sleep, it’s too late now :-\

so, make the component the right size
for start make it very large so you can confirm that is the problem, than (if that is the problem) write a code that will make it exact size as needed for picture loaded

there is any way to prevent the components to automatically anchor?