2010-04-07 56 views
2

我对使用java创建纸牌游戏的方法有一些疑问。有关如何创建Java纸牌游戏的提示

在Swing中处理卡片的最佳方式是什么?我怎样才能拖动它们,将它们对准正确位置的最佳方法是什么?

+1

提示 - 如果你打你的卡,你会发现,有人已经为你实现了这一点:-) – 2010-04-07 07:42:46

+0

我从堆栈中绘制一张卡片,它是... CardLayout,omg有多么伟大=) – jonas 2010-04-07 08:01:12

+0

在Java中创建纸牌游戏的最佳方式可能是*不*尝试使所有内容摇摆。相反:尽可能少地使用Swing。一个JFrame,一个JLabel,它的ImageIcon是一个BufferedImage,你很好走。 Java游戏程序员通常不会**使用Swing:我了解Java 3D游戏,了解Java手机游戏,了解Java 2D游戏......但我不介绍Java Swing游戏;) – SyntaxT3rr0r 2010-04-07 08:14:00

回答

1

将他们对齐到 他们的正确位置的最佳方法是什么?

Overlap Layout可能会帮助你。

+0

服务器正在下降。所以我无法检查出来... – jonas 2010-04-08 11:00:11

1

什么是拖动多个图像的最佳方法?我已经使用Java2D将两个图像绘制到JPanel,但我只能拖动它们中的一个。我会附上我的源代码。我的解决方案的问题是我需要重新绘制整个窗口,即使我只是操纵一个元素。是否可以处理卡片的对象,而不是它们的图像?所以,当我移动一卡,我会移动对象的视觉呈现,而不是像(像现在这样)。

import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseMotionAdapter; 
import java.awt.image.BufferedImage; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class Java2d_MainPanel extends JPanel{ 
    private static BufferedImage img = new logic.GetBufferedImage().getImage(); 
    private JButton knapp = new JButton("Nytt bilde"); 
    private JButton knapp2 = new JButton("Nytt bilde2"); 
    private static BufferedImage img2 = new logic.GetBufferedImage().getImage2(); 
    //coordinates for image 1 
    private int x1 = 0; 
    private int y1 = 0; 
    //coordinates for image 2 
    private int x2 = 50; 
    private int y2 = 50; 

public Java2d_MainPanel(){ 
    add(knapp); 
    add(knapp2); 
    knapp.addActionListener(new ButtonHandler()); 
    knapp2.addActionListener(new ButtonHandler2()); 
    addMouseMotionListener(new MouseMotionHandler()); 
} 

public void newImage(ActionEvent e){ 
    if(e.getSource().equals(knapp)){ 
    img = new logic.GetBufferedImage().getImage(); 
    repaint(); 
    } 
    else if(e.getSource().equals(knapp2)){ 
    img2 = new logic.GetBufferedImage().getImage2(); 
    repaint(); 
    } 
} 
@Override 
public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    Graphics2D g2d = (Graphics2D) g; 
    newPaint(g2d); 
} 
public void newPaint(Graphics2D g2d){ 
    g2d.drawImage(img, x1, y1, null); 
    g2d.drawImage(img2, x2, y2,null); 
} 

public static void main(String[] args) { 

    JFrame frame = new JFrame("Rabbits"); 
    frame.add(new Java2d_MainPanel()); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(800, 640); 
    frame.setLocationRelativeTo(null); 
    frame.setVisible(true); 
} 
class MouseMotionHandler extends MouseMotionAdapter { 
    @Override 
public void mouseDragged(MouseEvent e) { 

    x1 = e.getX()-(img.getWidth()/2); 
    y1 = e.getY()-(img.getHeight()/2); 
    repaint(); 
} 
} 
class ButtonHandler implements ActionListener{ 

    public void actionPerformed(ActionEvent e) { 
     newImage(e); 
    } 

    } 
class ButtonHandler2 implements ActionListener{ 

    public void actionPerformed(ActionEvent e) { 
     newImage(e); 
    } 

} 
}