Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: CAR PARK SIMULATOR

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    6
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up CAR PARK SIMULATOR

    Hi, im currently struggling with my java coding and as you can see from my work it isn't very good haha. i'm currently trying to add images to my jlabels once someone has entered there details for a Car, Lorry or Coach, i was wondering if anyone could help me with it.

    p.s if anyone want's to do the whole project i will pay you haha

    ICA2_MEMORY .zip


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,125
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: CAR PARK SIMULATOR

    Can you post the code here that you are having problems with?
    If it has too many lines (say over 250), make a small, complete program that compiles, executes and shows the problem you are having.
    add images to my jlabels
    If that is the problem, then code to do that is all that is needed in the posted code.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,803
    My Mood
    Mellow
    Thanks
    236
    Thanked 717 Times in 703 Posts

    Default Re: CAR PARK SIMULATOR

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Post the code you need help with between code tags as described in the link I provided above. If you're getting errors, post those. If you're just lost, don't know what to do, then describe why you're confused. Post some code and ask specific questions, then we can help.

    There is an area here to post advertisements for paid work, but most here will not participate in academic dishonesty.

    I did recently post an example of a single-thread pizza parlor simulation that you might find helpful, but I'm not sure since I haven't reviewed your assignment.

    Good luck.

  4. #4
    Junior Member
    Join Date
    Apr 2014
    Posts
    6
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CAR PARK SIMULATOR

    sorry, its not that i'm getting errors it because i'm finding it impossible as a beginner at Java to add images to my Jlabels when someone enters details, i may of posted it in the wrong section, can i forward it?

  5. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    6
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CAR PARK SIMULATOR

    COM1077-N ICA 2 (2013-14)(1).pdf

    Here's my brief if you find this helpful?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,125
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: CAR PARK SIMULATOR

    Which part are you having problems with:
    1)add images to my Jlabels
    2) detecting when someone enters details

    Post code and or questions here on the forum not links.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2014
    Posts
    6
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CAR PARK SIMULATOR

    sorry i won't post anything else, i thought it would be better to attach my project.
    These are my problems:

    1. When some presses OK on the JoptionPane after entering the details for the Car/Lorry/Coach, an image is suppose to appear onto the JLabels for the CarPark which i don't know how to do.

    2. as you can see on my document i need add functions to the rest of my buttons which are "ClearAll, Save, Load, CurrentTotal and OverallTotal

    if you or someone could help me with this it would be much appreciated, if someone wants paying to do this. give me a price and i will happily pay.

    Thank you for your help Norm

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,125
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: CAR PARK SIMULATOR

    Sorry, I don't follow links. If it isn't posted on the thread I haven't read it.

    Are you having problems adding an image to a JLabel? Post the code that you have written that tries to do that.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Apr 2014
    Posts
    6
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CAR PARK SIMULATOR

    i've attached my code in the thread.

    i've got a class for each vehicle which has a method (the code shown is in the car class)
    public ImageIcon GetImage(){
        ImageIcon car = new ImageIcon ("images/car.jpg");
            return car;

    then in my GUI class i have this for when someone wants to enter the details for the car:
    if (me.getSource() == buttons[0]) {                     //"Add Car Button Click"
     
            JPanel addCarPanel = new JPanel();
            addCarPanel.setLayout(new GridLayout(4, 2));
     
            addCarPanel.add(new JLabel("Reg no."));
            JTextField regNumField = new JTextField(8);
            addCarPanel.add(regNumField);
     
            addCarPanel.add(new JLabel("Length. "));
            JTextField lengthField = new JTextField(3);
            addCarPanel.add(lengthField);
     
            JCheckBox disabledBadge = new JCheckBox("");
            addCarPanel.add(new JLabel("Disabled Badge  "));
            addCarPanel.add(disabledBadge);
     
            addCarPanel.add(new JLabel("Number Of Hours "));
            JTextField hours = new JTextField(3);
            addCarPanel.add(hours);
     
            int optionCar = JOptionPane.showConfirmDialog(this, addCarPanel, "Add Car", JOptionPane.OK_CANCEL_OPTION);
     
            if (optionCar == JOptionPane.OK_OPTION) {
                System.out.println("Reg num entered: " + regNumField.getText());
                System.out.println("Length of Car: " + lengthField.getText());
                System.out.println("Disabled Badge: " + disabledBadge.isSelected());
                System.out.println("Number of hours: " + hours.getText());
                //CarLabels[] = new ImageIcon("images/car.jpg");
                //Carlabels.setIcon(car);
     
                double intLength = Double.parseDouble(lengthField.getText());
                int h = Integer.parseInt(hours.getText());
     
     
                Car CarNew = new Car(regNumField.getText(), intLength, disabledBadge.isSelected(), h);
            }

    this is where i am stuck ??

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,125
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: CAR PARK SIMULATOR

    Where is the getImage() method called? If it is not called it won't get the ImageIcon.

    This small disconnected piece of code won't be of any use fixing the problem. There needs to be a small, complete program made that compiles, executes and shows the problem. Trim out everything but one JLabel and set its image.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Apr 2014
    Posts
    6
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CAR PARK SIMULATOR

    Vehicle SuperClass
    package ica2;
     
    import javax.swing.ImageIcon;
     
    /**
     *
     * @author Philip Martin
     */
    public abstract class Vehicle {
     
        protected double charge;
        protected String regNumber;
     
     
        public Vehicle(){
           charge = 0.0;
           regNumber = "";
        }
     
        public Vehicle(String regNumber) {
            this.regNumber = regNumber;
        }
     
     
        public double getCharge() {
            return charge;
        }
     
        public abstract ImageIcon GetImage(); 
     
        public abstract double CalcCharge();
     
     
        @Override
        public String toString() {
            return "Vehicle{" + "toString=" + + '}';
        }
     
     
    }

    Car
    package ica2;
     
    import javax.swing.ImageIcon;
     
    /**
     * * @author Philip Martin
     */
    public class Car extends Vehicle {
     
        private double length;
        private boolean disabledBadge;
        private int numOfHours;
     
        public Car() {
            length = 0.0;
            disabledBadge = true;
            numOfHours = 0;
        }
     
        public Car(String rn, double length, boolean disabledBadge, int numOfHours) {
            super(rn);
            this.length = length;
            this.disabledBadge = disabledBadge;
            this.numOfHours = numOfHours;
        }
     
        public double getLength() {
            return length;
        }
     
        public ImageIcon GetImage(){
        ImageIcon car = new ImageIcon ("images/car.jpg");
            return car;
    }
     
        @Override
        public double CalcCharge(){
        if (length <= 6){
                charge = 1.0 * numOfHours;
        }
        else if (length > 6){
                charge = 1.5 * numOfHours;
        }
        else charge = 0;
            return charge;
        }
     
        public String toString() {
            return "Vehicle{" + "toString=" + + '}';
        }
     
    }

    Lorry
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package ica2;
     
    import javax.swing.ImageIcon;
     
    /**
     *
     * @author n3098463
     */
    public class Lorry extends Vehicle {
        private double weight;
        private int numOfDays;
     
        public Lorry() {
           weight = 0.0;
           numOfDays = 0;
        }
     
        public Lorry(String rn, double weight, int numOfDays) {
            super(rn);
            this.weight = weight;
            this.numOfDays = numOfDays;
        }
     
        public double getWeight() {
            return weight;
        }
     
          public ImageIcon GetImage(){
        ImageIcon lorry = new ImageIcon ("lorry.png");
            return lorry;
    }
     
        public double CalcCharge(){
        if (weight < 20.0 ){
                charge = 5.0 * numOfDays;
        }
        else if (weight >= 20.0 && weight < 35.0){
                charge = 8.0 * numOfDays;
        }
        else if (weight >= 35.0)
                charge = 0.0;
                numOfDays = 0;
                System.out.println("Sorry You Cannot Enter The CarPark");
            return charge;
    }
     
     
        @Override
        public String toString() {
            return "Vehicle{" + "toString=" +  + '}';
        }
     
    }

    Coach
    package ica2;
     
    import javax.swing.ImageIcon;
     
    /**
     * @author Philip Martin
     */
    public class Coach extends Vehicle {
     
        private int numOfPassengers;
        private boolean touristOperator;
     
        public Coach() {
        numOfPassengers = 0;
        touristOperator = false; 
        }
     
        public Coach(String rn, int numOfPassengers, boolean touristOperator) {
            super(rn);
            this.numOfPassengers = numOfPassengers;
            this.touristOperator = touristOperator;
        }
     
          public ImageIcon GetImage(){
        ImageIcon coach = new ImageIcon ("coach.png");
            return coach;
    }
     
        public double CalcCharge(){
         if (numOfPassengers <= 20){
             charge = 4.5;
        }
        else if (numOfPassengers > 20){
                charge = 6.0;
        }
         if (touristOperator = true){
             charge = charge * 0.9;
         }
         return charge;
    }
     
        @Override
        public String toString() {
            return "Vehicle{" + "toString=" +  + '}';
        }
     
    }

    GUI
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package ica2;
     
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.*;
    import java.awt.Dimension;
    import java.awt.Frame;
    import java.awt.TextField;
    import java.util.Vector;
    import javax.swing.border.Border;
    import javax.swing.JOptionPane;
     
    /**
     *
     * @author n3098463
     */
    public class GUI extends JPanel implements MouseListener {
     
        JPanel southPark;
        JPanel northPark;
        JPanel btns;
        JPanel carPark;
        JButton addCar;
        JLabel CarSpace;
        JLabel LorrySpace;
        int i;
        int L;
        int C;
        int c;
        String b[] = {"Add Car", "Add Lorry", "Add Coach", "Clear All", "Save", "Load", "Current Total", "Total for day"};
        JButton[] buttons = new JButton[8];
     
        GUI() {
            //*******************************************Vector ********************************************************
     
            //*********************************************BUTTON PANEL**************************************************
     
            btns = new JPanel();
     
            btns.setLayout(new GridLayout(8, 1));
            btns.setVisible(true);
     
            btns.addMouseListener(this);
     
            //btns.setPreferredSize(new Dimension(150, 200));
            //(int width, int height) 
     
     
            //**************************************ADDING THE BUTTONS ON THE PANEL***************************************
     
            for (i = 0; i < buttons.length; i++) {
                buttons[i] = new JButton(b[i]);
                buttons[i].setPreferredSize(new Dimension(200, 70));
                btns.add(buttons[i]);
                buttons[i].addMouseListener(this);
            }
     
            //***************************************CAR PARK PANEL*****************************************************
     
            carPark = new JPanel();
            carPark.setVisible(true);
            //this.add(carPark, BorderLayout.CENTER);
     
            carPark.setLayout(new BorderLayout());
     
            //****************************************NORTH PANEL (LORRYS & COACH)***************************************
            northPark = new JPanel();
            northPark.setVisible(true);                         //900,790
            carPark.add(northPark, BorderLayout.NORTH);
            northPark.setPreferredSize(new Dimension(600, 260));
            //northPark.setBackground(Color.RED);
     
            northPark.setLayout(new GridLayout(1, 4));
     
            //************************************* ARRAY FOR JLABELS LORRYS *******************************************
            JLabel[] LorrySpace = new JLabel[4];
     
            // String lS[] = {"1", "2", "3", "4"};
     
            for (L = 0; L < LorrySpace.length; L++) {
                LorrySpace[L] = new JLabel();
                Border borderLorry = BorderFactory.createLineBorder(Color.RED, 2);
                LorrySpace[L].setBorder(borderLorry);
                LorrySpace[L].setPreferredSize(new Dimension(75, 270));
                northPark.add(LorrySpace[L]);
            }
     
            //*************************************SOUTH PANEL (CARS) **************************************************
     
            southPark = new JPanel();
            southPark.setVisible(true);
            carPark.add(southPark, BorderLayout.SOUTH);
            //southPark.setPreferredSize(new Dimension(75, 100));
            //southPark.setBackground(Color.GREEN);
     
            southPark.setLayout(new GridLayout(3, 4));
     
            //****************************************  ARRAY FOR JLABELS CARS *****************************************
     
            JLabel[] CarLabels = new JLabel[12];
     
            //String cS[] = {"1", "2", "3", "4", "5" , "6" , "7" , "8" , "9" , "10" , "11" , "12" };
     
            for (C = 0; C < CarLabels.length; C++) {
     
                CarLabels[C] = new JLabel();
     
                Border border = BorderFactory.createLineBorder(Color.RED, 2);
     
                CarLabels[C].setBorder(border);
     
                CarLabels[C].setPreferredSize(new Dimension(75, 100));
     
                southPark.add(CarLabels[C]);
            }
            //************************************************************************************************************
     
            this.add(btns, BorderLayout.WEST);
            this.add(carPark);
        }
     
        public void mouseEntered(MouseEvent me) {
        }
     
        public void mousePressed(MouseEvent me) {
            if (me.getSource() == buttons[0]) {                     //"Add Car Button Click"
     
            JPanel addCarPanel = new JPanel();
            addCarPanel.setLayout(new GridLayout(4, 2));
     
            addCarPanel.add(new JLabel("Reg no."));
            JTextField regNumField = new JTextField(8);
            addCarPanel.add(regNumField);
     
            addCarPanel.add(new JLabel("Length. "));
            JTextField lengthField = new JTextField(3);
            addCarPanel.add(lengthField);
     
            JCheckBox disabledBadge = new JCheckBox("");
            addCarPanel.add(new JLabel("Disabled Badge  "));
            addCarPanel.add(disabledBadge);
     
            addCarPanel.add(new JLabel("Number Of Hours "));
            JTextField hours = new JTextField(3);
            addCarPanel.add(hours);
     
            int optionCar = JOptionPane.showConfirmDialog(this, addCarPanel, "Add Car", JOptionPane.OK_CANCEL_OPTION);
     
            if (optionCar == JOptionPane.OK_OPTION) {
                System.out.println("Reg num entered: " + regNumField.getText());
                System.out.println("Length of Car: " + lengthField.getText());
                System.out.println("Disabled Badge: " + disabledBadge.isSelected());
                System.out.println("Number of hours: " + hours.getText());
                //CarLabels[] = new ImageIcon("images/car.jpg");
                //Carlabels.setIcon(car);
     
                double intLength = Double.parseDouble(lengthField.getText());
                int h = Integer.parseInt(hours.getText());
     
     
                Car CarNew = new Car(regNumField.getText(), intLength, disabledBadge.isSelected(), h);
            }
     
            } 
            else if (me.getSource() == buttons[1]) {        //"Add Lorry Button Click"
     
                     JPanel addLorryPanel = new JPanel();
            addLorryPanel.setLayout(new GridLayout(4, 2));
     
            addLorryPanel.add(new JLabel("Reg no."));
            JTextField regNumField = new JTextField(8);
            addLorryPanel.add(regNumField);
     
            addLorryPanel.add(new JLabel("Weight(KG). "));
            JTextField weight = new JTextField(3);
            addLorryPanel.add(weight);
     
            addLorryPanel.add(new JLabel("Number Of Days "));
            JTextField numOfDays = new JTextField(3);
            addLorryPanel.add(numOfDays);
     
            int optionLorry = JOptionPane.showConfirmDialog(this, addLorryPanel, "Add Lorry", JOptionPane.OK_CANCEL_OPTION);
     
            if (optionLorry == JOptionPane.OK_OPTION) {
               // Lorry.CalcCharge();
                System.out.println("Reg num entered: " + regNumField.getText());
                System.out.println("Weight Of Lorry: " + weight.getText());
                System.out.println("Days Staying: " + numOfDays.getText());
                //ImageIcon lorry = new ImageIcon ("*IMAGE PATH HERE*");
     
     
                double intWeight = Double.parseDouble(weight.getText());
                int d = Integer.parseInt(numOfDays.getText());
     
                Lorry LorryNew = new Lorry(regNumField.getText(), intWeight, d );
     
        }
            } else if (me.getSource() == buttons[2]) {      //"Add Coach Button Click"
     
            JPanel addCoachPanel = new JPanel();
            addCoachPanel.setLayout(new GridLayout(4, 2));
     
            addCoachPanel.add(new JLabel("Reg no."));
            JTextField regNumField = new JTextField(8);
            addCoachPanel.add(regNumField);
     
            addCoachPanel.add(new JLabel("Number Of Passengers "));
            JTextField Passengers = new JTextField(3);
            addCoachPanel.add(Passengers);
     
            JCheckBox touristOperator = new JCheckBox("");
            addCoachPanel.add(new JLabel("Tourist Operator  "));
            addCoachPanel.add(touristOperator);
     
            int optionCoach = JOptionPane.showConfirmDialog(this, addCoachPanel, "Add Coach", JOptionPane.OK_CANCEL_OPTION);
     
            if (optionCoach == JOptionPane.OK_OPTION) {
               // Lorry.CalcCharge();
                System.out.println("Reg num entered: " + regNumField.getText());
                System.out.println("Number Of Pass': " + Passengers.getText());
                System.out.println("Tourist Operator " + touristOperator.isSelected());
                //ImageIcon coach = new ImageIcon ("*IMAGE PATH HERE*");
     
     
                int intPassengers = Integer.parseInt(Passengers.getText());
     
                Coach coachNew = new Coach(regNumField.getText(), intPassengers, touristOperator.isSelected());
     
            } else if (me.getSource() == buttons[3]) {
                System.out.println("Clear All");
            } else if (me.getSource() == buttons[4]) {
                System.out.println("Save");
            } else if (me.getSource() == buttons[5]) {
                System.out.println("Load");
            } else if (me.getSource() == buttons[6]) {
                System.out.println("Current Total");
            } else if (me.getSource() == buttons[7]) {
                System.out.println("Total");
            }
        }
        }
     
        public void mouseReleased(MouseEvent me) {
        }
     
        public void mouseClicked(MouseEvent me) {
        }
     
        public void mouseExited(MouseEvent me) {
        }
     
        /*
         * public void addCar() {
     
            JPanel addCarPanel = new JPanel();
            addCarPanel.setLayout(new GridLayout(4, 2));
     
            addCarPanel.add(new JLabel("Reg no."));
            JTextField regNumField = new JTextField(8);
            addCarPanel.add(regNumField);
     
            addCarPanel.add(new JLabel("Length. "));
            JTextField lengthField = new JTextField(3);
            addCarPanel.add(lengthField);
     
            JCheckBox disabledBadge = new JCheckBox("");
            addCarPanel.add(new JLabel("Disabled Badge  "));
            addCarPanel.add(disabledBadge);
     
            addCarPanel.add(new JLabel("Number Of Hours "));
            JTextField hours = new JTextField(3);
            addCarPanel.add(hours);
     
            int optionCar = JOptionPane.showConfirmDialog(this, addCarPanel, "Add Car", JOptionPane.OK_CANCEL_OPTION);
     
            if (optionCar == JOptionPane.OK_OPTION) {
                System.out.println("Reg num entered: " + regNumField.getText());
                System.out.println("Length of Car: " + lengthField.getText());
                System.out.println("Disabled Badge: " + disabledBadge.isSelected());
                System.out.println("Number of hours: " + hours.getText());
                //CarLabels[] = new ImageIcon("images/car.jpg");
                //Carlabels.setIcon(car);
     
                double intLength = Double.parseDouble(lengthField.getText());
                int h = Integer.parseInt(hours.getText());
     
     
                Car CarNew = new Car(regNumField.getText(), intLength, disabledBadge.isSelected(), h);
            }
     
        }
     
     
      public void addLorry() {
     
            JPanel addLorryPanel = new JPanel();
            addLorryPanel.setLayout(new GridLayout(4, 2));
     
            addLorryPanel.add(new JLabel("Reg no."));
            JTextField regNumField = new JTextField(8);
            addLorryPanel.add(regNumField);
     
            addLorryPanel.add(new JLabel("Weight(KG). "));
            JTextField weight = new JTextField(3);
            addLorryPanel.add(weight);
     
            addLorryPanel.add(new JLabel("Number Of Days "));
            JTextField numOfDays = new JTextField(3);
            addLorryPanel.add(numOfDays);
     
            int optionLorry = JOptionPane.showConfirmDialog(this, addLorryPanel, "Add Lorry", JOptionPane.OK_CANCEL_OPTION);
     
            if (optionLorry == JOptionPane.OK_OPTION) {
               // Lorry.CalcCharge();
                System.out.println("Reg num entered: " + regNumField.getText());
                System.out.println("Weight Of Lorry: " + weight.getText());
                System.out.println("Days Staying: " + numOfDays.getText());
                //ImageIcon lorry = new ImageIcon ("*IMAGE PATH HERE*");
     
     
                double intWeight = Double.parseDouble(weight.getText());
                int d = Integer.parseInt(numOfDays.getText());
     
                Lorry LorryNew = new Lorry(regNumField.getText(), intWeight, d );
     
        }
        }
     
     
        public void addCoach() {
     
            JPanel addCoachPanel = new JPanel();
            addCoachPanel.setLayout(new GridLayout(4, 2));
     
            addCoachPanel.add(new JLabel("Reg no."));
            JTextField regNumField = new JTextField(8);
            addCoachPanel.add(regNumField);
     
            addCoachPanel.add(new JLabel("Weight(KG). "));
            JTextField weightField = new JTextField(3);
            addCoachPanel.add(weightField);
     
            addCoachPanel.add(new JLabel("Number Of Days "));
            JTextField days = new JTextField(3);
            addCoachPanel.add(days);
     
            int optionCoach = JOptionPane.showConfirmDialog(this, addCoachPanel, "Add Lorry", JOptionPane.OK_CANCEL_OPTION);
     
     
     
     
     
        }
     
        public void clearAll() {
        }
     
        public void Save() {
        }
     
        public void Load() {
        }
     
        public void currentTotal() {
        }
     
        public void overallTotal() {
        }
      */  
    }

    Driver Class
    package ica2;
     
    import javax.swing.JFrame;
     
    /**
     *
     * @author n3098463
     */
    public class ICA2 {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
     
            JFrame CarParkProg = new JFrame("Car Park");
     
            GUI panel = new GUI();
     
            CarParkProg.pack();
            CarParkProg.add(panel);
            CarParkProg.setVisible(true);
            CarParkProg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            CarParkProg.setResizable(false);
     
     
            CarParkProg.pack();
     
        }
    }


    --- Update ---

    Vehicle SuperClass
    package ica2;
     
    import javax.swing.ImageIcon;
     
    /**
     *
     * @author Philip Martin
     */
    public abstract class Vehicle {
     
        protected double charge;
        protected String regNumber;
     
     
        public Vehicle(){
           charge = 0.0;
           regNumber = "";
        }
     
        public Vehicle(String regNumber) {
            this.regNumber = regNumber;
        }
     
     
        public double getCharge() {
            return charge;
        }
     
        public abstract ImageIcon GetImage(); 
     
        public abstract double CalcCharge();
     
     
        @Override
        public String toString() {
            return "Vehicle{" + "toString=" + + '}';
        }
     
     
    }

    Car
    package ica2;
     
    import javax.swing.ImageIcon;
     
    /**
     * * @author Philip Martin
     */
    public class Car extends Vehicle {
     
        private double length;
        private boolean disabledBadge;
        private int numOfHours;
     
        public Car() {
            length = 0.0;
            disabledBadge = true;
            numOfHours = 0;
        }
     
        public Car(String rn, double length, boolean disabledBadge, int numOfHours) {
            super(rn);
            this.length = length;
            this.disabledBadge = disabledBadge;
            this.numOfHours = numOfHours;
        }
     
        public double getLength() {
            return length;
        }
     
        public ImageIcon GetImage(){
        ImageIcon car = new ImageIcon ("images/car.jpg");
            return car;
    }
     
        @Override
        public double CalcCharge(){
        if (length <= 6){
                charge = 1.0 * numOfHours;
        }
        else if (length > 6){
                charge = 1.5 * numOfHours;
        }
        else charge = 0;
            return charge;
        }
     
        public String toString() {
            return "Vehicle{" + "toString=" + + '}';
        }
     
    }

    Lorry
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package ica2;
     
    import javax.swing.ImageIcon;
     
    /**
     *
     * @author n3098463
     */
    public class Lorry extends Vehicle {
        private double weight;
        private int numOfDays;
     
        public Lorry() {
           weight = 0.0;
           numOfDays = 0;
        }
     
        public Lorry(String rn, double weight, int numOfDays) {
            super(rn);
            this.weight = weight;
            this.numOfDays = numOfDays;
        }
     
        public double getWeight() {
            return weight;
        }
     
          public ImageIcon GetImage(){
        ImageIcon lorry = new ImageIcon ("lorry.png");
            return lorry;
    }
     
        public double CalcCharge(){
        if (weight < 20.0 ){
                charge = 5.0 * numOfDays;
        }
        else if (weight >= 20.0 && weight < 35.0){
                charge = 8.0 * numOfDays;
        }
        else if (weight >= 35.0)
                charge = 0.0;
                numOfDays = 0;
                System.out.println("Sorry You Cannot Enter The CarPark");
            return charge;
    }
     
     
        @Override
        public String toString() {
            return "Vehicle{" + "toString=" +  + '}';
        }
     
    }

    Coach
    package ica2;
     
    import javax.swing.ImageIcon;
     
    /**
     * @author Philip Martin
     */
    public class Coach extends Vehicle {
     
        private int numOfPassengers;
        private boolean touristOperator;
     
        public Coach() {
        numOfPassengers = 0;
        touristOperator = false; 
        }
     
        public Coach(String rn, int numOfPassengers, boolean touristOperator) {
            super(rn);
            this.numOfPassengers = numOfPassengers;
            this.touristOperator = touristOperator;
        }
     
          public ImageIcon GetImage(){
        ImageIcon coach = new ImageIcon ("coach.png");
            return coach;
    }
     
        public double CalcCharge(){
         if (numOfPassengers <= 20){
             charge = 4.5;
        }
        else if (numOfPassengers > 20){
                charge = 6.0;
        }
         if (touristOperator = true){
             charge = charge * 0.9;
         }
         return charge;
    }
     
        @Override
        public String toString() {
            return "Vehicle{" + "toString=" +  + '}';
        }
     
    }

    GUI
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package ica2;
     
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.*;
    import java.awt.Dimension;
    import java.awt.Frame;
    import java.awt.TextField;
    import java.util.Vector;
    import javax.swing.border.Border;
    import javax.swing.JOptionPane;
     
    /**
     *
     * @author n3098463
     */
    public class GUI extends JPanel implements MouseListener {
     
        JPanel southPark;
        JPanel northPark;
        JPanel btns;
        JPanel carPark;
        JButton addCar;
        JLabel CarSpace;
        JLabel LorrySpace;
        int i;
        int L;
        int C;
        int c;
        String b[] = {"Add Car", "Add Lorry", "Add Coach", "Clear All", "Save", "Load", "Current Total", "Total for day"};
        JButton[] buttons = new JButton[8];
     
        GUI() {
            //*******************************************Vector ********************************************************
     
            //*********************************************BUTTON PANEL**************************************************
     
            btns = new JPanel();
     
            btns.setLayout(new GridLayout(8, 1));
            btns.setVisible(true);
     
            btns.addMouseListener(this);
     
            //btns.setPreferredSize(new Dimension(150, 200));
            //(int width, int height) 
     
     
            //**************************************ADDING THE BUTTONS ON THE PANEL***************************************
     
            for (i = 0; i < buttons.length; i++) {
                buttons[i] = new JButton(b[i]);
                buttons[i].setPreferredSize(new Dimension(200, 70));
                btns.add(buttons[i]);
                buttons[i].addMouseListener(this);
            }
     
            //***************************************CAR PARK PANEL*****************************************************
     
            carPark = new JPanel();
            carPark.setVisible(true);
            //this.add(carPark, BorderLayout.CENTER);
     
            carPark.setLayout(new BorderLayout());
     
            //****************************************NORTH PANEL (LORRYS & COACH)***************************************
            northPark = new JPanel();
            northPark.setVisible(true);                         //900,790
            carPark.add(northPark, BorderLayout.NORTH);
            northPark.setPreferredSize(new Dimension(600, 260));
            //northPark.setBackground(Color.RED);
     
            northPark.setLayout(new GridLayout(1, 4));
     
            //************************************* ARRAY FOR JLABELS LORRYS *******************************************
            JLabel[] LorrySpace = new JLabel[4];
     
            // String lS[] = {"1", "2", "3", "4"};
     
            for (L = 0; L < LorrySpace.length; L++) {
                LorrySpace[L] = new JLabel();
                Border borderLorry = BorderFactory.createLineBorder(Color.RED, 2);
                LorrySpace[L].setBorder(borderLorry);
                LorrySpace[L].setPreferredSize(new Dimension(75, 270));
                northPark.add(LorrySpace[L]);
            }
     
            //*************************************SOUTH PANEL (CARS) **************************************************
     
            southPark = new JPanel();
            southPark.setVisible(true);
            carPark.add(southPark, BorderLayout.SOUTH);
            //southPark.setPreferredSize(new Dimension(75, 100));
            //southPark.setBackground(Color.GREEN);
     
            southPark.setLayout(new GridLayout(3, 4));
     
            //****************************************  ARRAY FOR JLABELS CARS *****************************************
     
            JLabel[] CarLabels = new JLabel[12];
     
            //String cS[] = {"1", "2", "3", "4", "5" , "6" , "7" , "8" , "9" , "10" , "11" , "12" };
     
            for (C = 0; C < CarLabels.length; C++) {
     
                CarLabels[C] = new JLabel();
     
                Border border = BorderFactory.createLineBorder(Color.RED, 2);
     
                CarLabels[C].setBorder(border);
     
                CarLabels[C].setPreferredSize(new Dimension(75, 100));
     
                southPark.add(CarLabels[C]);
            }
            //************************************************************************************************************
     
            this.add(btns, BorderLayout.WEST);
            this.add(carPark);
        }
     
        public void mouseEntered(MouseEvent me) {
        }
     
        public void mousePressed(MouseEvent me) {
            if (me.getSource() == buttons[0]) {                     //"Add Car Button Click"
     
            JPanel addCarPanel = new JPanel();
            addCarPanel.setLayout(new GridLayout(4, 2));
     
            addCarPanel.add(new JLabel("Reg no."));
            JTextField regNumField = new JTextField(8);
            addCarPanel.add(regNumField);
     
            addCarPanel.add(new JLabel("Length. "));
            JTextField lengthField = new JTextField(3);
            addCarPanel.add(lengthField);
     
            JCheckBox disabledBadge = new JCheckBox("");
            addCarPanel.add(new JLabel("Disabled Badge  "));
            addCarPanel.add(disabledBadge);
     
            addCarPanel.add(new JLabel("Number Of Hours "));
            JTextField hours = new JTextField(3);
            addCarPanel.add(hours);
     
            int optionCar = JOptionPane.showConfirmDialog(this, addCarPanel, "Add Car", JOptionPane.OK_CANCEL_OPTION);
     
            if (optionCar == JOptionPane.OK_OPTION) {
                System.out.println("Reg num entered: " + regNumField.getText());
                System.out.println("Length of Car: " + lengthField.getText());
                System.out.println("Disabled Badge: " + disabledBadge.isSelected());
                System.out.println("Number of hours: " + hours.getText());
                //CarLabels[] = new ImageIcon("images/car.jpg");
                //Carlabels.setIcon(car);
     
                double intLength = Double.parseDouble(lengthField.getText());
                int h = Integer.parseInt(hours.getText());
     
     
                Car CarNew = new Car(regNumField.getText(), intLength, disabledBadge.isSelected(), h);
            }
     
            } 
            else if (me.getSource() == buttons[1]) {        //"Add Lorry Button Click"
     
                     JPanel addLorryPanel = new JPanel();
            addLorryPanel.setLayout(new GridLayout(4, 2));
     
            addLorryPanel.add(new JLabel("Reg no."));
            JTextField regNumField = new JTextField(8);
            addLorryPanel.add(regNumField);
     
            addLorryPanel.add(new JLabel("Weight(KG). "));
            JTextField weight = new JTextField(3);
            addLorryPanel.add(weight);
     
            addLorryPanel.add(new JLabel("Number Of Days "));
            JTextField numOfDays = new JTextField(3);
            addLorryPanel.add(numOfDays);
     
            int optionLorry = JOptionPane.showConfirmDialog(this, addLorryPanel, "Add Lorry", JOptionPane.OK_CANCEL_OPTION);
     
            if (optionLorry == JOptionPane.OK_OPTION) {
               // Lorry.CalcCharge();
                System.out.println("Reg num entered: " + regNumField.getText());
                System.out.println("Weight Of Lorry: " + weight.getText());
                System.out.println("Days Staying: " + numOfDays.getText());
                //ImageIcon lorry = new ImageIcon ("*IMAGE PATH HERE*");
     
     
                double intWeight = Double.parseDouble(weight.getText());
                int d = Integer.parseInt(numOfDays.getText());
     
                Lorry LorryNew = new Lorry(regNumField.getText(), intWeight, d );
     
        }
            } else if (me.getSource() == buttons[2]) {      //"Add Coach Button Click"
     
            JPanel addCoachPanel = new JPanel();
            addCoachPanel.setLayout(new GridLayout(4, 2));
     
            addCoachPanel.add(new JLabel("Reg no."));
            JTextField regNumField = new JTextField(8);
            addCoachPanel.add(regNumField);
     
            addCoachPanel.add(new JLabel("Number Of Passengers "));
            JTextField Passengers = new JTextField(3);
            addCoachPanel.add(Passengers);
     
            JCheckBox touristOperator = new JCheckBox("");
            addCoachPanel.add(new JLabel("Tourist Operator  "));
            addCoachPanel.add(touristOperator);
     
            int optionCoach = JOptionPane.showConfirmDialog(this, addCoachPanel, "Add Coach", JOptionPane.OK_CANCEL_OPTION);
     
            if (optionCoach == JOptionPane.OK_OPTION) {
               // Lorry.CalcCharge();
                System.out.println("Reg num entered: " + regNumField.getText());
                System.out.println("Number Of Pass': " + Passengers.getText());
                System.out.println("Tourist Operator " + touristOperator.isSelected());
                //ImageIcon coach = new ImageIcon ("*IMAGE PATH HERE*");
     
     
                int intPassengers = Integer.parseInt(Passengers.getText());
     
                Coach coachNew = new Coach(regNumField.getText(), intPassengers, touristOperator.isSelected());
     
            } else if (me.getSource() == buttons[3]) {
                System.out.println("Clear All");
            } else if (me.getSource() == buttons[4]) {
                System.out.println("Save");
            } else if (me.getSource() == buttons[5]) {
                System.out.println("Load");
            } else if (me.getSource() == buttons[6]) {
                System.out.println("Current Total");
            } else if (me.getSource() == buttons[7]) {
                System.out.println("Total");
            }
        }
        }
     
        public void mouseReleased(MouseEvent me) {
        }
     
        public void mouseClicked(MouseEvent me) {
        }
     
        public void mouseExited(MouseEvent me) {
        }
     
        /*
         * public void addCar() {
     
            JPanel addCarPanel = new JPanel();
            addCarPanel.setLayout(new GridLayout(4, 2));
     
            addCarPanel.add(new JLabel("Reg no."));
            JTextField regNumField = new JTextField(8);
            addCarPanel.add(regNumField);
     
            addCarPanel.add(new JLabel("Length. "));
            JTextField lengthField = new JTextField(3);
            addCarPanel.add(lengthField);
     
            JCheckBox disabledBadge = new JCheckBox("");
            addCarPanel.add(new JLabel("Disabled Badge  "));
            addCarPanel.add(disabledBadge);
     
            addCarPanel.add(new JLabel("Number Of Hours "));
            JTextField hours = new JTextField(3);
            addCarPanel.add(hours);
     
            int optionCar = JOptionPane.showConfirmDialog(this, addCarPanel, "Add Car", JOptionPane.OK_CANCEL_OPTION);
     
            if (optionCar == JOptionPane.OK_OPTION) {
                System.out.println("Reg num entered: " + regNumField.getText());
                System.out.println("Length of Car: " + lengthField.getText());
                System.out.println("Disabled Badge: " + disabledBadge.isSelected());
                System.out.println("Number of hours: " + hours.getText());
                //CarLabels[] = new ImageIcon("images/car.jpg");
                //Carlabels.setIcon(car);
     
                double intLength = Double.parseDouble(lengthField.getText());
                int h = Integer.parseInt(hours.getText());
     
     
                Car CarNew = new Car(regNumField.getText(), intLength, disabledBadge.isSelected(), h);
            }
     
        }
     
     
      public void addLorry() {
     
            JPanel addLorryPanel = new JPanel();
            addLorryPanel.setLayout(new GridLayout(4, 2));
     
            addLorryPanel.add(new JLabel("Reg no."));
            JTextField regNumField = new JTextField(8);
            addLorryPanel.add(regNumField);
     
            addLorryPanel.add(new JLabel("Weight(KG). "));
            JTextField weight = new JTextField(3);
            addLorryPanel.add(weight);
     
            addLorryPanel.add(new JLabel("Number Of Days "));
            JTextField numOfDays = new JTextField(3);
            addLorryPanel.add(numOfDays);
     
            int optionLorry = JOptionPane.showConfirmDialog(this, addLorryPanel, "Add Lorry", JOptionPane.OK_CANCEL_OPTION);
     
            if (optionLorry == JOptionPane.OK_OPTION) {
               // Lorry.CalcCharge();
                System.out.println("Reg num entered: " + regNumField.getText());
                System.out.println("Weight Of Lorry: " + weight.getText());
                System.out.println("Days Staying: " + numOfDays.getText());
                //ImageIcon lorry = new ImageIcon ("*IMAGE PATH HERE*");
     
     
                double intWeight = Double.parseDouble(weight.getText());
                int d = Integer.parseInt(numOfDays.getText());
     
                Lorry LorryNew = new Lorry(regNumField.getText(), intWeight, d );
     
        }
        }
     
     
        public void addCoach() {
     
            JPanel addCoachPanel = new JPanel();
            addCoachPanel.setLayout(new GridLayout(4, 2));
     
            addCoachPanel.add(new JLabel("Reg no."));
            JTextField regNumField = new JTextField(8);
            addCoachPanel.add(regNumField);
     
            addCoachPanel.add(new JLabel("Weight(KG). "));
            JTextField weightField = new JTextField(3);
            addCoachPanel.add(weightField);
     
            addCoachPanel.add(new JLabel("Number Of Days "));
            JTextField days = new JTextField(3);
            addCoachPanel.add(days);
     
            int optionCoach = JOptionPane.showConfirmDialog(this, addCoachPanel, "Add Lorry", JOptionPane.OK_CANCEL_OPTION);
     
     
     
     
     
        }
     
        public void clearAll() {
        }
     
        public void Save() {
        }
     
        public void Load() {
        }
     
        public void currentTotal() {
        }
     
        public void overallTotal() {
        }
      */  
    }

    Driver Class
    package ica2;
     
    import javax.swing.JFrame;
     
    /**
     *
     * @author n3098463
     */
    public class ICA2 {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
     
            JFrame CarParkProg = new JFrame("Car Park");
     
            GUI panel = new GUI();
     
            CarParkProg.pack();
            CarParkProg.add(panel);
            CarParkProg.setVisible(true);
            CarParkProg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            CarParkProg.setResizable(false);
     
     
            CarParkProg.pack();
     
        }
    }


    --- Update ---

    im still a beginner you see and i have no clue

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,125
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: CAR PARK SIMULATOR

    How does a beginner write so much code? I don't understand how it can be done.

    It may be hard to find someone willing to go through all that code.

    A Short, Self Contained, Correct Example will get more help.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Moving a car
    By mituk2936@gmail.com in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 25th, 2013, 11:31 AM
  2. Car Park Simulator - HELP!
    By lycan17 in forum AWT / Java Swing
    Replies: 1
    Last Post: August 15th, 2012, 08:11 AM
  3. The car/vehicle application will be responsible for providing a car simulation.
    By vikasreddy556 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: June 20th, 2011, 01:32 AM