|
This 18 message thread spans 2 pages: [1] 2 > >
|
|
Hello, Been trying to learn abit of Java through Online Examples and one of my friends at university, but by working backwards, by looking at a program see how its made then attempting it myself, My current project is a Moon Lander of which i got the idea from My friend at uni who has just finished a similar project.
The program needs to
Display a message, which welcomes the user to the program and asks them to log in, Login Name, Password Etc before someone can use the program.
I Want to include a form which allows the user to enter there name, job and mission number which updates each time the program is run.
The “Mission Information” form should include a suitable logo, e.g. NASA, a picture of the moon, or a spaceship.
I'd like the program to display a representation of the Explorer travelling towards the surface, and also allow the user to monitor the vehicle and control its speed, and allow the user to input relevant information such as space and surface conditions. Some sort of basic graphical representation must be included. It must be updated at least once per second after the program starts running.
· The application must display the following information relating to the vehicle:-
Current conditions on the planet surface. (Is it dark or light? What is the surface temperature?)
Current speed relative to the moon surface (updated every second).
Distance from the planet surface (updated every second)
Time left (in seconds) before the vehicle meets the ground
A visual indicator showing the current position of ship and its progress towards the proposed landing site.
If anyone could help me with this it would be great, Thanks.
|
|
|
By the way, im using a program called Netbeans. Also if i want to add a Logo on a jframe form is this what i do?
public void handleButton(java.awt.event.ActionEvent evt) {
Icon icon = new ImageIcon(getClass().getResource(evt.getActionCommand() +"/forum/.gif"));
lblLogo.setIcon(icon);
|
|
|
Also How do i create a Welcome message to a user and then ask them to input a designated password such as "Hello12345" before accessing the program?
|
|
|
You can find more information and examples in the Swing tutorial on pages like: Lesson: Using Swing Components and Lesson: Laying Out Components Within a Container.
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class TestApp implements ActionListener {
JTextField textField;
public void actionPerformed(ActionEvent e) {
JButton button = (JButton)e.getSource();
String ac = button.getActionCommand();
if(ac.equals("log in"))
System.out.println("logging in..." + textField.getText());
// Verify name and password. If okay, you can
// show the next panel in a CardLayout. This
// next panel can have your display animation
// and controls on it.
}
private JPanel getContent() {
textField = new JTextField(12);
JButton logIn = new JButton("log in");
logIn.setActionCommand("log in");
logIn.addActionListener(this);
JPanel p = new JPanel();
p.add(new JLabel("name:"));
p.add(textField);
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JLabel("sign in", JLabel.CENTER), "North");
panel.add(p, "Center");
p = new JPanel();
p.add(logIn);
panel.add(p, "South");
return panel;
}
public static void main(String[] args) throws IOException {
// Best to load your images at start-up. You can keep them
// in arrays or save a reference to them in member variables.
BufferedImage duke = ImageIO.read(new File("/forum/images/duke.gif"));
TestApp testApp = new TestApp();
JFrame f = new JFrame();
f.setIconImage(duke);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(testApp.getContent(), "Center");
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}
|
|
|
|
|
Hello, Thanks for the help on the login, seems abit clearer to me now. Read some of the Java books and sites, but they kind of assume i know all the phrases and such, which i dont, Netbeans is quite easy to use and does quite abit for you you see. When it comes to the Representation of the Lander, Leaving Earth, such as a large Blue Circle, heading to the moon a Large Grey circle n the lander being a small Read Circle for example, how do i go about this? espesially when it comes to it being update every second and implementating a slider etc to control speeed? im finding it quite difficult to even start this part. Any help would be much appreciated.
Thanks
|
|
|
Another resource in the Swing tutorial: Lesson: Performing Custom Painting. This is the starting point for learning to do custom graphics in java. For follow-up there is Trail: 2D Graphics. Also see Programmer's Guide to the JavaTM 2D API in your javadocs folder: 1.5 docs/guide/2d/spec/j2d-bookTOC.html or online if you have not downloaded the javadocs.
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.text.NumberFormat;
import javax.swing.*;
import javax.swing.event.*;
public class TheJourney extends JPanel {
JLabel distanceLabel;
NumberFormat nf;
Ellipse2D earth;
Ellipse2D moon;
Ellipse2D ship;
Line2D travelPath;
double distance = 0;
double speed = 1.0;
public TheJourney() {
nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(1);
}
public boolean moveAhead() {
double tripDistance = travelPath.getP1().distance(travelPath.getP2());
if(distance + speed < tripDistance) {
double x = ship.getCenterX() + speed;
double y = ship.getCenterY();
double w = x + ship.getWidth()/2;
double h = y + ship.getHeight()/2;
ship.setFrameFromCenter(x, y, w, h);
distance += speed;
distanceLabel.setText(nf.format(distance));
repaint();
return false;
}
return true;
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
if(earth == null)
init();
g2.setPaint(Color.blue);
g2.fill(earth);
g2.setPaint(Color.lightGray);
g2.fill(moon);
g2.setPaint(Color.pink);
g2.draw(travelPath);
g2.setPaint(Color.red);
g2.fill(ship);
}
private void init() {
int w = getWidth();
int h = getHeight();
double d = Math.min(w,h)/12.0;
double s = d/2.0;
earth = new Ellipse2D.Double(w/16, h/2-d, 2*d, 2*d);
moon = new Ellipse2D.Double(w-d-w/16, h/2-d/2, d, d);
ship = new Ellipse2D.Double(w/16+2*d, h/2-s/2, s, s);
double x1 = ship.getCenterX();
double y1 = ship.getCenterY();
double x2 = moon.getCenterX() - moon.getWidth()/2 - ship.getWidth()/2;
double y2 = moon.getCenterY();
travelPath = new Line2D.Double(x1, y1, x2, y2);
}
private JPanel getUIPanel(ShipController controller) {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = GridBagConstraints.REMAINDER;
panel.add(getControls(controller), gbc);
panel.add(getDataPanel(), gbc);
return panel;
}
private JPanel getControls(final ShipController controller) {
JSlider slider = new JSlider(40, 100, 60);
slider.setPaintLabels(true);
slider.setMajorTickSpacing(10);
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
int value = ((JSlider)e.getSource()).getValue();
controller.setDelay(value);
}
});
final JButton start = new JButton("start");
final JButton stop = new JButton("stop");
ActionListener l = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton button = (JButton)e.getSource();
if(button == start)
controller.start();
if(button == stop)
controller.stop();
}
};
start.addActionListener(l);
stop.addActionListener(l);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 2;
panel.add(slider, gbc);
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.fill = GridBagConstraints.NONE;
panel.add(start, gbc);
panel.add(stop, gbc);
return panel;
}
private JPanel getDataPanel() {
distanceLabel = new JLabel(" ");
JPanel panel = new JPanel();
panel.add(new JLabel("distance traveled: "));
panel.add(distanceLabel);
return panel;
}
public static void main(String[] args) {
TheJourney journey = new TheJourney();
ShipController controller = new ShipController(journey);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(journey);
f.getContentPane().add(journey.getUIPanel(controller), "Last");
f.setSize(500,500);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
class ShipController implements Runnable {
TheJourney theJourney;
Thread thread = null;
boolean traveling = false;
int delay = 50;
public ShipController(TheJourney tj) {
theJourney = tj;
}
public void setDelay(int delay) {
this.delay = delay;
}
public void run() {
while(traveling) {
try {
Thread.sleep(delay);
} catch(InterruptedException e) {
stop();
}
boolean hasLanded = theJourney.moveAhead();
if(hasLanded)
stop();
}
}
public void start() {
if(!traveling) {
traveling = true;
thread = new Thread(this);
thread.setPriority(Thread.NORM_PRIORITY);
thread.start();
}
}
public void stop() {
traveling = false;
if(thread != null)
thread.interrupt();
thread = null;
}
}
|
|
|
|
|
Once again thanks for the help.
Now When the program runs a login message is displayed, they need to login with the currect password, then i want a form with there details to come up, how can i do this? For example i have 3 users that can login to the system, each with a different password, once they log in i want only there details to display, they can also change and clear there information if they wish to. Once theyve looked at there details how do i create a button that will open the Representatioon of the lander, or do you think i somehow make a menu screen which disply after login in, so then they can go to details, mission information or the Representation?
Any help would be appreciated.
<Added>
Also Perhaps have they data they input about them selves or about a mission to be saved to an external, but portable txt document?
|
|
|
By the way on that last piece of code, i copied it in exactly to see how it would run but i get:
Compiling 1 source file to C:\Documents and Settings\Owner\Desktop\JavaApplication27\build\classes
C:\Documents and Settings\Owner\Desktop\JavaApplication27\src\TheJourney.java:196: 'class' or 'interface' expected
private void initComponents() {
1 error
BUILD FAILED (total time: 1 second)
And this line Highlightedin red : private void initComponents() {
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 300, Short.MAX_VALUE)
);
}
// </editor-fold>
<Added>
P.S I copied the code into a Jpanel Form, in the Source area, Was this correct?
<Added>
Sorry Stupid Mistkae i put it into wrong area, Sorry. if you can help me out on my other question about some sort of menu or puttin this all together that would be great. Thanks alot. Those links you gave me are helping aswell.
|
|
|
Hello, i cant quite get the login in to work aswell says there is no main mehthod?
How would i use Netbeans to construct this login myself then enter some bits of code? or if you could just show me a basic login screen, or maybe just a dialog box welcoming the user then requesting a passowrd that would be fine, even if its just one password to access the program. just really need to see a basic login work. Any help would be appreciated.
|
|
|
Hello, Sorry for bomb barding with replies, but im actually getting somewhere on this today and geting quite into it. Been jotting down ideas for Code and things to add the things ive come up with to implment are:
Every time the progam is run, it says whether the moon is Dark or light, Randomly. I can select dark or light in a combo box but id like it to be random, how is this done?
Distance From the Moon.
Time Left before Landing.
However the main thing id like is that if the Vehcicle goes to fast, it starts to move out of a straight line and mayb possibly even miss the landing zone i'd somehow like an error message to come up if the Lander goes to far out of line or misses the Moon. is this something you could help with? I'm finding this quite interesting now the more i do, but my Noobie efforts dont always do it justice.
<Added>
Got the Login Screen to work, Had to delete information relating to loading an image. Instead of asking for a name, instead is it possible to ask for a common password such as Hello, But show the user this to the user as Asterix **** so Passoword is hello. and then can allow access to the user or to popup menu, directing them to details mission imformaiton or the Lander display. Bit stumped at the moment.
|
|
|
Back on the Lesson: Using Swing Components page, look down in the left-hand column for the link [u]How to Use Password Fields[/u] and see where that takes you. and then can allow access to the user or to popup menu, directing them to details mission imformaiton or the Lander display
So many things are possible. You just have to figure out (imagine) how you want your app to work (with user input/interaction) and then try to put it togeter. Dialogs are a possibility. CardLayout is another. However the main thing id like is that if the Vehcicle goes to fast, it starts to move out of a straight line and mayb possibly even miss the landing zone i'd somehow like an error message to come up if the Lander goes to far out of line or misses the Moon. is this something you could help with?
Possibly. "Going too fast ... moving out of straight-line..."
With the TheJourney app animation? Or something else? Need more information.
|
|
|
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LogInTest implements ActionListener {
JTextField textField;
JPasswordField passField;
JPanel contentPanel;
public void actionPerformed(ActionEvent e) {
JButton button = (JButton)e.getSource();
String ac = button.getActionCommand();
if(ac.equals("LOG_IN")) {
String name = textField.getText();
char[] chars = passField.getPassword();
String passWord = "";
if(chars != null) {
passWord = String.valueOf(chars);
for(int j = 0; j < chars.length; j++)
chars[j] = '0';
} else {
System.out.println("oops");
return;
}
if(name.equals("John") && passWord.equals("hello")) {
System.out.println("hello " + name);
CardLayout cards = (CardLayout)contentPanel.getLayout();
cards.next(contentPanel);
}
}
}
private JPanel getContent() {
contentPanel = new JPanel(new CardLayout());
contentPanel.add("logIn", getLogInPanel());
contentPanel.add("journey", new TheJourney());
return contentPanel;
}
private JPanel getLogInPanel() {
textField = new JTextField(12);
passField = new JPasswordField(12);
JButton logIn = new JButton("log in");
logIn.setActionCommand("LOG_IN");
logIn.addActionListener(this);
JPanel panel = new JPanel(new GridBagLayout());
panel.setBorder(BorderFactory.createTitledBorder("Log In"));
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(2,2,2,2);
gbc.gridwidth = GridBagConstraints.RELATIVE;
panel.add(new JLabel("User Name:"), gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
panel.add(textField, gbc);
gbc.gridwidth = GridBagConstraints.RELATIVE;
panel.add(new JLabel("Password:"), gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
panel.add(passField, gbc);
gbc.gridwidth = 2;
panel.add(logIn, gbc);
return panel;
}
public static void main(String[] args) {
LogInTest test = new LogInTest();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(test.getContent());
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}
|
|
Distance From the Moon
In the TheJourney class the member variable distance holds the total distance traveled from earth toward the moon. In the moveAhead method, first line, the total distance from earth to the moon is calculated and saved as the local variable tripDistance. The distance remaining from the current position to the destination is the difference in the values held by these two variables. Time Left before Landing
Consider this:
distance = velocity * time
velocity = pixelsPerFrame * framesPerSecond
framesPerSecond = 1000 / delay
velocity = speed * delay/1000
time = distanceRemaining / velocity
|
|
|
Thanks for the help and the suggestions.
Basically Mean that When the Lander Leaves Earth, and is for example a few metres away from the moon, if it is going a high speed of X, then the Lander, Moves away from the Line because its going to fast and then display an error message saying its off course. If that makes sense? (a possible implementation would be that when going to fast it leaves the line, but when lowering the speed it reverts bk to the line/Course to the Moon)
Also what i mean is once ive done the representation of the Lander leaving earth to the moon, Mission information and user details for example have been completed, how do i combine these into one Main screen which opens up the relevant thing, like a switchboard in access basically. Ive never used cardlayout before, but ill take a look.
I'll take a look at the links you gave me again tho.
Thanks, once again suggestions and help would be much apprecaited.
|
|
|
Also what i mean is once ive done the representation of the Lander leaving earth to the moon, Mission information and user details for example have been completed, how do i combine these into one Main screen which opens up the relevant thing, like a switchboard in access basically.
I tried to demonstrate this in the post above that has the LogInTest class.
Basically Mean that When the Lander Leaves Earth, and is for example a few metres away from the moon, if it is going a high speed of X, then the Lander, Moves away from the Line because its going to fast and then display an error message saying its off course. If that makes sense? (a possible implementation would be that when going to fast it leaves the line, but when lowering the speed it reverts bk to the line/Course to the Moon)
The implementation of the space ship traveling along the line of flight given in the TheJourney class will not do this. In other words, the ship will not deviate from the flight path at higher speed (I tried at delay = 10, no problem). So you must be talking about something else, viz, some other way of the ship traveling on a line to the moon.
Every time the progam is run, it says whether the moon is Dark or light, Randomly. I can select dark or light in a combo box but id like it to be random, how is this done?
...
import java.util.Random;
public class TheJourney ...
Random seed = new Random();
...
protected void paintComponent(Graphics g) {
...
Color c = seed.nextBoolean() ? Color.black : Color.lightGray;
g2.setPaint(c);
g2.fill(moon);
|
|
|
|
|
How would you suggest that the ship deviates when going to fast? Would it have to be some sort of manual system where the user controls the speed and direction of the Lander via the Slider for Speed and up, down, left, right controls buttons display on the form/panel? Each button acting as a sort of Thruster?
Then display a message if the lander reaches the moon showing that of success or if it missess it one failure. I know this is possible in java but as i say im not well versed in the Language so simplicity is the key really.
once again, thanks.
|
|
This 18 message thread spans 2 pages: [1] 2 > > |
|
|
|
|
|