943,166 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 3823
  • Java RSS
Apr 24th, 2010
0

JButton (an image) - change size

Expand Post »
Hi. I am coding a game for my basic Java programming course, and I am currently coding a game (Othello, aka Reversi).

I am having issues with the JButton size.

public void game ()
    {
        resize (800, 680);

        game = new Panel ();

        Panel board = new Panel (new GridLayout (row, col, 0, 0));
        //board.setSize (new Dimension (640, 640));

        //initialize tracking array to have all black backgrounds
        tracker = new Color [row] [col];
        for (int i = 0 ; i < row ; i++)
        {
            for (int j = 0 ; j < col ; j++)
            {
                tracker [i] [j] = Color.black;
            }
        }

        //declare a new array of buttons
        a = new JButton [total]; //<--resize the button (I think this is the one)
        //a.setSize (85, 85); //Attempt
        

        //initialize each of the buttons in the array
        //with an empty label
        for (int i = 0 ; i < total ; i++)
        {
            a [i] = new JButton (createImageIcon ("images\\blank.gif")); \\Calling blank image file, 80x80
            a [i].addActionListener (this);
            a [i].setActionCommand ("" + i);
            a [i].setBackground (Color.black);
            board.add (a [i]);
        }

        game.setBackground (Color.black);
        //Adding

        JPanel grid = new JPanel ();
        //Creating the layout
        grid.setLayout (new BoxLayout (grid, BoxLayout.Y_AXIS));
        grid.setBackground (Color.black);
        
        grid.add (menuBar);
        grid.add (board);

        game.add (grid);

        screens.add ("3", game);
    }

This is one of the cards (screens), and the layout used for that is CardLayout. I resized the window to 800 by 680.

And then I copied the teacher's 2D button array code, and changed it slightly. I just made it so that every button appears to be blank. The middle four are different (for the start of the game, but I don't know how to fix it yet).

I need to resize the button to 80x80 (the image is exactly 80x80, square). So the square grid can be 640x640 (or maybe slightly bigger if necessary).

It might also be worth noting that I am using Java 1.4.2, Ready to Program v1.7.1

Thank you
Last edited by IOwnAndPwnU; Apr 24th, 2010 at 10:23 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
IOwnAndPwnU is offline Offline
13 posts
since Apr 2010
Apr 24th, 2010
0
Re: JButton (an image) - change size
You don't say what the problem is, exactly, but you are mixing old-style AWT classes (eg Panel) with Swing classes (eg JButton). This is always dodgy. You should replace all the AWT ? classes with the swing J? equivalent.
Your buttons should resize to fit the image when you pack the window, although you will probably have to override the default insets to avoid blank space around the icon.
What do you know about Java 1.7.1 that we don't? 1.7 isn't out yet. (Current is 1.6u20).
Featured Poster
Reputation Points: 1895
Solved Threads: 944
Posting Expert
JamesCherrill is offline Offline
5,741 posts
since Apr 2008
Apr 24th, 2010
0
Re: JButton (an image) - change size
The problem is that the JButton is larger than the image in it. There is excess space around the image, which is 80x80. I need it to appear that all images/buttons are exactly side by side. I said Java 1.4.2, the program I use is v1.7.1 (Ready to Program).

What is the difference between JPanel and Panel? And I realize that it's not the best way, but I have to, since that's what our teacher told us.

So how would I override the default insets to avoid blank space around the icon?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
IOwnAndPwnU is offline Offline
13 posts
since Apr 2010
Apr 24th, 2010
0
Re: JButton (an image) - change size
The original Java AWT design was prett limited, so Swing was introduced with a far better architecture (and loads of other small improvements while they were at it).

So what's ReadyTo Program? I thought you were just using those words as simple English, sorry.

The SetMargin method overrides the insets, but I find it's hard to get the button default graqphics out of the way, so I always use JLabels to display graphics, you just have to listen for MouseClicked instead of ActionPerformed.
Last edited by JamesCherrill; Apr 24th, 2010 at 11:49 am.
Featured Poster
Reputation Points: 1895
Solved Threads: 944
Posting Expert
JamesCherrill is offline Offline
5,741 posts
since Apr 2008
Apr 24th, 2010
0
Re: JButton (an image) - change size
The original Java AWT design was prett limited, so Swing was introduced with a far better architecture (and loads of other small improvements while they were at it).

So what's ReadyTo Program? I thought you were just using those words as simple English, sorry.

