Resizing of frames doesn't work.
Hello. I am building a GUI which is to be used for a simple game. I have some panel classes that make certain panels with certain properties. However, when I try to add a frame into another, it gets really small. I don't seem to be able to resize and move it in any possible way. Can anyone tell me how I should do it?
A panel, quite similar in function to a button:
Code:
package GUI;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.LayoutManager;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* Simple program that loads, rotates and displays an image. Uses the file
* Duke_Blocks.gif, which should be in the same directory.
*
* @author MAG
* @version 20Feb2009
*/
@SuppressWarnings("serial")
public class TilePanel extends JPanel {
Image image;
private String color;
public TilePanel() {
super();
color = "empty";
image = Toolkit.getDefaultToolkit().getImage("empty.png");
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
System.out.println("CLICKED tilepanel");
}
});
}
public TilePanel(LayoutManager layout) {
super(layout);
color = "empty";
image = Toolkit.getDefaultToolkit().getImage("empty.png");
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
System.out.println("CLICKED tilepanel");
}
});
}
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(image, 0, 0, getWidth(), getHeight(), this);
}
/**
* @require color.equals("yellow") || color.equals("green") ||
* color.equals("red") || color.equals("blue") || color.equals("empty")
*/
public void setColor(String color) {
this.color = color;
image = Toolkit.getDefaultToolkit().getImage(color + ".png");
}
public String getColor() {
return color;
}
}
This one is used by this frame, which will in turn be a big part of the playing board:
Code:
package GUI;
//Import the basic graphics classes.
import java.awt.*;
import java.math.*;
import javax.swing.*;
public class BlockPanel extends JPanel {
Image image;
public BlockPanel() {
super();
image = Toolkit.getDefaultToolkit().getImage("wood.gif");
}
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
// g2d.translate(170, 0); // Translate the center of our coordinates.
g2d.drawImage(image, 0, 0, getWidth(), getHeight(), this);
}
public void rotates() {
}
public static void main(String arg[]) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
BlockPanel panel = new BlockPanel();
frame.setContentPane(panel);
frame.setVisible(true);
panel.rotates();
TilePanel tilePanel = new TilePanel(new FlowLayout());
tilePanel.setColor("green");
frame.add(tilePanel);
frame.setVisible(true);
}
}
wood.gif is just a texture of some wood, and there were empty.png, green.png, red.png,blue.png and yellow.png, which were all just differently coloured circles. Preferably I wouldn't want to bind these to specific locations, that is, use a layout to place them, and I'd like to set their coördinates and size instead. This is because the TilePanels need to move around, and by editing the coordinates in a while loop I could more easily animate this process (this is not the most important part of things, though, because only the begin positions and end positions count).
Re: Resizing of frames doesn't work.
Quote:
Originally Posted by
Amanoo
when I try to add a frame into another
That can't be done. What are you really doing?
db
Re: Resizing of frames doesn't work.
Sorry, I meant add a.JPanel into another. I always confuse the two words for some reason.
My current code can do that, but the methods for resizing don't seem to work. I just end up with a really small panel inside of a decently sized one. To be exact, I am trying to add a TilePanel to a Blockpanel, the two of which I have defined up here.
Re: Resizing of frames doesn't work.
Most layout managers respect the preferredSize. Does your TilePanel know what size it would like to be?
db