Click here to Skip to main content
12,242,812 members (42,119 online)
   

Java

 
AnswerRe: Tower of hanoi in java Pin
TorstenH.12-Nov-12 1:22
memberTorstenH.12-Nov-12 1:22 
GeneralRe: Tower of hanoi in java Pin
prog.sidra12-Nov-12 20:58
memberprog.sidra12-Nov-12 20:58 
QuestionIf element in diagonal is 1 then its unit matrix , else it's not unit matrix Pin
Asad JOgi11-Nov-12 20:26
memberAsad JOgi11-Nov-12 20:26 
AnswerRe: If element in diagonal is 1 then its unit matrix , else it's not unit matrix Pin
TorstenH.11-Nov-12 22:15
memberTorstenH.11-Nov-12 22:15 
AnswerRe: If element in diagonal is 1 then its unit matrix , else it's not unit matrix Pin
BobJanova13-Nov-12 1:06
memberBobJanova13-Nov-12 1:06 
QuestionError show ImageIcon ??? Pin
nghia09t39-Nov-12 20:34
membernghia09t39-Nov-12 20:34 
AnswerRe: Error show ImageIcon ??? Pin
Richard MacCutchan10-Nov-12 0:32
mvpRichard MacCutchan10-Nov-12 0:32 
AnswerRe: Error show ImageIcon ??? Pin
TorstenH.10-Nov-12 15:02
memberTorstenH.10-Nov-12 15:02 
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 {
	// The Rat
	JLabel lb;
 
	// Move it randomly!
	Random r;
 
	public CatchTheRat(int k) {
		createGUI(k);
	}
 
	private void createGUI(int k) {
 
		// Set frame properties
		setTitle("Catch The Rat");
		setLayout(new FlowLayout());
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
//		setExtendedState(MAXIMIZED_BOTH);
		setSize(new Dimension(800,600));
		this.add(createContent(k));
		
		// Maximize the frame
		setVisible(true);
	}
 
	private JPanel createContent(int k) {
		JPanel oPanel = new JPanel();
		// Set the background (just for a good look)
//		setContentPane(new JLabel(new ImageIcon("background.jpg")));

		// Set layout to the content pane
//		oPanel.setLayout(new FlowLayout());

		// Create the rat
		lb = new JLabel(new ImageIcon("rat.jpg"));
 
		// Add the rat
		oPanel.add(lb);
 
		// Create Random object
		r = new Random();
 
// The random positions do not work, will "hide" the icon therefor

//		// 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()), r.nextInt(getHeight()));
//
//			}
//
//		}).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!");
			}
 
		});
		
		return oPanel;
 
	}
 
	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(5);
 
	}
}
regards Torsten
When I'm not working

GeneralRe: Error show ImageIcon ??? Pin
Gowtham Gutha15-Nov-12 7:46
memberGowtham Gutha15-Nov-12 7:46 
GeneralRe: Error show ImageIcon ??? Pin
TorstenH.15-Nov-12 10:00
memberTorstenH.15-Nov-12 10:00 
GeneralRe: Error show ImageIcon ??? Pin
Gowtham Gutha16-Nov-12 8:08
memberGowtham Gutha16-Nov-12 8:08 
GeneralRe: Error show ImageIcon ??? Pin
TorstenH.18-Nov-12 20:10
memberTorstenH.18-Nov-12 20:10 
AnswerRe: Error show ImageIcon ??? Pin
Gowtham Gutha15-Nov-12 7:43
memberGowtham Gutha15-Nov-12 7:43 
Questionhow to create digital survey android and php?? Pin
Luis Antonio Mantilla9-Nov-12 13:22
memberLuis Antonio Mantilla9-Nov-12 13:22 
QuestionAny Java Book Recommend Pin
waylife7-Nov-12 20:14
memberwaylife7-Nov-12 20:14 
AnswerRe: Any Java Book Recommend Pin
Peter_in_27808-Nov-12 2:13
memberPeter_in_27808-Nov-12 2:13 
GeneralRe: Any Java Book Recommend Pin
waylife12-Nov-12 23:07
memberwaylife12-Nov-12 23:07 
AnswerRe: Any Java Book Recommend Pin
Gowtham Gutha9-Nov-12 10:02
memberGowtham Gutha9-Nov-12 10:02 
GeneralRe: Any Java Book Recommend Pin
waylife12-Nov-12 23:08
memberwaylife12-Nov-12 23:08 
GeneralRe: Any Java Book Recommend Pin
Gowtham Gutha15-Nov-12 1:22
memberGowtham Gutha15-Nov-12 1:22 
GeneralRe: Any Java Book Recommend Pin
waylife15-Nov-12 4:10
memberwaylife15-Nov-12 4:10 
GeneralRe: Any Java Book Recommend Pin
waylife14-Nov-12 1:54
memberwaylife14-Nov-12 1:54 
AnswerRe: Any Java Book Recommend Pin
Ivan Melnikov1-Jul-13 23:38
memberIvan Melnikov1-Jul-13 23:38 
QuestionHow to create/set an element of a document Pin
prithaa6-Nov-12 4:43
memberprithaa6-Nov-12 4:43 
AnswerRe: How to create/set an element of a document Pin
padmanabanproject7-Nov-12 20:58
memberpadmanabanproject7-Nov-12 20:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web02 | 2.8.160426.1 | Last Updated 24 Apr 2016
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid