Moving a JLabel for a game - java

I am coding a Backgammon game that plays by itself. While the backend code is mostly done, me and my colleague have pretty much no experience in GUI coding. We used the Designer given by a plugin for Eclipse and most of the code was generated.
So here's the thing. Right now there is a JFrame which has a JPanel in which are 2 JLabels, one for the background and one for 1 playing piece (when I figure this out, the rest of them will be added). These are all in an initialize() method which is run through EventQueue.invokeLater(new Runnable()). After the board and the piece are drawn, the startGame() method is called, which finally comes to a method called movePiece(), with the sole purpose of moving that JLabel piece I talked about before.
I have tried mostly everything I can think of, I have scoured the forums and only found people talking about layouts (which I may or may not be using, as I said, generated code) and the darn JLabel won't move when I call setLocation() inside the movePiece() method. It always throws a NullPointerException at that line which is bizarre because the image is already drawn. I will include part of the code below.
I know this will probably look like some of the worse codes ever, but please bear with my lack of skill and help me make this better. Any insights?
public class Main{
public JLabel image1;
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main window = new Main();
window.frame.setVisible(true);
new Main(1);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Main() {
initialize();
}
public Main(int i){
startGame();
}
public void startGame() {
//game code
movePiece(Move.getPos(), Move.getDest());
//rest of code
}
public void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 1023, 617);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setPreferredSize(new Dimension(800, 700));
JPanel panel_1 = new JPanel();
panel_1.setPreferredSize(new Dimension(200, 700));
frame.getContentPane().add(panel_1, BorderLayout.EAST);
JButton btnNewButton = new JButton("Start Game");
/* ACTION LISTENER FOR THE BUTTON */
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btnNewButton.setFocusable(false);
btnNewButton.setForeground(SystemColor.windowBorder);
btnNewButton.setBackground(SystemColor.menu);
btnNewButton.setVerifyInputWhenFocusTarget(false);
btnNewButton.setRequestFocusEnabled(false);
btnNewButton.setRolloverEnabled(false);
btnNewButton.setFont(new Font("Verdana", Font.PLAIN, 11));
btnNewButton.setDebugGraphicsOptions(DebugGraphics.NONE_OPTION);
/* ADDING THE BTN TO PANEL 1 TO THE RIGHT */
panel_1.add(btnNewButton);
JPanel parentPanel = new JPanel();
parentPanel.setFocusable(false);
parentPanel.setEnabled(false);
parentPanel.setDoubleBuffered(false);
parentPanel.setDebugGraphicsOptions(DebugGraphics.NONE_OPTION);
parentPanel.setIgnoreRepaint(true);
parentPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
parentPanel.setBorder(null);
parentPanel.setPreferredSize(new Dimension(800, 700));
parentPanel.setBackground(SystemColor.menu);
frame.getContentPane().add(parentPanel, BorderLayout.WEST);
/* BACKGAMMON BOARD LABEL-->PANEL */
ImageIcon imageIcon = new ImageIcon(
new ImageIcon("images/bg.png").getImage()
.getScaledInstance(800, 551, Image.SCALE_SMOOTH));
parentPanel.setLayout(null);
ImageIcon imageIcon1 = new ImageIcon(
new ImageIcon("images/pl.png").getImage()
.getScaledInstance(50, 50, Image.SCALE_SMOOTH));
image1 = new JLabel("");
image1.setBounds(37, 36, 50, 50);
parentPanel.add(image1);
image1.setIcon(imageIcon1);
JLabel bg = new JLabel("");
bg.setBounds(10, 11, 800, 551);
bg.setIcon(imageIcon);
parentPanel.add(bg);
}
void movePiece(int pos, int dest) {
//null exception happens here, ignore the arguments
image1.setLocation(37 + 50,36);
}

You create an instance of Main and make it visible...
Main window = new Main();
window.frame.setVisible(true);
You then create a new instance of Main, which, through it's constructor, calls your moviePiece method...
new Main(1);
But the new instance of Main has nothing to do with the previous instance or any of the components which they created.
Instead, trying doing...
window.movePiece(Move.getPos(), Move.getDest());
instead of new Main(1);

Related

Java Swing layout not appearing

