One of my friends asked me to program a card game that he invented.
What drawing strategy do you recommend I use?
9 replies to this topic
#1
Posted 05 February 2011 - 09:09 AM
|
|
|
#2
Posted 05 February 2011 - 10:01 AM
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?
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
Posted 05 February 2011 - 10:06 AM
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):
I just call MainCanvas.drawAll() when something is changed and I need to redraw cards, etc etc..
What do you say?
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
Posted 05 February 2011 - 10:26 AM
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.
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.
#5
Posted 05 February 2011 - 10:29 AM
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).
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
Posted 05 February 2011 - 11:16 AM
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:
I didn't draw a card, certainly. But I did draw a circle. :)
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
Posted 05 February 2011 - 06:19 PM
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
Posted 06 February 2011 - 06:43 AM
How does paintComponent() work? Can I just call it to draw the next frame?
#9
Posted 06 February 2011 - 10:02 AM
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.
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
Posted 06 February 2011 - 10:05 AM
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


Sign In
Create Account


Back to top









