8 Replies - 5723 Views - Last Post: 03 March 2009 - 10:41 PM Rate Topic: -----

#1 stutfly  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 30-August 08

moving a label on an applet

Posted 03 March 2009 - 08:45 PM

I need help setting up a timer to move labels across the x axis when a button is pushed can someone please help btw this is for a horse racing game that I am making currently the horses will move but only if you keep pushing the button

I am using netbeans IDE 6.5






/*
 * HorseRace.java
 *
 * Created on Mar 2, 2009, 9:07:17 AM
 *
 * @author Jordan Umbel
 */
import java.util.Random;

public class HorseRace extends javax.swing.JFrame {

	/** Creates new form HorseRace */
	public HorseRace() {
		initComponents();
	}

	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jPanel1 = new javax.swing.JPanel();
		Horse1 = new javax.swing.JLabel();
		Horse2 = new javax.swing.JLabel();
		Horse3 = new javax.swing.JLabel();
		Horse5 = new javax.swing.JLabel();
		Horse4 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		startButton = new javax.swing.JButton();
		jLabel3 = new javax.swing.JLabel();
		jLabel1 = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();
		timeLabel = new javax.swing.JLabel();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setBackground(new java.awt.Color(0, 0, 0));

		jPanel1.setBackground(new java.awt.Color(23, 123, 23));
		jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
		jPanel1.setName("HorseRace"); // NOI18N

		Horse1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Horse1.JPG"))); // NOI18N

		Horse2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Horse2.JPG"))); // NOI18N

		Horse3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Horse3.JPG"))); // NOI18N

		Horse5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Horse5.JPG"))); // NOI18N

		Horse4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Horse4.JPG"))); // NOI18N

		startButton.setBackground(new java.awt.Color(0, 204, 51));
		startButton.setText("Start");
		startButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				startButtonActionPerformed(evt);
			}
		});

		jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/back.JPG"))); // NOI18N

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout.setHorizontalGroup(
			jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addGroup(jPanel1Layout.createSequentialGroup()
				.addContainerGap()
				.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
					.addComponent(startButton, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
					.addComponent(Horse5, javax.swing.GroupLayout.PREFERRED_SIZE, 125, Short.MAX_VALUE)
					.addComponent(Horse4, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)
					.addComponent(Horse3, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)
					.addComponent(Horse2, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)
					.addComponent(Horse1, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE))
				.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
				.addComponent(jLabel2)
				.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
				.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 678, Short.MAX_VALUE)
				.addGap(68, 68, 68))
		);
		jPanel1Layout.setVerticalGroup(
			jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
				.addContainerGap()
				.addComponent(startButton, javax.swing.GroupLayout.DEFAULT_SIZE, 53, Short.MAX_VALUE)
				.addGap(26, 26, 26)
				.addComponent(Horse1, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
				.addGap(18, 18, 18)
				.addComponent(Horse2, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
				.addGap(18, 18, 18)
				.addComponent(Horse3, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
				.addGap(18, 18, 18)
				.addComponent(Horse4, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
				.addGap(18, 18, 18)
				.addComponent(Horse5, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
				.addGap(60, 60, 60))
			.addGroup(jPanel1Layout.createSequentialGroup()
				.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 558, javax.swing.GroupLayout.PREFERRED_SIZE)
				.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
			.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
				.addContainerGap(25, Short.MAX_VALUE)
				.addComponent(jLabel3)
				.addGap(21, 21, 21))
		);

		jLabel1.setBackground(new java.awt.Color(0, 0, 0));
		jLabel1.setFont(new java.awt.Font("Goudy Old Style", 3, 36));
		jLabel1.setText("Horse Racing");
		jLabel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

		jLabel4.setFont(new java.awt.Font("Goudy Old Style", 0, 36));
		jLabel4.setText("Time:");
		jLabel4.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

		timeLabel.setFont(new java.awt.Font("Goudy Old Style", 3, 36));
		timeLabel.setForeground(new java.awt.Color(153, 153, 153));
		timeLabel.setText("0");
		timeLabel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(
			layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addGroup(layout.createSequentialGroup()
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
					.addGroup(layout.createSequentialGroup()
						.addContainerGap()
						.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
					.addGroup(layout.createSequentialGroup()
						.addGap(162, 162, 162)
						.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE)
						.addGap(18, 18, 18)
						.addComponent(jLabel4)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(timeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)))
				.addContainerGap())
		);
		layout.setVerticalGroup(
			layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addGroup(layout.createSequentialGroup()
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
					.addComponent(jLabel1)
					.addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addComponent(timeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
				.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 562, javax.swing.GroupLayout.PREFERRED_SIZE)
				.addContainerGap(64, Short.MAX_VALUE))
		);

		pack();
	}// </editor-fold>

	private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {											
		int num = 0;
		int num2 = 0;
		int num3 = 0;
		int num4 = 0;
		int num5 = 0;



		num = (int) (Math.random() * 15);
		
		Horse1.setLocation(Horse1.getX()+num,Horse1.getY());
		num2 = (int) (Math.random() * 15);
		Horse2.setLocation(Horse2.getX()+num2,Horse2.getY());
		num3 = (int) (Math.random() * 15);
		Horse3.setLocation(Horse3.getX()+num3,Horse3.getY());
		num4 = (int) (Math.random() * 15);
		Horse4.setLocation(Horse4.getX()+num4,Horse4.getY());
		num5 = (int) (Math.random() * 15);
		Horse5.setLocation(Horse5.getX()+num5,Horse5.getY());
	}										   

	/**
	* @param args the command line arguments
	*/
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new HorseRace().setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify
	private javax.swing.JLabel Horse1;
	private javax.swing.JLabel Horse2;
	private javax.swing.JLabel Horse3;
	private javax.swing.JLabel Horse4;
	private javax.swing.JLabel Horse5;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JButton startButton;
	private javax.swing.JLabel timeLabel;
	// End of variables declaration

}