I'm just getting into creating GUIs in Java, and with this basic setup, I can't get anything to appear in my JFrame:
public class Main extends JFrame {
public static void main(String[] args) {
JFrame jframe = new JFrame();
jframe.setSize(400,400); // setting size
jframe.setVisible(true); // Allow it to appear
jframe.setTitle("Lab 7");
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Main init = new Main();
}
public Main() {
Container pane = getContentPane();
pane.setBackground(Color.BLUE);
pane.setLayout(new FlowLayout());
JButton add = new JButton("Add");
JButton close = new JButton("Close");
JTextArea area = new JTextArea();
JScrollPane scroll = new JScrollPane(area);
add.setBounds(70, 125, 80, 20);
close.setBounds(70, 115, 80, 20);
pane.add(add);
pane.add(close);
pane.add(scroll);
AddClick added = new AddClick();
add.addActionListener(added);
}
}
I also tried moving all the JFrame stuff into public Main() but it caused an infinite amount of windows opening and I had to end the program each time.
You're creating two separate JFrames: one is your Main class, and the other is an unrelated JFrame. Most of the customization, including adding components, happens to the Main in its constructor. But you only ever set the other JFrame to be visible.
Use your Main instance as the JFrame instead of creating another one, and the problem will be fixed.
public static void main(String[] args) {
JFrame jframe = new Main(); //Use an instance of Main as your JFrame
jframe.setSize(400,400); // setting size
jframe.setVisible(true); // Allow it to appear
jframe.setTitle("Lab 7");
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

setImageIcon doesn't set JFrame icon on mac swing window

I've already tried loads of code from Stack. For some reason it's just not setting the ImageIcon for my JFrame, the comments are other attempts that have not worked;I avoided calling super so that I could reference the JFrame -- GUIPhotoAlbum extends JFrame; code:
public GUIPhotoAlbum ()
{
super("PhotoAlbum");
ImageIcon img = new ImageIcon("Photos/albumIcon.png");
this.setIconImage(img.getImage());
/*
try{
setIconImage(ImageIO.read(new File("Photos/albumIcon.png")));
}catch(Exception e){
System.out.print("Didn't work.");
}
*/
setSize(875, 625);
this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout(5, 5));
initComponents();
initMenuBar();
initTopPanel();
add(topPanel, BorderLayout.CENTER);
initBottomPanel();
add(bottomPanel, BorderLayout.SOUTH);
addListeners();
setLocationRelativeTo(null);
setVisible(true);
}
EDIT
I'm running the program like this, where I try to set the ImageIcon of JFrame in the GUIPhotoAlbum() constructor; here's the driver:
public class AlbumDriver
{
public static void main (String [ ] args)
{
SwingUtilities.invokeLater
(
new Runnable()
{
#Override
public void run()
{
GUIPhotoAlbum pa = new GUIPhotoAlbum();
}
}
);
}
}
What am I doing wrong here?
PS I've tried BufferedImage, ImageIcon, using File.. and I'm using a Mac
Mac does not support frame icons, as seen in this answer.
Use this to change Dock Image in mac:
File imageFile = new File("Your image Path");
Image image = ImageIO.read(imageFile);
Application.getApplication().setDockIconImage(image);
For windows use this:
YourFrameObject.setIconImage(image);
The problem is, you class appears to be extending from JFrame but you're creating a new instance of a JFrame and setting it's icon instead...
JFrame newFrame = new JFrame("PhotoAlbum");
ImageIcon img = new ImageIcon("Photos/albumIcon.png");
newFrame.setIconImage(img.getImage());
Don't create the second instance of the JFrame, there's no need for newFrame in this instance...
For example...
public GUIPhotoAlbum ()
{
super("PhotoAlbum");
ImageIcon img = new ImageIcon("Photos/albumIcon.png");
setIconImage(img.getImage());
/*
//when uncommented, exception is never thrown
try{
setIconImage(ImageIO.read(new File("Photos/albumIcon.png")));
}catch(Exception e){
System.out.print("Didn't work.");
}
*/
// Hint use pack instead, but only after
// You've finished adding the components to the frame
setSize(875, 625);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout(5, 5));
initComponents();
initMenuBar();
initTopPanel();
add(topPanel, BorderLayout.CENTER);
initBottomPanel();
add(bottomPanel, BorderLayout.SOUTH);
addListeners();
setLocationRelativeTo(null);
setVisible(true);
}

Resize JButtons in a BoxLayout

