Jump to content

Adding Background image to JFrame

- - - - -

  • Please log in to reply
4 replies to this topic

#1
Aaron140

Aaron140

    Newbie

  • Members
  • Pip
  • 6 posts
I want to add an image to this code, i want it to be instead of the white space when my program runs.

can somebody edit my code to do this for me?


/**Driving School Tester*/


import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import javax.imageio.ImageIO;



public class DrivingSchoolTest extends JFrame implements ActionListener{


	JMenu fileMenu,lessonMenu,studentMenu;

	Students [] student;

	Lessons [] lesson;

	int count;

	int count1;



	public static void main( String[] args ) {

        DrivingSchoolTest frame = new DrivingSchoolTest();

        frame.setVisible(true);

    }


    public DrivingSchoolTest( ) {


        setTitle     ( "Driving School" );

        setSize      ( 400,200 );

        setLocation  ( 350,300 );

        Container pane = getContentPane();

        pane.setBackground(Color.WHITE);

        setDefaultCloseOperation( EXIT_ON_CLOSE );


        createFileMenu();

        createStudentMenu();

        createLessonMenu();

        JMenuBar menuBar = new JMenuBar();

        setJMenuBar(menuBar);

        menuBar.add(fileMenu);

        menuBar.add(studentMenu);

        menuBar.add(lessonMenu);

     }


     public void newSystemStudent() {

      	student = new Students[10];

      	count = 0;

      }



      /** writes the array of students to the file "student.dat"

       */   // NEW


      public void saveStudent() throws IOException {

      	ObjectOutputStream os;

      	os = new ObjectOutputStream(new FileOutputStream ("student.dat"));

      	os.writeObject(student);

      	os.close();

      }


       /** loads an array of students from the file "student.dat"

       */  // NEW

      public void openStudent() {

      	count = 0;

      	try{

      	  ObjectInputStream is;

      	  is = new ObjectInputStream(new FileInputStream ("student.dat"));

          student  = (Students []) is.readObject();

      	  is.close();

      	}

      	catch(Exception e){

      		JOptionPane.showMessageDialog(null,"Open File Failed");

      		e.printStackTrace();

      	}


      	// how many valid student records?

      	while (student[count] !=null)

      	   count++;

      } // end open()


       public void addStudent(){

      	Students s1 = new Students();

      	s1.setName(JOptionPane.showInputDialog("Enter Student Name:"));

      	s1.setAddress(JOptionPane.showInputDialog("Enter address for Student 2: "));

    	s1.setNumber(JOptionPane.showInputDialog("Enter phone number for Student 2: "));

    	s1.setNoLessons(Integer.parseInt(JOptionPane.showInputDialog("Enter number of lessons for Student 2: ")));

      	student[count] = s1; // 'default student

      	// set the attributes: ask the user for name etc

      	count++; // now there is one more student in the system

      }


      public void displayStudent(){

      	JTextArea area = new JTextArea();

      	if (count>0) {

      	  area.setText("Student List:\n\n");

      	  for (int i = 0; i<count; i++) // loop over existing bikes, rather than array size

      	    area.append("Students: " + i + " " +student[i].toString()+"\n");

      	  showMessage(area);

      	}

      	else

      	    showMessage("No students in the system");

      } // end display








       public void newSystemLesson() {

      	lesson = new Lessons[10];

      	count1 = 0;

      }


      /** writes the array of lessons to the file "lesson.dat"

       */   // NEW

      public void saveLesson() throws IOException {

      	ObjectOutputStream os;

      	os = new ObjectOutputStream(new FileOutputStream ("lesson.dat"));

      	os.writeObject(lesson);

      	os.close();

      }


      /** loads an array of lessons from the file "lesson.dat"

       */  // NEW

