Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (577)
games submitted by our members
Games in WIP (498)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  Making a card game  (Read 3979 times)
0 Members and 1 Guest are viewing this topic.
Offline avi58

Senior Newbie





« Posted 2007-05-27 16:26:44 »

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)
Offline Kova

Senior Member





« Reply #1 - Posted 2007-05-27 18:06:45 »

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

Senior Newbie





« Reply #2 - Posted 2007-05-27 18:39:59 »

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?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Kova

Senior Member





« Reply #3 - Posted 2007-05-27 23:56:15 »

everything you need is JRE already
well... search forums and find your answers, there are plenty of discussions about active / passive rendering
Online Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2007-05-28 00:04:51 »

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.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline avi58

Senior Newbie





« Reply #5 - Posted 2007-05-29 21:37:22 »

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

Senior Member





« Reply #6 - Posted 2007-05-29 21:59:32 »

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

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

Senior Newbie





« Reply #7 - Posted 2007-05-30 17:47:34 »

no suggestions?
Offline Kova

Senior Member





« Reply #8 - Posted 2007-05-30 19:10:43 »

I don't quite understand what you mean by "make cards appear", but for drawing JPanel is fine... draw in it's paintComponent()
Offline avi58

Senior Newbie





« Reply #9 - Posted 2007-06-01 12:39:55 »

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 Sad 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  Roll Eyes)

Here i attach the code, someone help me?

The class code
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
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");
            }
}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Kova

Senior Member





« Reply #10 - Posted 2007-06-01 15:41:34 »

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:
1  
2  
3  
4  
5  
// 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.
Offline avi58

Senior Newbie





« Reply #11 - Posted 2007-06-01 20:37:30 »

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?
Offline Kova

Senior Member





« Reply #12 - Posted 2007-06-01 22:27:18 »

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

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

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
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);


JFrame created automatically by the NetBeans IDE, not by me.
Sounds like you don't have any control over it  Grin It's all text code, you can do whatever you want.

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?
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.
Offline avi58

Senior Newbie





« Reply #13 - Posted 2007-06-01 23:08:54 »

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  Embarrassed

i know i know, its paintComponent(Graphics g)
Offline Kova

Senior Member





« Reply #14 - Posted 2007-06-01 23:14:55 »

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  Embarrassed

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

Senior Member





« Reply #15 - Posted 2007-06-02 00:17:33 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
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
Offline avi58

Senior Newbie





« Reply #16 - Posted 2007-06-02 22:58:48 »

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?
Offline avi58

Senior Newbie





« Reply #17 - Posted 2007-06-03 00:09:28 »

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?