I'm trying to make a simple menu for my game. I have 4 buttons in the center and I want to make them a little bit bigger and center them.
The last part worked but I can't seem to call any of my JButtons and do a .setSize / .setPreferedSize(new Dimension()) on it.
public class mainMenu extends JFrame {
private JButton start, highscore, help, stoppen;
public mainMenu() {
super("Master Mind");
maakComponenten();
maakLayout();
toonFrame();
}
private void maakComponenten() {
start = new JButton("Start");
start.setBackground(Color.gray);
highscore = new JButton("Higscores");
help = new JButton("Help");
stoppen = new JButton("Stoppen");
}
private void maakLayout() {
JPanel hoofdmenu = new JPanel();
hoofdmenu.setLayout(new BoxLayout(hoofdmenu, BoxLayout.Y_AXIS ));
hoofdmenu.add(start);
start.setAlignmentX(CENTER_ALIGNMENT);
hoofdmenu.add(highscore);
highscore.setAlignmentX(CENTER_ALIGNMENT);
hoofdmenu.add(help);
help.setAlignmentX(CENTER_ALIGNMENT);
hoofdmenu.add(stoppen);
stoppen.setAlignmentX(CENTER_ALIGNMENT);
super.add(hoofdmenu);
}
private void toonFrame() {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
setSize(500,500);
}
public static void main(String[] args) {
new mainMenu();
}
}
As an example, to change the size of the "Start" button,
change :
start1 = new JButton("Start");
to
start1 = new JButton("Start") {
{
setSize(150, 75);
setMaximumSize(getSize());
}
};
The problem is that JFrames use BorderLayout by default, which means that your JPanel will naturally fill the space.
Before adding your JPanel, call the following code to change the JFrame's layout to null and use the JPanel's settings instead.
this.setLayout(null);
JPanel hoofdmenu = new JPanel();
hoofdmenu.setBounds(0,0, 400, 100);
Alternatively, you could set the maximum size of the JButtons
Dimension maxSize = new Dimension(100, 100);
start.setMaximumSize(maxSize);
highscore.setMaximumSize(maxSize);
help.setMaximumSize(maxSize);
stoppen.setMaximumSize(maxSize);
Here's another example following behind the previous two - I'm making a soundboard program, and this is actually a sample from it - The JPanel actually is needed, agreeing to the second post.
JFrame frame = new JFrame();
JPanel menuPanel = new JPanel();
JButton Button1 = new JButton("<BUTTON NAME 1>");
Button1.setSize(80, 30);
Button1.setLocation(4, 4);
JButton Button2 = new JButton("<BUTTON NAME 2>");
Button2.setSize(80, 30);
Button2.setLocation(90, 4);
Ah, and another thing - You created the buttons in a different block from the second piece of code. Doing that causes the other blocks to not see it. You need to declare them outside the block so all the blocks can see them.

Adding JFileChooser with no action causes panels to not render