      public void openLesson() {

      	count1 = 0;

      	try{

      	  ObjectInputStream is;

      	  is = new ObjectInputStream(new FileInputStream ("lesson.dat"));

          lesson  = (Lessons []) is.readObject();

      	  is.close();

      	}

      	catch(Exception e){

      		JOptionPane.showMessageDialog(null,"Open File Failed");

      		e.printStackTrace();

      	}


      	// how many valid lesson records?

      	while (lesson[count1] !=null)

      	   count1++;

      } // end open()



      public void addLesson(){

      	Lessons l2 = new Lessons();

      	l2.setStName(JOptionPane.showInputDialog("Enter Student Name:"));

      	l2.setLocation(JOptionPane.showInputDialog("Enter location for Lesson 2: "));

    	l2.setTime(JOptionPane.showInputDialog("Enter time for Lesson 2: "));

    	l2.setPrice(Double.parseDouble(JOptionPane.showInputDialog("Please enter a price for lesson 2: ")));

      	lesson[count1] = l2; // 'default lesson

      	// set the attributes: ask the user for location etc

      	count1++; // now there is one more lesson in the system

      }


      public void displayLesson(){

      	JTextArea area = new JTextArea();

      	if (count1>0) {

      	  area.setText("Lesson List:\n\n");

      	  for (int i = 0; i<count1; i++) // loop over existing lessons, rather than array size

      	    area.append("Lessons: " + i + " " +lesson[i].toString()+"\n");

      	  showMessage(area);

      	}

      	else

      	    showMessage("No lessons in the system");

      } // end display



      public void actionPerformed (ActionEvent e) {

      	if (e.getActionCommand() .equals ("Quit")){

      	 showMessage("Shutting down the system");

      	 System.exit(0);

      	}

      	else if (e.getActionCommand() .equals ("Register")){

      	   addStudent(); // branch to a separate method

      	}

      	else if (e.getActionCommand() .equals ("Display")){

           displayStudent();

      	}

      	else if (e.getActionCommand() .equals ("Create")){

      	   addLesson(); // branch to a separate method

      	}

      	else if (e.getActionCommand() .equals ("View")){

           displayLesson();

      	}

      	else if (e.getActionCommand() .equals ("New File")){

           newSystemStudent();

      	   newSystemLesson();

        }

      	else if (e.getActionCommand() .equals ("Save")){

      	// Error Handling

      	 try{

      	 	saveStudent();

      	 	saveLesson();

      	 	showMessage("Unable to Save Data");

      	 } // try

      	 catch (IOException f){

      	 	showMessage("Unable to Save Data: ");

      	 	f.printStackTrace();

      	 }// catch

      	}// else if


      	else if (e.getActionCommand() .equals ("Open")){

      	 openStudent();

      	}

      	else

      	  showMessage("Error");

      } // actionPerformed


        private void createFileMenu(){

         // create the menu

      	fileMenu = new JMenu("File");

      	// declare a menu item (re-usable)

      	JMenuItem item;

      	item = new JMenuItem("New File");

      	item.addActionListener(this);

      	fileMenu.add(item);

      	item = new JMenuItem("Save");

      	item.addActionListener(this);

      	fileMenu.add(item);

      	item = new JMenuItem("Open");

      	item.addActionListener(this);

      	fileMenu.add(item);


      	// create the 'quit' option

      	fileMenu.addSeparator();

      	item = new JMenuItem("Quit");

      	item.addActionListener(this);

      	fileMenu.add(item);

      }


      private void createStudentMenu(){

      	// create the menu

      	studentMenu = new JMenu("Student");

      	// declare a menu item (re-usable)

      	JMenuItem item;


      	item = new JMenuItem("Register");

      	item.addActionListener(this);

      	studentMenu.add(item);


      	item = new JMenuItem("Display");

      	item.addActionListener(this);

      	studentMenu.add(item);

      }


      private void createLessonMenu(){

      	// create the menu

      	lessonMenu = new JMenu("Lesson");

      	// declare a menu item (re-usable)

      	JMenuItem item;


      	item = new JMenuItem("Create");

      	item.addActionListener(this);

      	lessonMenu.add(item);


      	item = new JMenuItem("View");

      	item.addActionListener(this);

      	lessonMenu.add(item);

      }