1  
2  
((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  Undecided
Offline Kova

Senior Member





« Reply #18 - Posted 2007-06-03 00:39:09 »

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

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
Offline avi58

Senior Newbie





« Reply #19 - Posted 2007-06-03 18:25:26 »

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

Senior Member





« Reply #20 - Posted 2007-06-03 18:34:46 »

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

what is anchoring?
Offline avi58

Senior Newbie





« Reply #21 - Posted 2007-06-03 21:12:43 »

what is anchoring?

When designing the frame, you put components and the IDE automatically puts them in a precise position, seems that if you move the component in execution time (like with 4 buttons editing their bounds) the component seems to move yes, but it usually automatically returns to it's starting position...

The solution for this, is to write yourself the code of the JFrame Cheesy

Btw... what is a layout?
Offline Kova

Senior Member





« Reply #22 - Posted 2007-06-04 13:39:59 »

still don't know what is anchoring... when you try to move/resize something it goes back like you did nothing?

layout? ...this is all basic of swing, read a few swing tutorials and always have JavaDoc accessible, I'm not gonna teach you basics every time you need something. And just to answer this one: layout is code (object) that controls how components are rendered (shown) in some container. Swing components have BorderLayout by default, that layout has sides (north, south, west, east) and center, so when you add components you specify where you add them.
.add(button, BorderLayout.CENTER); will add button to center of container
Offline avi58

Senior Newbie





« Reply #23 - Posted 2007-06-04 20:32:22 »

thank you kova

EDIT: i believe that the problem of anchoring is because the layout says what position must the components be in. I think i'll make my game without a layout until i've "played" more with layouts
Offline Coinerson

Junior Member




Introducing the world's cutest zombie, Timmy


« Reply #24 - Posted 2007-06-05 04:39:01 »

I thought it was FlowLayout by default... not that it matters. At all, just looking for an excuse to post something.
Offline avi58

Senior Newbie





« Reply #25 - Posted 2007-06-05 13:53:20 »

to put the cards in the field i won't use a layout but for the other components i'll use a layout, because the cards will have a more or less arbitrary position and no layout can help me. Anyways, the user wont be able to resize the main frame....
Offline avi58

Senior Newbie





« Reply #26 - Posted 2007-06-05 20:43:45 »

Well, after some tries, i will tell what i've got:

I've got a frame of 800x600 which it should show, a JPanel that shows an image of about 600x600, in the right side of the screen i have 3 textpanels that show different information, until here everything is okay.

The 3 textpanels are organized in different layouts but they show everything i want like i want. The problem comes with the main image.

Over this image, there should appear 16 different pannels on a distribution like:

http://img236.imageshack.us/img236/2096/cardlayoutta0.jpg

The problem is: if i use no layout (JFrame.setLayout(null)) then only the components with layout will be shown (the info on the right side of the screen and with their right size) but no cards and no background image is shown.

If, in the other hand, i use the default layout (a box one?) then a big black square appears instead of the background (this is, i believe, because when the cards are init they look like black rectangles).

Can someone help me in choosing the correct layout? if absolute posicioning must be used, can someone help me use it? because i've done what java tutorials say but with no success
Offline Kova

Senior Member





« Reply #27 - Posted 2007-06-06 01:05:21 »

every container (JFrame, JPanel,...) has a layout. You don't need to set layout of JFrame to null, and please don't. Null layout (FlowLayout, are these the same?) is used for absolute positioning. Set it for JPanel you draw into. Your components will only display when you set their bounds (size and location). I've done that in my example code I've posted earlier.

Again to remind you... you could always forget stupid swing and just use images and draw. Much simpler and faster to do.

Edit: maybe it's FlowLayout by default for swing components and not BorderLayout like I said... dunno, but I'm sticking to my original thought Wink
Offline avi58

Senior Newbie





« Reply #28 - Posted 2007-06-06 20:59:31 »

My point is, i think that the best solution is put my cards with absolutie posicioning, the user won't be able to resize the window anyway...

But i dont understand why the cards aren't shown..

Edit: now no big black rectangle is shown...

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
public MesaGrafica() {
        //Note: X offset = 4, Y offset = 30
       super("Mus");
        fondomesa = new Fondo();
        ssalida = new JScrollPane();
        salida = new JTextPane();
        puntuaciones = new JPanel();
        puntuacion1 = new JTextPane();
        puntuacion2 = new JTextPane();
        infoderecha = new JPanel();
        this.setLayout(null);  //For the absolute posicioning
//The next lines are to create the infomation panels that are shown correctly (there should be no problem from here
       SimpleAttributeSet centrado = new SimpleAttributeSet();
        StyleConstants.setAlignment(centrado,StyleConstants.ALIGN_CENTER);
        puntuacion1.setParagraphAttributes(centrado,true);
        puntuacion2.setParagraphAttributes(centrado,true);
        puntuaciones.setLayout(new BoxLayout(puntuaciones,BoxLayout.X_AXIS));
        puntuaciones.add(puntuacion1);
        puntuaciones.add(puntuacion2);
        infoderecha.setLayout(new BoxLayout(infoderecha,BoxLayout.Y_AXIS));
        ssalida.setPreferredSize(new Dimension(196,266));
        puntuaciones.setPreferredSize(new Dimension(192,260));
        JLabel estado = new JLabel("Estado");
        estado.setForeground(new Color(0,0,255));
        estado.setAlignmentX(Component.CENTER_ALIGNMENT);
        infoderecha.add(puntuaciones);
        infoderecha.add(Box.createRigidArea(new Dimension(0,20)));
        infoderecha.add(estado);
        infoderecha.add(ssalida);
        ssalida.setViewportView(salida);
        this.setBounds((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth()/2 - 400,(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight()/2 - 300,800,600);
//to here
       cartasEnMesa = new CartaGrafica[4][4];
        cartasEnMesa[0][0] = new CartaGrafica(156,428,57,85);
        cartasEnMesa[0][1] = new CartaGrafica(233,428,57,85);
        //(...)
       for (int i=0;i<4;i++)
        {
            for (int j=0;j<4;j++)
            {
                cartasEnMesa[i][j].setLayout(null);
                this.add(cartasEnMesa[i][j]);
            }
        }
        this.add(infoderecha);
        this.add(fondomesa);
        infoderecha.setBounds(600,0,192,566);
        puntuacion1.setBorder(BorderFactory.createLineBorder(new Color(0,0,0),1));
        puntuacion2.setBorder(BorderFactory.createLineBorder(new Color(0,0,0),1));
        puntuacion1.setEditable(false);
        puntuacion2.setEditable(false);
        this.actualizarPuntuaciones(null);
        salida.setEditable(false);
        salida.setFont(new Font("Arial",Font.PLAIN,10));
        this.setVisible(true);
        this.setResizable(false);
        fondomesa.setVisible(true);
    }

CartaGrafica "code" the important thing is the paint method, the gets and sets are omitted here...
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
public class CartaGrafica extends javax.swing.JPanel {
   
    /** Creates a new instance of CartaGrafica */
    private StringBuffer rutaImagen;
    private int jugador;
    private boolean tapada;
    public CartaGrafica() {  
    }
   
    public CartaGrafica(int x, int y, int width, int height)
    {
        super();
        this.setBounds(x,y,width,height);
    }
    public void paint(Graphics g)
    {
        if (rutaImagen == null)
        {
            g.setColor(new Color(0,0,0));
            g.fillRect((int)this.getBounds().getX(),(int)this.getBounds().getY(),(int)this.getBounds().getWidth(),(int)this.getBounds().getHeight());
             return;
        }
        if (this.estaTapada())
        {
            g.setColor(new Color(0,0,255));
            g.fillRect((int)this.getBounds().getX(),(int)this.getBounds().getY(),(int)this.getBounds().getWidth(),(int)this.getBounds().getHeight());
        }
        else
        {
            try
            {
                //((Graphics2D)g).setTransform(java.awt.geom.AffineTransform.getQuadrantRotateInstance(1,this.getWidth()/2,this.getHeight()/2));
               g.drawImage((Image)ImageIO.read(new File("C:\\" + rutaImagen.toString() + ".jpg")),0,0,this.getWidth(),this.getHeight(),null);
            }
            catch(IOException e)
            {
                g.setColor(new Color(0,0,0));
                g.fillRect((int)this.getAlignmentX(),(int)this.getAlignmentY(),this.getWidth(),this.getHeight());
                System.out.println("Ha fallado al leer la imagencita");
            }
        }
    }
}


It could be nice to print directly into the frame but the image to print depends on the values of another class (a non graphical Card) and this way (with a JPanel) i think is easier of implement...
Offline Kova

Senior Member





« Reply #29 - Posted 2007-06-07 01:52:12 »

didn't you read the tutorials!? You need to override paintComponent(), not paint() in CartaGrafica. Ok, everything you basicly need is in that swing/awt painting tutorial. I won't help you much more, it's useless, you must try yourself so you can learn.
Pages: [1] 2
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (19 views)
2014-04-15 18:08:23

BurntPizza (15 views)
2014-04-15 03:46:01

UprightPath (28 views)
2014-04-14 17:39:50

UprightPath (13 views)
2014-04-14 17:35:47

Porlus (29 views)
2014-04-14 15:48:38

tom_mai78101 (54 views)
2014-04-10 04:04:31

BurntPizza (111 views)
2014-04-08 23:06:04

tom_mai78101 (212 views)
2014-04-05 13:34:39

trollwarrior1 (181 views)
2014-04-04 12:06:45

CJLetsGame (187 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!