Is This A Good Question/Topic? 0
  • +

Replies To: moving a label on an applet

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: moving a label on an applet

Posted 03 March 2009 - 08:57 PM

There's a nice easy method that lets you change the location of a component...

whatever_label.setLocation(x_position, y_position);


To move the horse when they press the button, some sort of running position of where it is will be needed, and you'll be able to change the position according to where it was before.

This post has been edited by Locke: 03 March 2009 - 08:58 PM

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: moving a label on an applet

Posted 03 March 2009 - 09:23 PM

View PostLocke, on 3 Mar, 2009 - 07:57 PM, said:

There's a nice easy method that lets you change the location of a component...

whatever_label.setLocation(x_position, y_position);


To move the horse when they press the button, some sort of running position of where it is will be needed, and you'll be able to change the position according to where it was before.

Yha... but the catch is: this only work with Null Layout in this GUI was generated by Netbeans
If you want to do fancy stuff with a GUI do not use the GUI generator of an IDE

You have the choice between 2 worlds:
- or you do it yourself and do whatever you want with it
- or you use a GUI generator and stay with the look and feel and layout you created it with
Was This Post Helpful? 0
  • +
  • -

#4 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: moving a label on an applet

Posted 03 March 2009 - 09:24 PM

I thought NetBeans did use a null layout. Hm...