The SetMargin method overrides the insets, but I find it's hard to get the button default graqphics out of the way, so I always use JLabels to display graphics, you just have to listen for MouseClicked instead of ActionPerformed.
Yeah, Ready to Program is a Java Compiler.

I tried

Java Syntax (Toggle Plain Text)
  1. a.setMargin (0, 0);

but I keep getting the error:
No method named "setMargin" was found in type "javx.swing.JButton[]".

Your other solution looks like a good one, but I think it would not work for my course - cause I have to use her way.

So how else could I eliminate the space of a button with an image inside it.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
IOwnAndPwnU is offline Offline
13 posts
since Apr 2010
Apr 24th, 2010
0
Re: JButton (an image) - change size
Check the API. setMargin doesn't take 2 ints, it takes an Insets instance.

http://java.sun.com/j2se/1.4.2/docs/....awt.Insets%29

If you get a "no method..." error you should always check the API first.
Featured Poster
Reputation Points: 1895
Solved Threads: 944
Posting Expert
JamesCherrill is offline Offline
5,741 posts
since Apr 2008
Apr 24th, 2010
0
Re: JButton (an image) - change size
Alright, I tried using the Insets instance ... kind of confused.

I'll show you what I tried:

Java Syntax (Toggle Plain Text)
  1. a = new JButton [total];
  2. a.setMargin (new Insets (0,0,0,0));

It gives me the same error ("No method named "setMargin" was found in type "javax.swing.JButton[]."

So how would I use this Insets thing (code would be helpful please).

I was trying out MouseListener, but at the top of my code, I have this:

Java Syntax (Toggle Plain Text)
  1. public class Othello extends Applet implements ActionListener

It says on http://java.sun.com/docs/books/tutor...elistener.html I need to add

Java Syntax (Toggle Plain Text)
  1. public class MouseEventDemo ... implements MouseListener {

I tried:

Java Syntax (Toggle Plain Text)
  1. public class Othello extends Applet implements ActionListener MouseListener


Java Syntax (Toggle Plain Text)
  1. public class Othello extends Applet implements ActionListener implements MouseListener

But they both failed. So how do I get both Listeners in there?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
IOwnAndPwnU is offline Offline
13 posts
since Apr 2010
Apr 24th, 2010
0
Re: JButton (an image) - change size
Alright, I managed to get the border removed by adding 2 lines, so the array aspect looks like this:

 for (int i = 0 ; i < total ; i++)
        {
            //Set-up the starting game of Othello
            a [i] = new JButton (createImageIcon ("images\\blank.gif"));
            //Since 0, 27 is the 28th space in the board
            a [27] = new JButton (createImageIcon ("images\\white.gif"));
            a [28] = new JButton (createImageIcon ("images\\black.gif"));
            a [35] = new JButton (createImageIcon ("images\\black.gif"));
            a [36] = new JButton (createImageIcon ("images\\white.gif"));
            a [i].addActionListener (this);
            a [i].setActionCommand ("" + i);
            a [i].setBackground (Color.black);
            a [i].setForeground (Color.black);
            //Remove space between buttons and images in button (margin)
            a [i].setMargin (new Insets (0, 0, 0, 0));
            //Further removes button aspect of images
            a [i].setBorder (null);
            board.add (a [i]);
        }

I added the two lines bold.

Next question is how would I implement two listeners.

I need the

Java Syntax (Toggle Plain Text)
  1. public class Othello extends Applet implements ActionListener

which is just below my libraries. But I also want to be able to use the MouseListener. So, I'm having trouble implementing both listeners.

(additional untaught concepts is good).

I tried:


Java Syntax (Toggle Plain Text)
  1. public class Othello extends Applet implements ActionListener MouseListener

Java Syntax (Toggle Plain Text)
  1. 1.
  2. public class Othello extends Applet implements ActionListener implements MouseListener

The reason I need the MouseListener is because I may want it such that one a user is over a certain part of the array, a hint shows. Or maybe, I want it so that (since my game is Othello, aka Reversi), such that when the user's mouse is over a possible move, the image changes. Regardless, I need the MouseListener and the ActionListener (that's a must).

Thank you.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
IOwnAndPwnU is offline Offline
13 posts
since Apr 2010
Apr 25th, 2010
0
Re: JButton (an image) - change size
implements ActionListener, MouseListener, anyOtherInterfaces
Featured Poster
Reputation Points: 1895
Solved Threads: 944
Posting Expert
JamesCherrill is offline Offline
5,741 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: HelloWorldApplet
Next Thread in Java Forum Timeline: Encryption/Decryption of files in java.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC