dragster
setup Form
- set Opaque property to TRUE to see background colours
set Label Mouse Click Event
- go to the events panel, find the
mouseClicked
event, click on the drop down box to add an event name (this will make it a clickable label) as show here:
import Packages
- goes at very top of Source code lines 1-5 so either get rid of the comments that are there or move them down
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.Timer;
public Variables
- goes just inside the whole public class jFrame declaration, but still appears before the constructor. FYI the constructor is the public procedure that has the
initComponents()
method in it that loads when the program starts... hence the word 'constructor'
public Timer animateCar = null;
public Point carLocation = null;
jFrame Constructor
- put this on the next line available directly underneath the line of code that has
initComponents()
on it:
ActionListener movePixels = (ActionEvent meh) -> {
carLocation = lblCar.getLocation();
int x = (int)carLocation.getX();
int y = (int)carLocation.getY();
lblCar.setLocation(x+1, y);
if(x>100){
animateCar.stop();
JOptionPane.showMessageDialog(null, "winner");
}
};
animateCar = new Timer(10, movePixels);
lblGo (mouseClicked)
animateCar.start();