Jump to content

Card Game

- - - - -

  • Please log in to reply
9 replies to this topic

#1
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
One of my friends asked me to program a card game that he invented.
What drawing strategy do you recommend I use?

#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
What is the card game? Does it use a standard deck of cards, or does it require it's own specialized playing cards?

What do you mean by "drawing strategy"? Are you intending on using a Canvas to draw a specialized field on, or are you going to use something like JOGL to render the actual playfield?
Wow I changed my sig!

#3
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
The cards are non conventional and I'm using a canvas.

I'm doing something like this and it seems to be a good choice (I've just started programming this thing now):

public class GFrame extends JFrame {

	private static final long serialVersionUID = 1L;

	

	private MainCanvas canvas;

	

	public GFrame(){

		super(GameInfo.name()+" - " + GameInfo.version());

		

		...

                ...		

		canvas=new MainCanvas(); //create drawing canvas

		canvas.setSize(StaticVars.initialWidth, StaticVars.initialHeight);

		canvas.setBackground(Color.GREEN);

		add(canvas); //add it to frame and show

		...

                ...

	}

	

	public static void main(String arg[]){

		new GFrame();

	}


}

public class MainCanvas extends Canvas implements MouseListener, MouseMotionListener {

	private static final long serialVersionUID = 1L;

	

	private BufferStrategy strategy;

	

	public MainCanvas(){

		addMouseListener(this);

		addMouseMotionListener(this);

	}

	

	public void drawAll(){

		System.out.println("Disegno frame");

		Graphics2D g=(Graphics2D) strategy.getDrawGraphics();

		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

		

		g.setColor(Color.GREEN);

		g.fillRect(0, 0, getHeight(), getWidth());

		

            //draw all things things here

	    

	    g.dispose();

	    strategy.show();

	}


	public void addNotify(){

		super.addNotify();

		

		createBufferStrategy(2);

	        strategy = getBufferStrategy();

	}

	...

        ...


}

I just call MainCanvas.drawAll() when something is changed and I need to redraw cards, etc etc..

What do you say?

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Dependant on how you want to draw cards you could do it in various ways. Like the left example in the image, it may be easier to just put JPanels with gridLayouts there.
Then all you need to do is add images to the grid and they will position themselves correctly.
The example on the right will require more "manually" drawing the cards using the Graphics class.
Posted Image

#5
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
No no no no, I want to use the Graphics class. I'm drawing all by hand, doing all the calculations myself.
Even the "buttons", I want to do it all myself, handling all the events (for example, change the icon for a button when the mouse passes over it).

#6
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
I'd recommend just overriding the paintComponent() method, personally. Maybe not even use a Canvas and just opt for a standard JComponent or JPanel and paint on that. I had no trouble setting up a painting JPanel:

import java.awt.Graphics;

import java.awt.Color;

import java.awt.Dimension;


import java.awt.event.MouseMotionListener;

import java.awt.event.MouseEvent;


import javax.swing.BoxLayout;

import javax.swing.JFrame;

import javax.swing.JPanel;


public class PaintingTest extends JPanel

{

    public static void main(String[] args)

    {

        JFrame frame = new JFrame();

        frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.LINE_AXIS));

        frame.add(new PaintingTest());

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setVisible(true);

    }


    private class MML implements MouseMotionListener

    {

        public void mouseDragged(MouseEvent e)

        {

            registerLocation(e);

        }


        public void mouseMoved(MouseEvent e)

        {

            registerLocation(e);

        }


        private void registerLocation(MouseEvent e)

        {

            mouseXLocation = e.getX();

            mouseYLocation = e.getY();

            repaint();

        }

    }


    public PaintingTest()

    {

        mouseXLocation = 0;

        mouseYLocation = 0;

        addMouseMotionListener(new MML());

        setPreferredSize(new Dimension(1000000, 1000000));

    }


    protected void paintComponent(Graphics g)

    {

        // Always start by painting the parent.

        super.paintComponent(g);


        // Let's just paint a green dot around the MouseLocation.

        Color oldColor = g.getColor();


        g.setColor(dotColor);

        g.fillOval(mouseXLocation - scaleSize, mouseYLocation - scaleSize,

                   scaleSize * 2,             scaleSize * 2);

        g.setColor(oldColor);

    }


    private final Color dotColor = new Color(0.0f, 1.0f, 0.0f, 0.5f);

    private final int scaleSize = 50;


    private int mouseXLocation;

    private int mouseYLocation;

}

I didn't draw a card, certainly. But I did draw a circle. :)
Wow I changed my sig!

#7
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
I second the opinion of extending JPanel. Any custom component you would like to use can be made simple by extending what the java swing has provided.

#8
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
How does paintComponent() work? Can I just call it to draw the next frame?

#9
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
paintComponent() is called automatically by the paint() method on Swing components, so you're no longer supposed to override paint() (like with AWT components), and instead override paintComponent. This makes it much easier for implementors to perform painting since now you don't have to worry about painting the child widgets (that's done by paintChildren), nor painting the border (done by paintBorder). Generally speaking there is no reason to override paintChildren or paintBorder.

Also, don't invoke paint() directly! Instead, you should call repaint() in your program code to tell the Event Dispatch Thread that it needs to paint the display since Swing is not thread safe. You can learn more about painting in Painting in AWT and Swing. It can seem a bit complicated (and in some ways it is), but it's done to create the most efficient painting system they could design using AWT while maintaining the flexibility of the developer to have essentially complete freedom when writing their GUI. What this means is you should not call paintComponent() directly, Swing will call it for you, just call repaint() in a Listener and the paint event will occur.
Wow I changed my sig!

#10
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
I'm already using a Canvas for this game, but I'll try to do as you say in my next project, thanks.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users