Layouts in Applets
Author |
Message |
nonamedude
|
Posted: Tue May 11, 2010 11:35 pm Post subject: Layouts in Applets |
|
|
I am doing a game (count4) whereby you have to put pieces in rows and try and get 4 in a row.I am just a beginner so bear with me. I need a layout whereby I can assign buttons to a specific location (just like assigning a shape a location). I cannot use the GridLayout since my display block all the buttons. Just wondering if there is a layout like that... |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
DemonWasp
|
Posted: Wed May 12, 2010 7:49 am Post subject: RE:Layouts in Applets |
|
|
I've read your description twice and I still have no idea what you want. Can you rephrase your problem, possibly with more words, so that it makes sense to someone who has never seen your existing work? |
|
|
|
|
 |
chrisbrown

|
Posted: Wed May 12, 2010 9:54 am Post subject: Re: Layouts in Applets |
|
|
nonamedude @ Tue May 11, 2010 11:35 pm wrote: since my display block all the buttons.
I'm not sure what you mean by that, but could you not just add your buttons to a new component, use a GridLayout on that, and resize it as necessary?
Alternatively, using the null layout will alow you to specify exactly where you want each button to be (not a good solution), or the SpringLayout which is tedious but powerful. |
|
|
|
|
 |
TheGuardian001
|
Posted: Wed May 12, 2010 11:56 am Post subject: Re: Layouts in Applets |
|
|
Is there a reason that you are using buttons, instead of directly checking for the location of the mouse clicks to the applet? The buttons aren't really necessary for connect/count 4, so there's really no reason to include them. Just check where they clicked, and act based on that. That way, you don't have to mess with layouts (which are really annoying) or buttons at all.
However, if you must use buttons, I'd recommend a GridLayout. I'm not sure this will solve whatever problem you're having (I didn't understand it either), however it will allow you to have rows and columns of same-sized buttons, which seems ideal for this type of thing. |
|
|
|
|
 |
nonamedude
|
Posted: Wed May 12, 2010 4:58 pm Post subject: Re: Layouts in Applets |
|
|
How would you do this?
TheGuardian001 wrote:
Just check where they clicked, and act based on that. That way, you don't have to mess with layouts (which are really annoying) or buttons at all.
|
|
|
|
|
 |
TheGuardian001
|
Posted: Wed May 12, 2010 8:58 pm Post subject: Re: Layouts in Applets |
|
|
Have your class implement the MouseListener class. Once you've done that, in whichever mouse handler you want to use (generally MouseClicked, MousePressed, or MouseReleased), you just need to add code to check where the mouse was when the event happened. The MouseEvent parameter of that handler can give you lots of useful information, including the location of the event.
Java: |
public void MouseReleased (MouseEvent e )
{
int mouseX = e. getX();
int mouseY = e. getY();
System. out. println("X: " + mouseX )
System. out. println("Y: " + mouseY )
}
|
|
|
|
|
|
 |
nonamedude
|
Posted: Thu May 13, 2010 6:49 pm Post subject: Re: Layouts in Applets |
|
|
Hmmmmmm, do you need to assign this action to a button, just like the action listener or can a user just click on the screen? |
|
|
|
|
 |
TheGuardian001
|
Posted: Thu May 13, 2010 7:18 pm Post subject: Re: Layouts in Applets |
|
|
Provided that your class implements MouseListener, you simply add a MouseListener the same way you would to a button, and supply itself as the actual listener, IE:
Java: |
public class MyClass extends Applet implements MouseListener
{
public MyClass ()
{
this. addMouseListener(this); //The applet now handles its own mouse interactions.
//These can be handled by overriding the MouseListener methods, listed below.
}
/*
*Class MyClass implements MouseListener, and must contain the following methods (they can be empty if you want):
*void mouseClicked(MouseEvent e)
*void mouseReleased(MouseEvent e)
*void mousePressed(MouseEvent e)
*void mouseEntered(MouseEvent e)
*void mouseExited(MouseEvent e)
*For example:
*/
public void mouseClicked (MouseEvent e )
{
System. out. println("Mouse click at X: " + e. getX() + " Y: " + e. getY());
}
public void mouseReleased (MouseEvent e ){}
public void mousePressed (MouseEvent e ){}
public void mouseEntered (MouseEvent e ){}
public void mouseExited (MouseEvent e ){}
}
|
Once you've done that, the program should be able to detect user clicks to anywhere in the window.
Since your class is implementing MouseListener, you can directly override the methods in MouseListener to handle clicks however you want. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
nonamedude
|
Posted: Thu May 13, 2010 8:21 pm Post subject: Re: Layouts in Applets |
|
|
ty a lot, that really helps  |
|
|
|
|
 |
nonamedude
|
Posted: Thu May 13, 2010 8:31 pm Post subject: Re: Layouts in Applets |
|
|
Btw before I start changing all my code, can you make it do an action if a user clicks within a certain range for example anywhere between the area below
|--------------------|
|--------------------|
|--------------------|
|--------------------|
|--------------------|
It would result in an action |
|
|
|
|
 |
TheGuardian001
|
Posted: Thu May 13, 2010 9:15 pm Post subject: Re: Layouts in Applets |
|
|
Yes, you can (kind of). The easiest way I've found to do this is to use nested for loops, combined with an array with one element for each section.
For example, if you have a 4x4 grid, with each square being 10 pixels across, to check which column the click was in:
code: |
for (int i = 0; i < 4; i++)
{
if (e.getX() > i * 10 && e.getX() < (i+1) * 10)
{
system.out.println("Click in column " + i);
}
}
|
If you nest your other for loop inside of that if, you can check both row and column at once. You should just add those loops straight into whatever mouse event handler you are using. |
|
|
|
|
 |
nonamedude
|
Posted: Fri May 14, 2010 6:46 pm Post subject: Re: Layouts in Applets |
|
|
Ty a lot man, I really appreciate, got rid of those annoying buttons  |
|
|
|
|
 |
|
|