Reputation: 127
I'm new in Java learning, I was asked to develop a GUI application that simulates four cars racing, You can set the speed for each car. and the following is my code, it can run but can't display the picture. I don't know much about where should I position my picture (In JPanel or JFrame). could someone give me some help to finish the project? Thanks in advance.
import JavaFX.scene.canvas.GraphicsContext;
import JavaFX.scene.image.ImageView;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Main {
static class program extends JFrame {
//create two JPanel objects.
JPanel topRow = new JPanel();
JPanel lowRow = new JPanel() {
public void paint(Graphics graphics) {
//draw a graph
graphics.drawRect(5, 25, 490, 240);
graphics.drawLine(5, 85, 495, 85);
graphics.drawLine(5, 145, 495, 145);
graphics.drawLine(5, 205, 495, 205);
}
};
private JTextField carped_a = new JTextField();
String s1 = carped_a.getText();//there I want to change this to the //car's speed
private JTextField carped_b = new JTextField();
String s2 = carped_b.getText();
private JTextField carped_c = new JTextField();
String s3 = carped_c.getText();
private JTextField carped_d = new JTextField();
String s4 = carped_d.getText();
program() {
super("program");
setLookAndFeel();
setResizable(false);
setSize(515, 330);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
BorderLayout layout = new BorderLayout();
setLayout(layout);
topRow.setBounds(5, 5, 490, 40);
GridLayout grid = new GridLayout(1, 4, 20, 10);
topRow.setLayout(grid);
JLabel carLabel_a = new JLabel("car 1: ", JLabel.RIGHT);
topRow.add(carLabel_a);
carped_a.setEditable(true);
topRow.add(carped_a);
JLabel carLabel_b = new JLabel("car 2:", JLabel.RIGHT);
topRow.add(carLabel_b);
carped_b.setEditable(true);
topRow.add(carped_b);
JLabel carLabel_c = new JLabel("car 3:", JLabel.RIGHT);
topRow.add(carLabel_c);
carped_c.setEditable(true);
topRow.add(carped_c);
JLabel carLabel_d = new JLabel("car 4:", JLabel.RIGHT);
topRow.add(carLabel_d);
carped_d.setEditable(true);
topRow.add(carped_d);
add(topRow, BorderLayout.NORTH);
add(lowRow, BorderLayout.CENTER);
}
private void setLookAndFeel() {
try {
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception exc) {
// ignore error
}
}
}
static class Car extends JPanel{
JLabel carLabel =new JLabel();
Car(int a, int b) {
carLabel.setBounds(a,b,50,40);
this.add(carLabel);
ImageIcon img=new ImageIcon("car.jpg");
carLabel.setIcon(img);
carLabel.setText(null);
}
}
public static void main(String[] args) {
program program1= new program();
Car car1= new Car(5,45);
}
}
I'm using IntelliJ IDEA.
Upvotes: 0
Views: 25
Reputation: 6808
First of all, you create a Car
panel but you never add it to the frame:
public static void main(String[] args) {
program program1 = new program();
Car car1 = new Car(5, 45);
program1.add(car1);
}
Secondly, the image won't shown because it is not in the correct path. When you say new JLabel("myimage.png");
, it is expected to have the image into project's main directory. However, this way your application will not be portable (actually it will but you will have to take the whole project with it). In order to make it independent and portable read this question.
Upvotes: 1