       /** utility methods to make the code simpler */

      public void showMessage (String s){

      	JOptionPane.showMessageDialog(null,s);

      }


      public void showMessage (JTextArea s){

      	JOptionPane.showMessageDialog(null,s);

      }

}


#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Try adding:

 @Override

    public void paint(Graphics g) {

        try {

            Image img = ImageIO.read(new File("C:\\Folder\\image.jpg"));             

            g.drawImage(img, 0, 0, this);

        } catch (IOException e) {

            e.printStackTrace();

        }

    }



#3
Aaron140

Aaron140

    Newbie

  • Members
  • Pip
  • 6 posts
That displays the image,

But it displays it over my current menu and if u ajust where it lays on the program, the background is transparent, is there any way of making it so my Menu buttons are visible and the image i want sits under that?

#4
Aaron140

Aaron140

    Newbie

  • Members
  • Pip
  • 6 posts
New Problem!!..

My program will not hold "Name" after it was entered in under Student Menu.

Any ideas why?

Updated Code.

DRIVER PROGRAM

/**Driving School Tester*/

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.imageio.ImageIO;


public class DrivingSchoolTest extends JFrame implements ActionListener{

	JMenu fileMenu,lessonMenu,studentMenu;
	Students [] student;
	Lessons [] lesson;
	int count;
	int count1;


	public static void main( String[] args ) {
        DrivingSchoolTest frame = new DrivingSchoolTest();
        frame.setVisible(true);
    }


    public DrivingSchoolTest( ) {

        setTitle     ( "Driving School" );
        setSize      ( 305,205 );
        setLocation  ( 350,300 );

        Container pane = getContentPane();
        pane.setBackground(Color.WHITE);
        setDefaultCloseOperation( EXIT_ON_CLOSE );

        JPanel mainFramePanel= new JPanel(new FlowLayout());
	    ImageIcon picture= new ImageIcon("MainImage.jpg");
	   	JLabel peopleLabel= new JLabel(picture);
		pane.add(peopleLabel);

        createFileMenu();
        createStudentMenu();
        createLessonMenu();
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        menuBar.add(fileMenu);
        menuBar.add(studentMenu);
        menuBar.add(lessonMenu);
     }


     public void newSystemStudent() {
      	student = new Students[10];
      	count = 0;
      }

      /** writes the array of students to the file "student.dat"
       */   // NEW

      public void saveStudent() throws IOException {
      	ObjectOutputStream os;
      	os = new ObjectOutputStream(new FileOutputStream ("student.dat"));
      	os.writeObject(student);
      	os.close();
      }

       /** loads an array of students from the file "student.dat"
       */  // NEW
      public void openStudent() {
      	count = 0;
      	try{
      	  ObjectInputStream is;
      	  is = new ObjectInputStream(new FileInputStream ("student.dat"));
          student  = (Students []) is.readObject();
      	  is.close();
      	}
      	catch(Exception e){
      		JOptionPane.showMessageDialog(null,"Open File Failed");
      		e.printStackTrace();
      	}

      	// how many valid student records?
      	while (student[count] !=null)
      	   count++;
      } // end open()


       public void addStudent(){
      	Students s1 = new Students();
      	s1.setName(JOptionPane.showInputDialog("Enter Student Name:"));
      	s1.setAddress(JOptionPane.showInputDialog("Enter address for Student 2: "));
    	s1.setNumber(JOptionPane.showInputDialog("Enter phone number for Student 2: "));
    	s1.setNoLessons(Integer.parseInt(JOptionPane.showInputDialog("Enter number of lessons for Student 2: ")));
      	student[count] = s1; // 'default student
      	// set the attributes: ask the user for name etc
      	count++; // now there is one more student in the system
      }