This is a weird problem. I have a solution for it, but I don't know WHY the problem occurs in the first place. Observe the code below:
// VERSION 1
public class test {
public static void main(String[] args) {
JFrame mainFrame = new JFrame("Test");
JPanel inputPanel = new JPanel();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.getContentPane().add(BorderLayout.CENTER, inputPanel);
mainFrame.setBounds(100, 50, 200, 100);
mainFrame.setVisible(true);
JButton inputFileButton = new JButton("BROWSE");
inputPanel.add(inputFileButton);
}
}
It works as expected. The button does nothing, but it renders correctly. Now, I add a JFileChooser (which I plan to do something with later, but for now all I'm doing is instantiating it).
// VERSION 2
public class test {
public static void main(String[] args) {
JFrame mainFrame = new JFrame("Test");
JPanel inputPanel = new JPanel();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.getContentPane().add(BorderLayout.CENTER, inputPanel);
mainFrame.setBounds(100, 50, 200, 100);
mainFrame.setVisible(true);
JFileChooser inputFileChooser = new JFileChooser(); // NEW LINE
JButton inputFileButton = new JButton("BROWSE");
inputPanel.add(inputFileButton);
}
}
All of a sudden my button no longer renders. Why? I know two ways to make it work again, but neither makes 100% sense to me. One way to fix it:
// VERSION 3
public class test {
public static void main(String[] args) {
JFrame mainFrame = new JFrame("Test");
JPanel inputPanel = new JPanel();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.getContentPane().add(BorderLayout.CENTER, inputPanel);
mainFrame.setBounds(100, 50, 200, 100);
mainFrame.setVisible(true);
JButton inputFileButton = new JButton("BROWSE");
inputPanel.add(inputFileButton);
JFileChooser inputFileChooser = new JFileChooser(); // MOVE LINE TO END
}
}
So moving that line to the end allows the button to render again, but that still makes no sense to me what an instantiated JFileChooser has to do with the unconnected button. Another way I can fix this issue:
// VERSION 4
public class test {
public static void main(String[] args) {
JFrame mainFrame = new JFrame("Test");
JPanel inputPanel = new JPanel();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.getContentPane().add(BorderLayout.CENTER, inputPanel);
mainFrame.setBounds(100, 50, 200, 100);
JFileChooser inputFileChooser = new JFileChooser();
JButton inputFileButton = new JButton("BROWSE");
inputPanel.add(inputFileButton);
mainFrame.setVisible(true); // MOVE *THIS* LINE TO THE END
}
}
It kind of makes sense why the version above fixes the problem... obviously something about the JFileChoose instantiation was making my button invisible, but this setVisible() method afterwards bring it back into the light. But that still doesn't tell me WHY it went invisible in the first place.
Can somebody please help me figure out what I'm missing? Thanks!
You are making your mainFrame visible and adding the button afterwards. Take a look at this SO question on what steps you need to take to make sure your button is visible.
The reason why it works in your first example is probably pure luck. Your call to add the button will be performed before the EDT shows your component.
Note: please perform the Swing operations on the EDT

Java inheritance or GUI gone wrong

despite some tips, I'm still getting this one wrong. I end up with one basic window and another one with extra features, but without the basic ones from the previous window. Instead, I would like one new window combining the basic and new features. Here is the code I've got: (also which approach would you advise?)
package windows;
import java.awt.*;
import javax.swing.*;
public abstract class WindowTemplate extends JFrame {
/**
* Create the GUI and show it. For thread safety, this method should be
* invoked from the event-dispatching thread.
*/
public WindowTemplate () {
JFrame myFrame = new JFrame("My first window");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);
myFrame.setSize(550, 450);
myFrame.setLocationRelativeTo(null);
// JLabel emptyLabel = new JLabel("");
// emptyLabel.setPreferredSize(new Dimension(550, 450));
// myFrame.getContentPane().setLayout(new CardLayout());
// myFrame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
// myFrame.pack();
}
}
now the one that is meant to be "extended":
package windows;
import java.awt.*;
import javax.swing.*;
public class a_Welcome extends WindowTemplate {
public a_Welcome() {
JPanel area = new JPanel();
JLabel text = new JLabel("One line another line and another line"); // , JLabel.CENTER);
// text.setBounds(80, 400, 400, 50);
add(area);
// area.setLayout(null);
area.add(text, new CardLayout());
// area.add(text); // , BorderLayout.CENTER);
Font font = new Font("SansSerif", Font.BOLD, 30);
text.setFont(font);
text.setForeground(Color.green);
area.setBackground(Color.darkGray);
area.setSize(550, 450);
}
}
// timer-after 5 seconds-go to the next window (countdown in the bottom right corner)
and the main:
package windows;
public class Launcher {
public static void main(String[] args) {
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
// WindowTemplate.createWindow();
// a_Welcome.createWindow();
a_Welcome window = new a_Welcome();
window.setVisible(true);
}
});
}
}
-- Alternatively --
public class WindowTemplate extends JFrame {
// Constructor
public WindowTemplate() {
init();
}
public void init() {
// add basic components
JFrame myFrame = new JFrame("My first window");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);
myFrame.setSize(550, 450);
myFrame.setLocationRelativeTo(null);
}
}
and
public class a_Welcome extends WindowTemplate {
public a_Welcome() {
super();
}
#Override
public void init() {
super.init(); // important so you get the base stuff
// add other components
JPanel area = new JPanel();
JLabel text = new JLabel("One line another line and another line");
add(area);
area.add(text, new CardLayout());
Font font = new Font("SansSerif", Font.BOLD, 30);
text.setFont(font);
text.setForeground(Color.green);
area.setBackground(Color.darkGray);
area.setSize(550, 450);
}
}
Sorry for lots of code and thanks for your help!
I'm not quite sure what your problem is, but your WindowTemplate basically is a JFrame, so you don't want to create a new JFrame in your constructor but rather "apply" those method to this instead of myFrame.
Try something like this:
public WindowTemplate()
{
super("My first window");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // "this" is optional
setVisible(true);
setSize(550, 450);
setLocationRelativeTo(null);
}
and
public a_Welcome()
{
super(); // this is implicit, because a_Welcome extends WindowTemplate, which has got a constructor without parameters
//[add more stuff here]
}
When you're creating a new a_Welcome, its constructor will call the super constructor, which is WindowTemplate which will in turn call the JFrame constructor
First, although you extend JFrame, you create a new JFrame and use it in each constructor. It should be looking like that:
public WindowTemplate () {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setPreferredSize(new Dimension(550, 450));
this.pack();
this.setVisible(true);
Now, if you do it like that, you can add JComponents in each constructor / init method. Instead, you create two separate JFrames, one in each constructor.
I would recommend to avoid too deep hierarchy in case of swing components, since you will face many unexpected layout issues, what works for one, doesn't work for the other, when more components are added.
I think in your second example, in WindowTemplate, you're creating another JFrame when you think you're initializing the this JFrame.
Instead of
JFrame myFrame = new JFrame("My first window");
you probably want
super("My first window");
and then replace any myFrame references thereafter with this
If I read your code correctly, WindowTemplate is a JFrame. And within WindowTemplate's constructor you instantiate another JFrame? This does not make much sense. Instead of creating and configuring myFrame, you should configure this
public WindowTemplate () {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
...
}
as you are doing it in the subclass, where you are calling this.add(area)

Categories