3

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.

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.

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!

Regards Gareth

Gareth
  • 69
  • 1
  • 1
  • 6
  • You really should start with Swing or SWT to get the default look and feel of your environment and customize from there. If you start with AWT then you'll need to put the work into stylizing your app for each OS. – Mike Yockey Sep 26 '11 at 19:34

2 Answers2

5

Do you really not want to use Swing, or do you just not want the default look and feel of a JButton and other swing controls? What does " (generic windows looky likey objects), " mean?

There are many sources out there that describe customizing buttons to include images on top of them: Creating a custom button in Java

JButton and other controls have all the events and methods associated with adding click listeners, etc. You probably don't want to create your own control. We do not have enough information to go off of, for example what does "interactive objects" mean?

If you simply want to add an icon to a JButton, use the constructor that takes an Icon.

Community
  • 1
  • 1
Stealth Rabbi
  • 10,156
  • 22
  • 100
  • 176
  • I just don't like the default look for JButton. I just wanted to know how to possibly change it. My game buttons will be rectangular with artistic images on them to represent things like: Character Button Buildings Button Map Button Marketplace Button Hope this cleared a little something up for you. – Gareth Sep 26 '11 at 20:57
  • 1
    If you're talking about wanting a basic button with an image on it (like Sim City 2000 panel), just use JButton's constructor that takes an icon. – Stealth Rabbi Sep 27 '11 at 13:15
  • 2
    *"I just don't like the default look for JButton."* [This source](http://stackoverflow.com/questions/6743306/how-do-i-create-an-event-handler-for-a-jlabel/6743726#6743726) shows how to change it so only an image shows. The button will appear exactly as nice as whatever image(s) you put in it. :) – Andrew Thompson Sep 27 '11 at 13:52
  • Thanks guys for all your comments and help .. I'll take this azway and learn from it ... I appreciate your time and guidance! – Gareth Sep 27 '11 at 21:16
0

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

}
Ramzi Khahil
  • 4,932
  • 4
  • 35
  • 69
  • Why are you subclsasing JButton and overriding paint without calling super.paint()? I don't think this will work. Why not just use the JButton constructor that takes an Icon? http://leepoint.net/notes-java/GUI/components/20buttons/23buttonicons.html – Stealth Rabbi Sep 27 '11 at 12:55
  • -1 1) Why are you overriding `paint(Graphics)` rather than `paintComponent(Graphics)`? 2) Why are you extending `JButton` at all? It has methods to set images for a number of different uses (see my linked example). – Andrew Thompson Sep 27 '11 at 13:55
  • 1
    What's with all the down-rating? just trying to help someone, and that's the best I know. And here are some things to think about: 1) The most correct way to do things is maybe very hard to learn, and thats not always what the one who asked looking for 2) according to his question he is a little new to that. And that's the first thing I learned at it was a good way into that direction. 3) he accepted my answer as the best one, so the fact that this is simple is probably the bast thing for him. 4)If that makes that a big difference to him, he can open another topic asking what is more efficient – Ramzi Khahil Sep 27 '11 at 17:27