Order Entry with background image

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 1871 Views - Last Post: 04 October 2010 - 07:29 PM Rate Topic: -----

#1 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

Order Entry with background image

Posted 02 October 2010 - 06:01 PM

Hello Everyone,
I am using this code for an Order Entry program.
I need to know how I can invoke a background image.
I tried the following for the Order program works fine but the background image is not appearing in the background. No errors present. Just image not displaying.
This is the code I am using for the image to appear in the background.

Image img = Toolkit.getDefaultToolkit().createImage("background.gif");

public void paint(Graphics g)
{
g.drawImage(img, 0, 0, null);
}

Can anyone tell me why is not appearing in the background?
I have the image in the same folder of the code.


import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class OrderEntry extends JFrame implements ActionListener, ItemListener
{
	final int BOOK_PRICE = 15;
	int totalPrice;
	String[] credit = {"Visa","Mastercard","ECheck","Paypal"};
	private Font bigFont = new Font("Arial",Font.BOLD,24);
	private JLabel title = new JLabel("Order Entry");
	private JTextField name = new JTextField("Name",5);
	private JTextField address = new JTextField("Address",10);
	private JTextField total = new JTextField(5);
	private JLabel totalLbl = new JLabel("Total:");
	private JButton enterData = new JButton("Submit Order");
	private JLabel success = new JLabel("Order Successful!");
	private JComboBox payment = new JComboBox(credit);
	private JCheckBox book1 = new JCheckBox("Devious Endeavors", false);
	private JCheckBox book2 = new JCheckBox("Pending Evolution", false);
	DataOutputStream ostream;
	final int WIDTH = 300;
	final int HEIGHT = 400;
	
	Image img = Toolkit.getDefaultToolkit().createImage("background.gif");

	public void paint(Graphics g)
	{
    	g.drawImage(img, 0, 0, null);
	}
	
	public OrderEntry()
	{
	super("Order Entry");
	try
	{
	ostream = new DataOutputStream(new FileOutputStream("OrderData.txt", true));
	}
	catch(IOException e)
	{
	System.err.println("File not opened.");
	System.exit(1);
	}
	
	setLayout(new GridLayout(10,1));
	title.setFont(bigFont);
	total.setText("$" + totalPrice);
	add(title);
	add(name);
	add(address);
	add(book1);
	add(book2);
	add(totalLbl);
	add(total);
	add(enterData);
	add(payment);
	add(success);
	enterData.addActionListener(this);
	book1.addItemListener(this);
	book2.addItemListener(this);
	setSize(WIDTH, HEIGHT);
	setVisible(true);
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	success.setVisible(false);
	}
	 
		
	public void actionPerformed(ActionEvent e)
	{
	try
	{
	ostream.writeUTF(name.getText());
	ostream.writeUTF(address.getText());
	ostream.writeUTF(total.getText());
	success.setVisible(true);
	}
	catch(IOException e2)
	{
	System.err.println("Unable to write to file.");
	System.exit(1);
	}
	}

	public void itemStateChanged(ItemEvent e)
	{
	Object source = e.getSource();
	int select = e.getStateChange();
	if(source == book1)
		if(select == ItemEvent.SELECTED)
		totalPrice += BOOK_PRICE;
		else
		totalPrice -= BOOK_PRICE;
	else if(source == book2)
		if(select == ItemEvent.SELECTED)
		totalPrice += BOOK_PRICE;
		else
		totalPrice -= BOOK_PRICE;
	total.setText("$" + totalPrice);
	}

	public static void main(String[] args)
	{
	OrderEntry frame = new OrderEntry();
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Order Entry with background image

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Order Entry with background image

Posted 02 October 2010 - 06:39 PM

Asked so many times...
Here is my CodeSnippet

http://www.dreaminco...snippet5599.htm
Was This Post Helpful? 0
  • +
  • -

#3 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

Re: Order Entry with background image

Posted 02 October 2010 - 07:54 PM

View Postpbl, on 02 October 2010 - 05:39 PM, said:

Asked so many times...
Here is my CodeSnippet

http://www.dreaminco...snippet5599.htm


I appreciate your help but seems you're not understanding what I am saying either.
I am not using a JPanel, I am using a JFrame. How can I invoke an image in the background.
It will not show in the background. Your snippet didn't work either. Look at my code and tell what to put in and where. I have tried the sample image code above, Please take a look at it and understand what I am saying. I am not seeing the background image on my end. See the attached file and see if you can see the background image on your end. The image is included in the zip file.

Thank you.

Attached File(s)


This post has been edited by bravo659: 02 October 2010 - 07:56 PM

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10444
  • View blog
  • Posts: 38,682
  • Joined: 27-December 08

Re: Order Entry with background image

Posted 02 October 2010 - 08:05 PM

The JComponents on the JFrame hide the background. Better to use a JPanel to paint the background onto, and add the JComponents to that JPanel as well. Then, add the JPanel to the JFrame.

Also, pbl's snippet works. It is for JPanel and other JComponents. JFrame extends AWT Frame, not Swing JComponent.
Was This Post Helpful? 1
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Order Entry with background image

Posted 02 October 2010 - 08:47 PM

View Postmacosxnerd101, on 02 October 2010 - 09:05 PM, said:

The JComponents on the JFrame hide the background. Better to use a JPanel to paint the background onto, and add the JComponents to that JPanel as well. Then, add the JPanel to the JFrame.

Also, pbl's snippet works. It is for JPanel and other JComponents. JFrame extends AWT Frame, not Swing JComponent.

so it should work understanding or not your problem :)
Was This Post Helpful? 0
  • +
  • -

#6 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

Re: Order Entry with background image

Posted 02 October 2010 - 09:15 PM

View Postpbl, on 02 October 2010 - 07:47 PM, said:

View Postmacosxnerd101, on 02 October 2010 - 09:05 PM, said:

The JComponents on the JFrame hide the background. Better to use a JPanel to paint the background onto, and add the JComponents to that JPanel as well. Then, add the JPanel to the JFrame.

Also, pbl's snippet works. It is for JPanel and other JComponents. JFrame extends AWT Frame, not Swing JComponent.

so it should work understanding or not your problem :)



