aspose file tools*
The moose likes Beginning Java and the fly likes Please can you help me? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Please can you help me?" Watch "Please can you help me?" New topic
Author

Please can you help me?

Jose Abreu
Greenhorn

Joined: Apr 17, 2006
Posts: 3
I am currently working on a uni coursework. I need to create a car Rental interface. I currently have the basic design sorted out, however I am having problem with the painting and repainting of the 4 images of the cars. They seem to dissapear when I open up the Combo boxes on the 2nd row. Could you please help me and let me know how this can be resolved?

import java.awt.*;
import javax.swing.*;
import java.applet.*;
import java.awt.event.*;


public class CarRentalsMain extends JApplet implements ItemListener, ActionListener, MouseListener {
Graphics g;

JLabel myLabel;
JComboBox PickUpLocation, DropOffLocation, PickUpDay, PickUpMonth, DropOffDay, DropOffMonth;
JRadioButton groupa,groupb,groupc,groupd;
String PickUpLocationC, DropOffLocationC, PickUpMonthC, DropOffMonthC, PickUpDayString, DropOffDayString;
int PickUpDayC, DropOffDayC;
String[] Locations = {"London Heathrow", "London Gatwick", "London Luton", "London Stansted", "London Down Town City Centre"};
String[] Months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
String[] Days28 = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28"};
String[] Days30 = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30"};
String[] Days31 = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"};


public void init() {

Container contentPane = getContentPane();
getContentPane().setLayout(null);
getContentPane().setBackground(new Color(253,182,75));

//display the first line of the introduction
myLabel = new JLabel("Welcome to the 1000+1 Car Rentals Reservation Form, please choose Pick-Up and Drop-Off location and dates during which you would");
myLabel.setFont(new java.awt.Font("Arial", 0, 12));
myLabel.setBounds(7,110,780,30);
getContentPane().add(myLabel);

//display the second line of the introduction
myLabel = new JLabel("like to rent the car and the group of car you would like to rent.");
myLabel.setFont(new java.awt.Font("Arial", 0, 12));
myLabel.setBounds(7,125,780,30);
getContentPane().add(myLabel);


//display the title for the location and dates
myLabel = new JLabel("LOCATION AND DATES:");
myLabel.setFont(new java.awt.Font("Arial", Font.BOLD, 18));
myLabel.setBounds(7,165,780,30);
getContentPane().add(myLabel);

//display the label for the Pick Up location and its Combo Box
myLabel = new JLabel("Pick Up Location:");
myLabel.setFont(new java.awt.Font("Arial", 36, 12));
myLabel.setBounds(7,185,780,30);
getContentPane().add(myLabel);
PickUpLocation = new JComboBox(Locations);
PickUpLocation.setBounds(60,210,250,20);
PickUpLocation.addItemListener(this);
contentPane.add(PickUpLocation);

//display the label for the Drop Off location and its Combo Box
myLabel = new JLabel("Drop Off Location:");
myLabel.setFont(new java.awt.Font("Arial", 36, 12));
myLabel.setBounds(407,185,780,30);
getContentPane().add(myLabel);
DropOffLocation = new JComboBox(Locations);
DropOffLocation.setBounds(460,210,250,20);
DropOffLocation.addItemListener(this);
contentPane.add(DropOffLocation);

//display the label for the Pick Up Date and its Combo Boxes
myLabel = new JLabel("Pick Up Date:");
myLabel.setFont(new java.awt.Font("Arial", 36, 12));
myLabel.setBounds(7,225,780,30);
getContentPane().add(myLabel);
PickUpDay = new JComboBox(Days31);
PickUpDay.setBounds(60,250,50,20);
PickUpDay.addItemListener(this);
contentPane.add(PickUpDay);
PickUpMonth = new JComboBox(Months);
PickUpMonth.setBounds(125,250,120,20);
PickUpMonth.addItemListener(this);
contentPane.add(PickUpMonth);

//display the label for the Pick Up Date and its Combo Boxes
myLabel = new JLabel("Drop Off Date:");
myLabel.setFont(new java.awt.Font("Arial", 36, 12));
myLabel.setBounds(407,225,780,30);
getContentPane().add(myLabel);
DropOffDay = new JComboBox(Days31);
DropOffDay.setBounds(460,250,50,20);
DropOffDay.addItemListener(this);
contentPane.add(DropOffDay);
DropOffMonth = new JComboBox(Months);
DropOffMonth.setBounds(525,250,120,20);
DropOffMonth.addItemListener(this);
contentPane.add(DropOffMonth);


//display the title for the car groups
myLabel = new JLabel("CAR GROUPS:");
myLabel.setFont(new java.awt.Font("Arial", Font.BOLD, 18));
myLabel.setBounds(7,295,780,30);
getContentPane().add(myLabel);

//display the label asking to select the car group
myLabel = new JLabel("Please select car group:");
myLabel.setFont(new java.awt.Font("Arial", 36, 12));
myLabel.setBounds(7,315,780,30);
getContentPane().add(myLabel);

//display Radio Button and information for Car Group A
JRadioButton groupa = new JRadioButton("Group A", false);
groupa.setActionCommand("Group A");
groupa.setBounds(7,340,100,20);
groupa.addActionListener(this);
groupa.setBackground(new Color(253,182,75));
contentPane.add(groupa);

//display Radio Button and information for Car Group b
JRadioButton groupb = new JRadioButton("Group B", false);
groupb.setActionCommand("Group B");
groupb.setBounds(407,340,100,20);
groupb.addActionListener(this);
groupb.setBackground(new Color(253,182,75));
contentPane.add(groupb);

//display Radio Button and information for Car Group c
JRadioButton groupc = new JRadioButton("Group C", false);
groupc.setActionCommand("Group C");
groupc.setBounds(7,415,100,20);
groupc.addActionListener(this);
groupc.setBackground(new Color(253,182,75));
contentPane.add(groupc);

//display Radio Button and information for Car Group 4x4
JRadioButton group4x4 = new JRadioButton("Group 4x4", false);
group4x4.setActionCommand("Group 4x4");
group4x4.setBounds(407,415,100,20);
group4x4.addActionListener(this);
group4x4.setBackground(new Color(253,182,75));
contentPane.add(group4x4);

//Group the Radio Buttons
ButtonGroup cargroups = new ButtonGroup();
cargroups.add(groupa);
cargroups.add(groupb);
cargroups.add(groupc);
cargroups.add(group4x4);

addMouseListener(this);
repaint();



}


public void paint(Graphics g) {

super.paint(g);

//displays the title
Image titleimage = getImage(getCodeBase(),"title.jpg");
g.drawImage(titleimage,10,5,this);

//Group A car pic
Image groupaimage = getImage(getCodeBase(),"GroupA.jpg");
g.drawImage(groupaimage,225,350,this);
//Group B car pic
Image groupbimage = getImage(getCodeBase(),"GroupB.jpg");
g.drawImage(groupbimage,625,350,this);
//Group C car pic
Image groupcimage = getImage(getCodeBase(),"GroupC.jpg");
g.drawImage(groupcimage,225,425,this);
//Group 4x4 car pic
Image group4x4image = getImage(getCodeBase(),"4x4.jpg");
g.drawImage(group4x4image,625,425,this);


}

public void paintCars(Graphics g) {


//Group A car pic
Image groupaimage = getImage(getCodeBase(),"GroupA.jpg");
g.drawImage(groupaimage,225,350,this);
//Group B car pic
Image groupbimage = getImage(getCodeBase(),"GroupB.jpg");
g.drawImage(groupbimage,625,350,this);
//Group C car pic
Image groupcimage = getImage(getCodeBase(),"GroupC.jpg");
g.drawImage(groupcimage,225,425,this);
//Group 4x4 car pic
Image group4x4image = getImage(getCodeBase(),"4x4.jpg");
g.drawImage(group4x4image,625,425,this);


}

public void mouseClicked( MouseEvent event ) {}
public void mouseEntered( MouseEvent event ) {}
public void mouseExited( MouseEvent event ) {}
public void mousePressed( MouseEvent event ) {
repaint();
}
public void mouseReleased( MouseEvent event ) { }


public void itemStateChanged(ItemEvent e) {

Object source = e.getItemSelectable();

if ( source == PickUpLocation ) {
PickUpLocationC = (String) e.getItem();
} else if ( source == DropOffLocation ) {
DropOffLocationC = (String) e.getItem();
} else if ( source == PickUpLocation ) {
PickUpLocationC = (String) e.getItem();
} else if ( source == PickUpDay ) {
PickUpDayString = (String) e.getItem();
PickUpDayC = Integer.parseInt(PickUpDayString);
} else if ( source == PickUpMonth ) {
PickUpMonthC = (String) e.getItem();
} else if ( source == DropOffDay ) {
DropOffDayString = (String) e.getItem();
DropOffDayC = Integer.parseInt(DropOffDayString);
} else if ( source == DropOffMonth ) {
DropOffMonthC = (String) e.getItem();
}

repaint();

}


public void actionPerformed(ActionEvent e)
{

repaint();

}



}



Thank you very much in advance... If you would like to see it at work please visit http://members.lycos.co.uk/joseabreuslb/



please,please,please can you help me?

thanks
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Hi,

Welcome to JavaRanch!

In a JApplet, [i]don't[i] override paint(); override paintComponent() instead. It's a lot like paint(), really; in fact, if you just change the declaration of your paint(Graphics g) method to paintComponent(Graphics g), without changing anything else, that very well may fix your problem right there.


[Jess in Action][AskingGoodQuestions]
Jose Abreu
Greenhorn

Joined: Apr 17, 2006
Posts: 3
Thanks for the quick response. Unfortunately when i change the paint to paintComponent(Graphics g) none of the graphics load.
Jose Abreu
Greenhorn

Joined: Apr 17, 2006
Posts: 3
its ok, i have resolved it. i have created the images as icons for labels.
hooray

thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please can you help me?