I'm storing some x,y coordinates in the rectangles (JTextFields) and I'd like the JLabels to be in the middle of those two text fields.
Idea:
Related
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
How to make a circle border with arcs of two colors like following picture?
I'm using canvas and the thing which I started by creating separate arcs of different color and then combining them together like this.
But this greatly increases the complexity of the app. Additionally, I have to create multiple circles and rotate them frame by frame once the app starts. With so many different arcs, everything gets messy.
Is there a nicer way of doing this? Is it possible to represent the circle as a single entity?
I am hoping someone can offer a strategy for the following Java GUI:
I am implementing a Scrabble-like algorithm and I would like to write a GUI where a user can compete with the algorithm. My experience with GUIs is limited and I am trying to avoid a big learning curve if someone can suggest a useful subset of components upon which I can focus.
I would like to display two playing areas side by side. Each playing area contains a board and a tile holder. Tiles from the holder can be dragged and dropped onto the board.
Obviously, the basic component is the tile, which is just a square displaying a letter. The tile holder and the board are both tile containers (grids of different sizes). The playing areas each hold the two tile containers and allow dragging and dropping from one container to the other.
The layout of the two playing areas is: the board on top, the tile holder on the bottom, and some neutral space between them.
The layout for the window is just to display the two playing areas side by side.
Perhaps naively, it seems to me I don't need much. But finding the right pieces is proving to be quite a task.
Any advice is appreciated.
Top level JPanel with a GridLayout, 1 row, 2 columns, will create for you a panel with two evenly split areas. After that you can add another JPanel for each side (just sequentially add them), and these two JPanels can have a BorderLayout. For these two BorderLayout JPanels, you can add to each the following:
A JPanel with a FlowLayout of ImageIcons that hold a pictures of a scrabble tile, and this JPanel will be added to the south position on the BorderLayout JPanel.
Another JPanel with a custom class you'll make called ScrabbleBoard extends JComponent. Here you can extend paintComponent and draw the board how you wish, probably making use of obtaining the width and height so you can draw a board that scales to how much space you have available for the component. This custom JComponent will be added to the center position of the BorderLayout JPanel.
A component added to the center position of a BorderLayout tries to be greedy and takes up as much room as possible, while components on the sides do not, so it's often a good choice for UI's that require a small navigation or menu control area, and a larger area to view what is considered the main graphics.
I'm creating a civilization game for a project just wondering what the best way to do it when it reads in a .map file with each line having characters inside of it representing a terrian
so for example t000="aaaaaaaaaaaaaaaaaaaaaaa"
would be the terrain for the arctic.
I'm just asking what the best method to place this into a view in java SWING, to view the map because what I'm doing right now is creating 2d array of jbuttons and placing the terrain image in those, but I'm thinking it would mess me up when I start adding sprites to the map.
Can anyone help?
Consider using ImageIcons for each terrain's background, and then perhaps creating a grid of JLabel, each holding the ImageIcon that corresponds to the correct terrain.
Edit
If your sprite is to remain on a tile, then add it to the tile as a component (just be sure to give the JLabel a decent layout manager). If the sprite is to move over the tiles, then it should be drawn in a different layer, either on the top level window's glasspane or in a JLayeredPane.
So far, my program draws many rectangles and the locations of the rectangles are random (not nice). How can I place the rectangle in grid order so that it look appealing?
Use a grid manager like GridLayout