Well you need to be a little more understanding with people trying to get some help. be clear like macosxnerd101.
He cleared up the air but telling me that JFrame hides the images so I need to use the JPanel best to the JFrame. Now I understand. Thanks macosxnerd101 for clearing up the air.
Was This Post Helpful? 0
  • +
  • -

#7 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

Re: Order Entry with background image

Posted 03 October 2010 - 02:06 AM

Hello pbl and macosxnerd101,
I finally got the project done and the graphics figured out.
I used the Jlabel to display the image on the window.
I appreciate for your help. Is only one line of code to have the image display on the JFrame window.

See the attachment for the image on the gui.
I also apologize for any misunderstanding. I am trying to learn java and is difficult for me to learn something I not exposed. I took other programming languages but only classroom experience and not so quite as good.

thanks for helping out.

Desi

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Order Entry with background image

Posted 03 October 2010 - 09:37 PM

Added that comment to my Code Snippet

Be careful if you want to apply this code to a JFrame. A JFrame is not really a JComponent and the JPanel contained in the JFrame will hide the background image of the JFrame anyhow.
Was This Post Helpful? 0
  • +
  • -

#9 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: Order Entry with background image

Posted 04 October 2010 - 03:52 AM

The Component that you CAN manipulate is called the ContentPane. You can perform method on it that are defined by the Component class.

Here's my tutorial on top-level components:
http://www.dreaminco...vel-components/
Was This Post Helpful? 0
  • +
  • -

#10 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

Re: Order Entry with background image

Posted 04 October 2010 - 04:48 PM

View Postpbl, on 03 October 2010 - 08:37 PM, said:

Added that comment to my Code Snippet

Be careful if you want to apply this code to a JFrame. A JFrame is not really a JComponent and the JPanel contained in the JFrame will hide the background image of the JFrame anyhow.


Thanks pbl, I appreciate for your feedback.
the problem is I don't know how to implement it with the JPanel haven't gotten that far until i get to the Advanced class and thats next year. I know how to use the JPanel for certain things but not all.

Thanks.

Desi
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Order Entry with background image

Posted 04 October 2010 - 04:52 PM

View PostDogstopper, on 04 October 2010 - 04:52 AM, said:

The Component that you CAN manipulate is called the ContentPane. You can perform method on it that are defined by the Component class.

Here's my tutorial on top-level components:
http://www.dreaminco...vel-components/

The problem is that the method() is general and receives a JComponent as parameter
Sure you can clone it to receive JFrame and then play with the ContentPane
Was This Post Helpful? 0
  • +
  • -

#12 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

Re: Order Entry with background image

Posted 04 October 2010 - 04:57 PM

View PostDogstopper, on 04 October 2010 - 02:52 AM, said:

The Component that you CAN manipulate is called the ContentPane. You can perform method on it that are defined by the Component class.

Here's my tutorial on top-level components:
http://www.dreaminco...vel-components/


I gonna practice more on the background part with images. I saw your tutorial it does set the background color no problem but my problem is the image background with the JPanel on the JFrame. Like let's say I create a

class Background extends JFrame{
ImagePanel panel = new ImagePanel(new ImageIcon("images/background.png").getImage());
JPanel panel = new JPanel();
panel.add(panel);
}

Something like this would work?

Desi
Was This Post Helpful? 0
  • +
  • -

#13 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: Order Entry with background image

Posted 04 October 2010 - 05:04 PM

No, you have to use the JFrame's panel...The one attached to the JFrame OR make sure you've added the JPanel to the JFrame.

Method 1:
class Background extends JFrame{
    ImagePanel panel = new ImagePanel(new ImageIcon("images/background.png").getImage());
    getContentPane().(panel);
}



OR Method 2:
class Background extends JFrame{
    ImagePanel panel = new ImagePanel(new ImageIcon("images/background.png").getImage());
    JPanel pan = new JPanel();
    pan.add(panel);
    add(pan);
}


This post has been edited by Dogstopper: 04 October 2010 - 05:37 PM
Reason for edit:: stupidity

Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Order Entry with background image

Posted 04 October 2010 - 05:33 PM

View PostDogstopper, on 04 October 2010 - 06:04 PM, said:

OR Method 2:

class Background extends JFrame{
ImagePanel panel = new ImagePanel(new ImageIcon("images/background.png").getImage());
JPanel panel = new JPanel();
panel.add(panel);
add(panel);
}


:)
Was This Post Helpful? 1
  • +
  • -

#15 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: Order Entry with background image

Posted 04 October 2010 - 05:38 PM

*facepalm*

I'm tired...I fixed it. Thanks for the catch.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2