(if NetBeans doesn't use a null layout) I could have sworn it works otherwise anyways, because I've used it to override NetBeans when it decides to put components other than places I specified...

-_-

This post has been edited by Locke: 03 March 2009 - 09:25 PM

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: moving a label on an applet

Posted 03 March 2009 - 09:55 PM

View PostLocke, on 3 Mar, 2009 - 08:24 PM, said:

I thought NetBeans did use a null layout. Hm...

(if NetBeans doesn't use a null layout) I could have sworn it works otherwise anyways, because I've used it to override NetBeans when it decides to put components other than places I specified...

-_-

Seems to use a GroupLayout
one of these layout introduced in JRE 1.6 for GUI generators but completly understandable for human beings

(Even if you and me can understand nested if up to 16 levels of depth B) )
Was This Post Helpful? 0
  • +
  • -

#6 stutfly  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 30-August 08

Re: moving a label on an applet

Posted 03 March 2009 - 10:05 PM

So in other words I cant do it like this

View Postpbl, on 3 Mar, 2009 - 08:55 PM, said:

View PostLocke, on 3 Mar, 2009 - 08:24 PM, said:

I thought NetBeans did use a null layout. Hm...

(if NetBeans doesn't use a null layout) I could have sworn it works otherwise anyways, because I've used it to override NetBeans when it decides to put components other than places I specified...

-_-

Seems to use a GroupLayout
one of these layout introduced in JRE 1.6 for GUI generators but completly understandable for human beings

(Even if you and me can understand nested if up to 16 levels of depth B) )

Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: moving a label on an applet

Posted 03 March 2009 - 10:16 PM

View Poststutfly, on 3 Mar, 2009 - 09:05 PM, said:

So in other words I cant do it like this

View Postpbl, on 3 Mar, 2009 - 08:55 PM, said:

View PostLocke, on 3 Mar, 2009 - 08:24 PM, said:

I thought NetBeans did use a null layout. Hm...

(if NetBeans doesn't use a null layout) I could have sworn it works otherwise anyways, because I've used it to override NetBeans when it decides to put components other than places I specified...

-_-

Seems to use a GroupLayout
one of these layout introduced in JRE 1.6 for GUI generators but completly understandable for human beings

(Even if you and me can understand nested if up to 16 levels of depth B) )


You can do it this way.
But you can't expect us to help you if you use an IDE to generate your GUI.

When you use a GUI generator form an IDE like NetBEans it prompts you for a lot of options, or provide you with a lot of Checkbox to check/uncheck... should it be transparent, should it be over the other components, should it be opaque, .... and so on

GUI generators generates hundreds of line of code that can be hardly understood by a human being

If you write your own GUI we will be happy to help
if you based your GUI on a GUI generator, unless you pay me a reasonnable hour fee I won't even have a look at it... and you will probably be wasting your money anyhow
May be the GUI generator offered you a few checkbox to check/uncheck and you didn't choose the right ones

If you have problems related to GUI generated by NetBeans go to the NetBeans forum
we fix human generated code not IDE generated code

And you are still welcome here
Was This Post Helpful? 0
  • +
  • -

#8 stutfly  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 30-August 08

Re: moving a label on an applet

Posted 03 March 2009 - 10:31 PM

O I get it and completely understand

so I will throw some of my own code ut now what I want to do is
move the labels so could I do something like

Horse1.setLocation(Horse1.getX()+num,Horse1.getY());



with a for loop to keep doing this until it reaches a certain place on my applet

View Postpbl, on 3 Mar, 2009 - 09:16 PM, said:

View Poststutfly, on 3 Mar, 2009 - 09:05 PM, said:

So in other words I cant do it like this

View Postpbl, on 3 Mar, 2009 - 08:55 PM, said:

View PostLocke, on 3 Mar, 2009 - 08:24 PM, said:

I thought NetBeans did use a null layout. Hm...

(if NetBeans doesn't use a null layout) I could have sworn it works otherwise anyways, because I've used it to override NetBeans when it decides to put components other than places I specified...

-_-

Seems to use a GroupLayout
one of these layout introduced in JRE 1.6 for GUI generators but completly understandable for human beings

(Even if you and me can understand nested if up to 16 levels of depth B) )


You can do it this way.
But you can't expect us to help you if you use an IDE to generate your GUI.

When you use a GUI generator form an IDE like NetBEans it prompts you for a lot of options, or provide you with a lot of Checkbox to check/uncheck... should it be transparent, should it be over the other components, should it be opaque, .... and so on

GUI generators generates hundreds of line of code that can be hardly understood by a human being

If you write your own GUI we will be happy to help
if you based your GUI on a GUI generator, unless you pay me a reasonnable hour fee I won't even have a look at it... and you will probably be wasting your money anyhow
May be the GUI generator offered you a few checkbox to check/uncheck and you didn't choose the right ones

If you have problems related to GUI generated by NetBeans go to the NetBeans forum
we fix human generated code not IDE generated code

And you are still welcome here

Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: moving a label on an applet

Posted 03 March 2009 - 10:41 PM

View Poststutfly, on 3 Mar, 2009 - 09:31 PM, said:

O I get it and completely understand

so I will throw some of my own code ut now what I want to do is
move the labels so could I do something like

Horse1.setLocation(Horse1.getX()+num,Horse1.getY());



with a for loop to keep doing this until it reaches a certain place on my applet


In a null layout it will work... you will also have to specify the size of the JLabel the GUI cannot just guess what it should be
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1