      public void displayStudent(){
      	JTextArea area = new JTextArea();
      	if (count>0) {
      	  area.setText("Student List:\n");
      	  for (int i = 0; i<count; i++) // loop over existing students, rather than array size
      	    area.append("\nStudents: " + i + " " +student[i].toString()+"\n");
      	  showMessage(area);
      	}
      	else
      	    showMessage("No students in the system");
      } // end display


       public void newSystemLesson() {
      	lesson = new Lessons[10];
      	count1 = 0;
      }


      /** writes the array of lessons to the file "lesson.dat"
       */   // NEW
      public void saveLesson() throws IOException {
      	ObjectOutputStream os;
      	os = new ObjectOutputStream(new FileOutputStream ("lesson.dat"));
      	os.writeObject(lesson);
      	os.close();
      }


      /** loads an array of lessons from the file "lesson.dat"
       */  // NEW
      public void openLesson() {
      	count1 = 0;
      	try{
      	  ObjectInputStream is;
      	  is = new ObjectInputStream(new FileInputStream ("lesson.dat"));
          lesson  = (Lessons []) is.readObject();
      	  is.close();
      	}
      	catch(Exception e){
      		JOptionPane.showMessageDialog(null,"Open File Failed");
      		e.printStackTrace();
      	}

      	// how many valid lesson records?
      	while (lesson[count1] !=null)
      	   count1++;
      } // end open()


      public void addLesson(){
      	Lessons l2 = new Lessons();
      	l2.setLocation(JOptionPane.showInputDialog("Enter location for the Lesson: "));
    	l2.setTime(JOptionPane.showInputDialog("Enter time for the Lesson: "));
    	l2.setPrice(Double.parseDouble(JOptionPane.showInputDialog("Please enter a price for the lesson: ")));
      	lesson[count1] = l2; // 'default lesson
      	// set the attributes: ask the user for location etc
      	count1++; // now there is one more lesson in the system
      }


      public void displayLesson(){
      	JTextArea area = new JTextArea();
      	if (count1>0) {
      	  area.setText("Lesson List:\n");
      	  for (int i = 0; i<count1; i++) // loop over existing lessons, rather than array size
      	    area.append("\nLessons: " + i + "\n" +lesson[i].toString()+"\n");
      	  showMessage(area);
      	}
      	else
      	    showMessage("No lessons in the system");
      } // end display


	public void displayAll(){
		JTextArea area = new JTextArea();
		if (count>0) {
      	  area.setText("Student and Lesson List:\n\n");
      	  for (int i = 0; i<count; i++) // loop over existing bikes, rather than array size
      	    area.append("Students and Lessons: " + i + " " +student[i].toString()+"\n");
      	}
      	else
      	    showMessage("No students in the system");

      	if (count1>0) {
      	  for (int i = 0; i<count1; i++) // loop over existing lessons, rather than array size
      	    area.append(lesson[i].toString()+"\n");
      	  showMessage(area);
      	}
      	else
      	    showMessage("No lessons in the system");
	}

      public void actionPerformed (ActionEvent e) {
      	if (e.getActionCommand() .equals ("Quit")){
      	 showMessage("Shutting down the system");
      	 System.exit(0);
      	}
      	else if (e.getActionCommand() .equals ("Register")){
      	   addStudent(); // branch to a separate method
      	}
      	else if (e.getActionCommand() .equals ("Display")){
           displayStudent();
      	}
      	else if (e.getActionCommand() .equals ("Create")){
      	   addLesson(); // branch to a separate method
      	}
      	else if (e.getActionCommand() .equals ("View")){
           displayLesson();
      	}
      	else if (e.getActionCommand() .equals ("New File")){
           newSystemStudent();
      	   newSystemLesson();
        }
        else if (e.getActionCommand() .equals ("Show All")){
           displayAll();
        }
      	else if (e.getActionCommand() .equals ("Save")){
      	// Error Handling
      	 try{
      	 	saveStudent();
      	 	saveLesson();
      	 	showMessage("Unable to Save Data");
      	 } // try
      	 catch (IOException f){
      	 	showMessage("Unable to Save Data: ");
      	 	f.printStackTrace();
      	 }// catch
      	}// else if

      	else if (e.getActionCommand() .equals ("Open")){
      	 openStudent();
      	}
      	else
      	  showMessage("Error");
      } // actionPerformed


