有关如何创建Java纸牌游戏的提示 [英] Tips regarding how to create an Java Solitaire game

查看:129
本文介绍了有关如何创建Java纸牌游戏的提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对使用java创建单人纸牌游戏的方法提出了一些疑问。

I got some question regarding the approach to create an solitaire game in java.

在Swing中处理这些卡片的最佳方法是什么?如何拖动它们,将它们捕捉到正确位置的最佳方法是什么?

What is the best way of handling the cards in Swing? How can I drag them, and what is the best way to snap them into their right positions?

推荐答案

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

What would be the best approach for dragging multiple images? I've come as far as using Java2D to draw two images to a JPanel, but I'm only able to drag one of them. I'll attach my source code. The problem with my solution is that I need to repaint the entire window even though I'm just manipulating one element. Is it possible to handle objects of cards, instead of images of them? So when I move one card, I'll move the visual presentation of the object, instead of a image (as I do now.)

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);
    }

 }
}

这篇关于有关如何创建Java纸牌游戏的提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