为Java游戏创建交互式GUI [英] Creating a Interactive GUI for a java game

查看:134
本文介绍了为Java游戏创建交互式GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们我正在创建一个类似于java中的farmville的游戏,我只是想知道如何实现用户通常点击与游戏客户端交互的交互式对象/按钮。

Hey guys I'm creating a game similar to farmville in java and I'm just wondering how would I implement the interactive objects/buttons that the user would usually click to interact with the game client.

我不想使用swing库(通用窗口看起来像对象),我想为我的按钮导入自定义图像,并为那些将用于GUI。

I do not want to use the swing library (generic windows looky likey objects), I would like to import custom images for my buttons and assign button like properties to those images which would be used for the GUI.

有什么建议吗?有什么指针吗?我似乎无法通过youtube或其他一些Java游戏网站找到这些信息,因为它们只显示使用swing的简单示例。

Any advice? Any pointers? I can't seem to find that information through youtube or some other java gaming sites as they're only showing simple example using swing.

任何帮助都将深表感谢,谢谢!

Any help would be deeply appreciated thanks!

关于
Gareth

Regards Gareth

推荐答案

你可以使用JButton,只是覆盖 paint 函数。并在那里画出你想要的东西。这需要一段时间,直到你第一次得到它如何工作。我建议你阅读一些关于事件调度线程的信息这里是java的解释)

You can use JButton, just override the paint function. and draw what ever you want there. It takes a while until you get it at the first time how this works. I recommend you to read a little about the event-dispatching thread (here is java's explanation)

以下是我写的一些代码,所以你有一个简单的参考。

And here is some code that I wrote so you have a simple reference.

import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;


public class Test extends JButton implements ActionListener{

    private static final long serialVersionUID = 1L;
    Image img;

        /**  constuctor     **/
    public Test(String tImg, JFrame parent){
        this.img = new ImageIcon(tImg).getImage();
        this.addActionListener(this);

    }


           /***********    this is the function you want to learn  ***********/
    @Override
    public void paint(Graphics g){
        g.drawImage(this.img, 0, 0, null);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO do some stuff when its clicked
        JOptionPane.showMessageDialog(null, "you clicked the button");
    }




    public static void main(String[] args) {
        JFrame f = new JFrame();
        Test t = new Test("pics.gif", f);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(new GridLayout(1, 1));
        f.add(t);
        f.setSize(400,600);
        f.setVisible(true);
    }

}

这篇关于为Java游戏创建交互式GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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