I am creating a minesweeper game in Java, and I want my JFrame to be in the exact size to be able to see all of the buttons and without margin.
I have tried to calculate the size per number of tiles, and it didn't really work. Is there any way I can make the JFrame to "auto size" by the size of the tiles?
You can use .setBounds(x,y,width,height) method for the JFrame. For the width and height, create an int counter which stores the number of tiles you have on screen. I am guessing you are using buttons are the tiles? So if the user chooser the difficulty, they are basically choosing the number of tiles that the board will be. e.g. easy mode: 20x20 tiles, medium mode: 50x50 tiles, etc. So if they choose easy, set the counter number (in the actionListener of the "Easy Mode" button to '20'. Then do yourJFrame.setBounds(0,0,counter,counter); This should work for a square board.
If you want a different shape, change the above counter to "counterX" for x-axis. Then make another int counterY'. This is for the y-axis. In this case, your code will beyourJFrame.setBounds(0,0,counterX,counterY);`
This should work. Have fun!
May be try this.
((JFrame)myButton.getTopLevelAncestor()).pack();
Where myButton is the button whose text is modified during execution.
Related
I have made a player that can display few steps with different images.
Under this player I have added a JSlider, to jump to every step. It should have labels for the first and last steps.
I managed by setting my own LabelTable for the JSlider.
It works very well for numbers under 100. But numbers with three or more digits get are cut off.
See the bottom-right corner of this image. Step 438 is not completely visible:
Is it possible to get the last (step)label completely visible?
I have already tried:
set the (max, min, preferred) size of the slider
get the JLabel over getLabelTable and set the location (a few pixels to the left) of the JLabel, but it is always (0,0).
The JSlider uses the SynthSliderUI.
Thank you! ;)
What I want is a little unusual. I want to make a screen that shows me a number squares inside of it. I should be able to determine the number of rows and lines by two integers. I also want to be able to set for example colors of the squares, and they should be clickable so they need to have an id that is numbered and an onClick function.
For example:
height=2
rows=3
color1=FFFFFF
color5=000000
is Something like this achievable? How should I approach this?
I think what you're looking for is a JButton.
For your particular problem, consider creating a 2D Array of JButtons and display them using a JPanel and JFrame. The size height and width of your grid would be the length of each of these arrays. E.g.
myArray.length() is the width (number of columns in the grid) and myArray[0].length() is the height of the grid (number of rows).
JButton supports setting images and colours.
Documentation:
https://docs.oracle.com/javase/tutorial/uiswing/components/button.html
So I have a large grid of 289 rectangles (17x17) and I need some way to change each one's color when they are clicked without having to make 289 different event methods. The retangles are colored based on a pattern:
if(y%2==0){
if(x%2==0)
g2.setColor(Color.WHITE)
else
g2.setColor(Color.BLUE)
}else{
if(x%2!=0)
g2.setColor(Color.WHITE)
else
g2.setColor(Color.RED)
}
I don't have any idea where I should start other than creating an big ArrayList of positions for each rectangle and their colors (such as {{0,0,Color.WHITE},{x,0,Color.BLUE}...etc};). I would use a for loop to create each of the rectangles using their parameters, but I don't know how I would create the ArrayList and Event method to detect which, if any, rectangle was clicked. How do I go about this?
EDIT:
I'm saying, how would I know which rectangle was clicked so I can change it's color? If it makes it easier, it's for a game where there are two players, red and blue. The board is made using the script above. When a player of a certain color clicks a white space the board changes that rectangles color to the players color, and that's where I have the trouble. I never have anyway to know when a player clicked one of the rectangles. How would I know when a player clicked a rectangle and how would I change it's color when clicked?
Add an instance of the same MouseListener to each component as it is constructed. There's an example here that changes a circle's color when the mouse is pressed.
Addendum: Based on your revised question, GridButtonPanel shows how a component may know its own coordinates, as well as how to reference a component based on its grid coordinates.
I was thinking of doing a commodore 64 style loading screen (with the alternating bars that change colour and grow/shrink in size) and was wondering if anyone has tried this or knows of any code I could look at. I'm gonna make a start on it myself today, but it would be good to have something to compare it against :)
So far, I'm guessing you divide the screen size up and set a constant for the bar height (say 12 horizontal bars for example) and you pick a random number between the constant and constant - 5 or whatever and refresh the screen. It's hard to tell looking at it, but it's something I'd be interested in reproducing.
I'd override paintComponent() to draw the horizontal bars. To tile the overlay, you might look at this example that uses getSubimage() to slice the image and javax.swing.Timer to pace the animation.
Addendum: See also this example that does horizontal slices.
I am working on a project in which I have a background image with specific points of interest. Each of these specific points will have a custom button class overlaid on it so that when I click the point, I'm actually clicking the button. However, I would like to be able to rotate the background image and have the buttons rotate with the image so that the custom buttons are still overlaid on the specific points. Any tips as to how I should go about doing this?
Are you actually wanting to rotate 4 different images and move them around the square, but always keeping them upright? Or are you rotating a single image so that after one button click the single image is on its side? If the former, then that can be easily done by using a container (a JPanel) that uses BorderLayout, and having four JPanels with background images and JButtons held in the container JPanel at the four compass points of the BorderLayout: BorderLayout.EAST, BorderLayout.WEST, BorderLayout.NORTH, and BorderLayout.SOUTH (although Java gurus prefer you use the newer constants, i.e., BorderLayout.PAGE_START). Then when a button is pressed, remove components and re-add but in a rotated order.
If you want to do the latter, then things get a bit trickier in that you'll likely need to use AffineTransforms, rotate instance to rotate the container, and you'll need to perform the same transformation on the point of the mouse press/click/release, so that the rotated buttons receive correct clicks. If the container is not square, things get even trickier still.