I'm writing a Chinese Chess program in Java and would much appreciate some guidance on designing/implementing the GUI.
The board is to be divided into a 9x10 grid, with an "image" of the chess piece occupying each cell. The cells also need to be 'registered' when clicked so I know which piece was clicked.
1) I'm thinking GridLayout for the layout manager for the JPanel representing the board. How do I add an image to each component?
public void paintComponent(Graphics g) {
Image dog = new ImageIcon("dog.png").getImage();
add(dog)
}
Does not work as dog is not a Component.
2) How would I register for clicks in each cell?
Use a JLabel containing an Icon. Then add the label to the grid layout. Read the Swing tutorial on How to Use Icons for more information.
Also read the section on How to Write a Mouse Listener for listening to clicks on the label.
Or you could use a JButton with an Icon and then use:
button.setBorderPainted(false);
so you don't see the action of clicking the button. Then you would use an ActionListener. The tutorial also has a section on using an ActionListener.
Yes, GridLayout seems appropriate for this use.
See the constructor JButton(Icon).
See this answer for an example that carves up an existing image tile set for use in JLabel and JButton instances.
Related
I am writing a main menu for my Java Swing application, I want it to have 3 options: "New Game", "High Scores", "Exit" and a background image. I created a JPanel, for these 3 buttons and a JLabel with some title(i.e "Main Menu"). JLayeredPane seems to be the best choice for making a menu with a background image, so I decided to implement it, and this is where my problem comes up.
As I understood by reading the JLayeredPane docs, it can have only 1 Layout type. I imagined doing it by giving my JPanel - the one with buttons and a label - a GridBagLayout and giving my other JPanel with background image an absolute position. But how can I do it if giving an absolute position to an object requires having Layout set to null?
Thanks in advance!
I trying to make mini game and this game have option to choose map. Diffrent chooice, choose diffrent map. Every choice have own map ( background image ). For background image I useing JLabel and method: setIcon().
My problem is that when I set image, all my components get hide. This is picuture: http://prntscr.com/qi5m8a ( You can see that only image can be seen ).
For map choose I use this structure, here is picture: http://prntscr.com/qi5n4c There is Play button with event like this:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if(mapOneRadioButton.isSelected())
{
this.dispose();
GameWindow game = new GameWindow();
game.setVisible(true);
game.setLocationRelativeTo(null);
backgroundLabelGameWin.setIcon(new javax.swing.ImageIcon(getClass().getResource("/main/earthProject.gif")));
}else if(mapTwoRadioButton.isSelected())
{
}else if(mapTreeRadioButton.isSelected())
{
}else if(mapFourRadioButton.isSelected())
{
}else if(mapFiveRadioButton.isSelected())
{
}
}
I useing JPanel where I add JLabel for background image and I useing JLayeredPanel for rest of components.
My problem/question is, How can I change/set image without breaking order in my frame ?? I mean, I want to change/set image in background so all my components can be seen.
Please help me, I have no idea how to fix this.
For background image I useing JLabel and method: setIcon(). My problem is that when I set image, all my components get hide.
Two options:
add the components to the JLabel. Then the components will paint on top of the image. The restriction of this approach is that the components must fit inside the image, since the image is always painted at its actual size.
do custom painting of the image on a JPanel by overring the paintComponent(...) method of the panel and then invoke the Grapics.drawImage(...) method. Then add your components to the panel. See Background Panel for a class that provides this support. This provides more flexibility as you can scale the image to fill the panel.
It will be better if you change the background of the JPanel instead of using a JLabel for image. You can't directly change the background of a JPanel. But you can refer following link to do it
http://www.java2s.com/Code/Java/Swing-JFC/Panelwithbackgroundimage.htm
I am trying to create JButton such as there must be a number painted on the top and right corner of JButton.
For example, in case of notification buttons there is a message in the button, how is that possible? Can the help be taken of paint method to draw the label?
there are three ways, by using
GlassPane
JLayer (Java7) based on JXLayer(Java6)
JButtton (all JComponents) is container too, there is easy and possible use standard LayoutManagers (only JFrame == BorderLayout and JPanel == FlowLayout have got implemented LayoutManager in API directly), then basically everything is possible
JButton and any JComponent extend Container class, so you should be able to add elements into JButton as if it were a simple panel. So in your case you can add a JLabel with your text into a button.
Also consider implementing Icon to decorate the button; ColorIcon is a simple example. You can use the color to signify buttons that are likely to need attention, and you can use drawString() specify a number.
I need some help with Java Swing components and its capabilities. I need to add a JPanel to a JFrame and paint an Ellipse2D on it. Onto the Ellipse2D I want to add another element, in my case it is a picture (right now I use an ImageIcon, maybe wrong). How can I achieve adding the Ellipse2D and the picture on the panel as shown in the image I attached?
The reason why I need the images separated is, because I need to change the filling color of the ellipse sometimes.
Thanks for any help.
What you need is to create a custom JPanel implementation and override paintComponent method.
Inside it, you just do:
public void paintComponent(Graphics g) {
super.paintComponent(g);
// Draw ellipse here
// Draw your image here. It will be drawn on top of the ellipse.
}
This way, you can hold the ellipse fill color in the CustomPanel class, and just call repaint() method after you change the color.
your idea could be very good described (including code example) in the Oracles tutorial How to Decorate Components with the JLayer Class
notice JLayer is available only for Java7, but its based on (for Java6) JXLayer
you can use (I'm using) GlassPane too, with the same / similair output to the Swing GUI
EDIT
quite easy and nice output is by using OverlayLayout, there is possible to overlay J/Component(s) with Graphics e.g., a few examples
take the two images as image icons like
ImageIcon car=new ImageIcon("image path");
ImageIcon elipse=new ImageIcon("image path");
add those two image icons two label
JLabel carLabel=new JLabel(car);
JLabel ellipseLabel=new JLabel(ellipse);
and set the position of ellipse and car
carLabel.setBounds(0,0,50,50);
ellipseLabel.setBounds(10,10,50,50);
I'm trying to come up with an elegant recreation of the search bar component in Thunderbird. The clear button doesn't appear until there is text in the box, so that screen-shot is a bit inaccurate.
Should I use a layered pane and get some buttons to float above the box? (but then getting the text to not appear below the buttons would be hacky)
Maybe just put buttons at the ends of the search bar and have it somehow blend in?
Any ideas or maybe a style reconsideration is welcome, thank you.
What about a white panel with a border and a JTextField without borders inside. Two buttons (or more) in the west and east. Button will appear/hide depending on the text field content.
You might be able to use the Text Prompt for the "Search all text" display.
Check out JideSoft's Common Layer and the Overlayable class.
Demos
For building a very similar component I've used JXLayer (for drawing the buttons) in conjunction with IntelliHints from JIDE OSS project (for implementing a drop down list of values).
This code adds a label with given icon to the right of the JTextPane. One thing to work on: don't let the text go under the label. You can use setMargin(), but it shifts the label too.
JTextField searchField = new JTextField(30);
searchField.setLayout(new BorderLayout());
JLabel label = new JLabel(icon);
label.setCursor(Cursor.getDefaultCursor());
searchField.add(label, BorderLayout.LINE_END);
label.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
searchField.setText("");
}
});