        private void createFileMenu(){
         // create the menu
      	fileMenu = new JMenu("File");
      	// declare a menu item (re-usable)
      	JMenuItem item;
      	item = new JMenuItem("New File");
      	item.addActionListener(this);
      	fileMenu.add(item);
      	item = new JMenuItem("Show All");
      	item.addActionListener(this);
      	fileMenu.add(item);
      	item = new JMenuItem("Save");
      	item.addActionListener(this);
      	fileMenu.add(item);
      	item = new JMenuItem("Open");
      	item.addActionListener(this);
      	fileMenu.add(item);

      	// create the 'quit' option
      	fileMenu.addSeparator();
      	item = new JMenuItem("Quit");
      	item.addActionListener(this);
      	fileMenu.add(item);
      }


      private void createStudentMenu(){
      	// create the menu
      	studentMenu = new JMenu("Student");
      	// declare a menu item (re-usable)
      	JMenuItem item;

      	item = new JMenuItem("Register");
      	item.addActionListener(this);
      	studentMenu.add(item);

      	item = new JMenuItem("Display");
      	item.addActionListener(this);
      	studentMenu.add(item);
      }


      private void createLessonMenu(){
      	// create the menu
      	lessonMenu = new JMenu("Lesson");
      	// declare a menu item (re-usable)
      	JMenuItem item;

      	item = new JMenuItem("Create");
      	item.addActionListener(this);
      	lessonMenu.add(item);

      	item = new JMenuItem("View");
      	item.addActionListener(this);
      	lessonMenu.add(item);
      }


       /** utility methods to make the code simpler */
      public void showMessage (String s){
      	JOptionPane.showMessageDialog(null,s);
      }


      public void showMessage (JTextArea s){
      	JOptionPane.showMessageDialog(null,s);
      }
}

INSTANTIABLE CLASS - STUDENTS

/** A class that models a Student of a driving school
*/


public class Students extends Lessons{

	private String name;
	private String address;
	private String number;
	private int noLessons;


	public Students() {
		name = "No Name Specified";
		address = "No Address Specified";
		number = "No Number Entered";
		noLessons = 0;
	}


	public Students (String name, String addr, String num, int noLes) {
		setName(name);
		setAddress(addr);
		setNumber(num);
		setNoLessons(noLes);

	}


	public String getName() {
		return name;
	}



    public String getAddress() {
		return address;
	}


    public String getNumber() {
    	return number;
    }


    public int getNoLessons() {
    	return noLessons;
    }



    public void setName(String name) {
    	name = name;
    }



    public void setAddress(String addr) {
    	address = addr;
    }


    public void setNumber(String num) {
    	number = num;
    }


    public void setNoLessons(int noLes) {
    	noLessons = noLes;
    }

    public String toString(){
    	return "\nName: " + name + "\nAddress: " + address + "\nPhone Number: " + number + "\nNumber of Lessons: " + noLessons;
    }

}

Thanks in Advance.

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
public void setName(String [COLOR="orange"][B]name[/B][/COLOR]) {

    	[COLOR="orange"][B]name [/B][/COLOR]= [COLOR="orange"][B]name[/B][/COLOR];

}
It all points to the variable of the parameter, the class variable 'name' never changes.
You need to use the this -keyword to point to the class variable

private String [COLOR="green"][B]name[/B][/COLOR];

...

public void setName(String [COLOR="orange"][B]name[/B][/COLOR]) {

    	[COLOR="green"][B]this.nam[/B]e[/COLOR] = [COLOR="orange"][B]name[/B][/COLOR];

}






2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users