|
I want to show ImageIcon by adding to Label, but it has error, help me?
<pre lang="java">import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.util.Scanner;
class CatchTheRat extends JFrame
{
// The Rat
JLabel lb;
// Move it randomly!
Random r;
public CatchTheRat(int k)
{
// Set frame properties
setTitle("Catch The Rat");
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
// Set the background (just for a good look)
setContentPane(new JLabel(new ImageIcon("background.jpg")));
// Set layout to the content pane
getContentPane().setLayout(new FlowLayout());
// Create the rat
lb=new JLabel(new ImageIcon("rat.jpg"));
// Add the rat
getContentPane().add(lb);
// Create Random object
r=new Random();
// Create a timer and call it for every k seconds
new Timer(k,new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
// Move the rat randomly, subtract 75, so that the rat should not meet the edges
lb.setLocation(r.nextInt(getWidth()-75),r.nextInt(getHeight()-75));
}
}).start();
// Add mouselistener, notify when user clicks it!
lb.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent me)
{
// Create a beep sound when clicked to notify
Toolkit.getDefaultToolkit().beep();
// Also print it!
System.out.println("Caught!");
}
});
// Maximize the frame
setExtendedState(MAXIMIZED_BOTH);
}
public static void main(String args[])
{
// Create Scanner object
Scanner s=new Scanner(System.in);
// Let the user enter his capability of catching the rat!
System.out.println("Enter the speed");
// Read the input
int k=s.nextInt();
// Create the frame and send the value of k
new CatchTheRat(k);
}
}
</pre>
|
|
|
|
|
- Please fix your formatting.
- Which line has the error?
- What error?
Note, we cannot see your screen or guess what problems you are seeing.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
It's called OOP for a reason. Please, the constructor never executes code. Define some methods for that.
You are adding the JLabel to the ContentPane - which you first set with a JLabel. Adding a JLabel to a JLabel does not work.
Also please just refer to the JFrame by adding to "this".
Suggestion:
1. OOP that.
2. Set up a JPanel, that forms the playground for the rat.
You probably want a second Panel for the Game controls - which are now to be entered in the console before.
3. Use a null-Layout for the game-Panel. You want to move the rat by pixel / absolute position, not by relative position to some other control.
FlowLayout is also default, does not need to be initalized.
import javax.swing.*;
class CatchTheRat extends JFrame {
JLabel lb;
Random r;
public CatchTheRat(int k) {
createGUI(k);
}
private void createGUI(int k) {
setTitle("Catch The Rat");
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(new Dimension(800,600));
this.add(createContent(k));
setVisible(true);
}
private JPanel createContent(int k) {
JPanel oPanel = new JPanel();
lb = new JLabel(new ImageIcon("rat.jpg"));
oPanel.add(lb);
r = new Random();
lb.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
Toolkit.getDefaultToolkit().beep();
System.out.println("Caught!");
}
});
return oPanel;
}
public static void main(String args[]) {
new CatchTheRat(5);
}
}
|
|
|
|
|
Thanks for the suggestion. But that worked in my system. We can add JLabel to another JLabel because JComponent itself is a container and we can add any component to any component. There is no limitation. The add method does it.
Gowtham Gutha (http://java-demos.blogspot.com)
|
|
|
|
|
Technically - yes. Until someone wants to place place a text in the JLabel or plays around.
So it's pretty much safer to use a JPanel, that is known as a container.
|
|
|
|
|
Ok. By the way, could i also know why it is not recommended to write code in the constructor and only in a separate method and then call that method?
Gowtham Gutha (http://java-demos.blogspot.com)
|
|
|
|
|
|
What really is the error in my program? I couldn't understand. You'll need to have those image files (the background and the rat) to see them. That may be the possible error because it has worked in my development environment.
Gowtham Gutha (http://java-demos.blogspot.com)
|
|
|
|