Avatar of CharleneS77
CharleneS77
Flag for United States of America asked on

What am I not doing correctly here to paint?

I am able to paint an image using the ImagePanelMain class, yet I am not able to paint an image using the SodaMachineWindowMain class.  The SodaMachineWindowMain class will be used to run the whole program.

I have tried two separate scenarios:

Run the ImagePanelMain class.
Call the displayImage method from the ImagePanel constructor.
If I pass a 1 into the displayImage method from the constructor, a bottle is displayed.
If I pass a 2 into the displayImage method from the constructor, a can is displayed.
So I know the ImagePanel works.

Run the SodaMachineWindowMain class.
Call displayImage method from the buyBottleButtonPressed method or buyCanButtonPressed method in the ComponentPanel class.
When the buyCanButton or buyBottleButton is pressed (in the ComponentPanel class), the output from the System.out.println statements is changing, yet the image does not change.
So I know the displayImage method in the ImagePanel class is being accessed from the SodaMachineWindow class.  

The only problem I have is that the image is not showing up.  Any suggestions?

Please let me know if I need to post more code.

//////////////////////////////////////////////////////
//////////////////////////////////////////////////////
//ComponentPanel class
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
 
//***************************************************************
class ComponentPanel extends JPanel/*implements ActionListener*/
{
	//---------------------------constants--------------------------
	private static final int COMPONENT_PANEL_HEIGHT = 173;
	private static final int COMPONENT_PANEL_WIDTH = 356;
 
	//-----------------------------instance variables----------------------
	private String [ ] bills = { "ONE", "FIVE", "TEN", "TWENTY" }; 
	//----------------------componentPanel----------------------
	private JPanel componentPanel = new JPanel( ); // holds all component panels
	//----------------------depositPanel------------------------
	private JPanel depositPanel = new JPanel( ); 
			//-----depositBillPanel-----
	private JPanel depositBillPanel = new JPanel( );
	private JComboBox billSelector = new JComboBox( bills ); // JComboBox
	private JButton depositBillButton = new JButton( "Deposit Bill" );
			//-----depositCoinPanel-----
	private JPanel depositCoinPanel = new JPanel( ); 	
	private JRadioButton nickelButton = new JRadioButton( "Nickel", false );
	private JRadioButton dimeButton = new JRadioButton( "Dime", false );
	private JRadioButton quarterButton = new JRadioButton( "Quarter", true );
	private ButtonGroup radioButtonGroup = new ButtonGroup ( );		
	private JPanel radioButtonPanel = new JPanel( );
	private JButton depositCoinButton = new JButton( "Deposit Coin" );
			//-----amountDepositedPanel-----
	private JPanel amountDepositedPanel = new JPanel( ); 
	private JTextField totalAmountDepositedText = new JTextField( "0", 4 );		
	//------------------------buyPanel--------------------------
	private JPanel buyPanel = new JPanel( );
	//private JPanel buyCanPanel = new JPanel( );
	private JButton buyCanButton = new JButton( "Buy Can" );
	//private JPanel buyBottlePanel = new JPanel( );
	private JButton buyBottleButton = new JButton( "Buy Bottle" );
	//----------------------messagePanel------------------------
	private JPanel messagePanel = new JPanel( );
	private JTextField messageText = new JTextField( "Input coins&", 32 );
	//----------------------------------------------------------
	
	private ImagePanel imagePanel = new ImagePanel( );
	
	private int bottle = 1;
	private int can = 2;
	private int one = 3;
	private int five = 4;
	private int ten = 5;
	private int twenty = 6;
	
	//*****************************************************************
	// constructor method
	ComponentPanel( )
	{
		billSelector.setToolTipText( "Select a bill" );
		depositBillButton.setToolTipText( "Click to deposit a bill." );
		nickelButton.setToolTipText( "Select a coin" );
		dimeButton.setToolTipText( "Select a coin" );
		quarterButton.setToolTipText( "Select a coin" );
		depositCoinButton.setToolTipText( "Click to deposit a coin." );
		buyCanButton.setToolTipText( "Click to buy a can." );
		buyBottleButton.setToolTipText( "Click to buy a bottle." );
	
		messageText.setEditable( false );
		messageText.setBackground( Color.WHITE );
		totalAmountDepositedText.setEditable( false );
		totalAmountDepositedText.setBackground( Color.WHITE );
		
		billSelector.addActionListener( new ActionListener( )
		{ 
			public void actionPerformed( ActionEvent e )
			{ 
				// billSelected( );
				/*
				if one
				
				if five
				
				if ten
				
				if twenty
				*/
			}
		}
		);
		
		depositCoinButton.addActionListener( new ActionListener( )
		{ 
			public void actionPerformed( ActionEvent e )
			{ 
				// depositCoinButtonPressed( );
				/*
				if quarter
					update total amount deposited
					draw quarter
				if dime
					update total amount deposited
					draw dime
				if nickel
					update total amount deposited
					draw nickel
				*/
			}
		}
		);
	
		////////////////////////////////////////////////////////////
	
		depositBillButton.addActionListener( new ActionListener( )
		{ 
			public void actionPerformed( ActionEvent e )
			{ 
				// depositBillButtonPressed( );
				/*
				if one bill
					draw one dollar bill
				if five dollar bill
					draw five dollar bill
				if ten dollar bill
					draw ten dollar bill
				if twenty dollar bill
					draw twenty dollar bill
				update the total amount deposited
				*/
			}
		}
		);
		/////////////////////////////////////////////////////////////
	
		buyCanButton.addActionListener( new ActionListener( )
		{ 
			public void actionPerformed( ActionEvent e )
			{ 
				buyCanButtonPressed( );
	
				/*
				check for insuffient funds (check total amount deposted)
					if insufficient funds
						display insufficient funds message in text field
						
				check to see if the product is sold out
					if product is sold out
						display sold out message in text field
						
				if suffient funds (at least 60 cents)
					draw can
					update total amount deposited
					display a purchase successful message									
				*/
			}
		}
		);
		///////////////////////////////////////////////////////////////
	
		buyBottleButton.addActionListener( new ActionListener( )
		{ 
			public void actionPerformed( ActionEvent e )
			{ 
				buyBottleButtonPressed( );
				
				/*
				check for insuffient funds (check total amount deposted)
					if insufficient funds
						display insufficient funds message in text field
						
				check to see if the product is sold out
					if product is sold out
						display sold out message in text field
						
				if suffient funds (at least 60 cents)
					draw bottle
					update total amount deposited
					display a purchase successful message										
				*/
			}
		}
		);
			
		radioButtonGroup.add( nickelButton );
		radioButtonGroup.add( dimeButton );
		radioButtonGroup.add( quarterButton );
			
		radioButtonPanel.setLayout( new GridLayout( 1, 3 ) );
		radioButtonPanel.add( nickelButton );
		radioButtonPanel.add( dimeButton );
		radioButtonPanel.add( quarterButton );
		//radioPanel.setBorder( BorderFactory.createTitledBorder(
		//						BorderFactory.createEtchedBorder( ), //"Coin?"////"" ) );
		
		//----------------------depositPanel------------------------
				//---depositBillPanel---		
		depositBillPanel.add( billSelector );
		depositBillPanel.add( depositBillButton );
				//---depositCoinPanel---
		/*
		depositCoinPanel.add( nickelButton );
		depositCoinPanel.add( dimeButton );
		depositCoinPanel.add( quarterButton );
		*/
		depositCoinPanel.add( radioButtonPanel );
		depositCoinPanel.add( depositCoinButton );		
				//---amountDepositedPanel---
		amountDepositedPanel.add( totalAmountDepositedText );
		
		// add panels to depositPanel
		depositPanel.setLayout( new BorderLayout( ) );
		depositPanel.add( depositBillPanel, BorderLayout.NORTH );
		depositPanel.add( depositCoinPanel, BorderLayout.SOUTH );
		depositPanel.add( amountDepositedPanel, BorderLayout.EAST );
		//------------------------buyPanel-----------------------
		buyPanel.setLayout( new BorderLayout ( ) );
		buyPanel.add( buyCanButton, BorderLayout.NORTH );
		buyPanel.add( buyBottleButton, BorderLayout.CENTER );
		buyPanel.setLayout( new GridLayout( 2, 1 ) );	
		//----------------------messagePanel------------------------
		messagePanel.setLayout( new BorderLayout( ) );
		messagePanel.add( messageText, BorderLayout.NORTH );
	
		// add panels to componentPanel		
		componentPanel.setLayout( new BorderLayout( ) );
		componentPanel.add( depositPanel, BorderLayout.NORTH );
		componentPanel.add( buyPanel, BorderLayout.CENTER );
		componentPanel.add( messagePanel, BorderLayout.SOUTH );
		
		// add panels to JFrame
		this.setLayout( new BorderLayout( ) );
		this.add( componentPanel, BorderLayout.WEST );		
	}
	
	
	public void buyBottleButtonPressed( )
	{
	
		imagePanel.displayImage( bottle );
		//repaint( );
	}
	
	public void buyCanButtonPressed( )
	{
		imagePanel.displayImage( can );
		//repaint( );
	}
	
	
	public Dimension getPreferredSize( )
	{
		return new Dimension( COMPONENT_PANEL_WIDTH, COMPONENT_PANEL_HEIGHT );
	}
	
	public Dimension getMinimumSize( )
	{
		return getPreferredSize( );
	}
	
}
 
 
 
 
 
//////////////////////////////////////////////////////
//////////////////////////////////////////////////////
//ImagePanel class
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
 
//***************************************************************
// ImagePanel class
class ImagePanel extends JPanel
{
	//---------------------------constants--------------------------
	public static final int IMAGE_PANEL_HEIGHT = 500;
	public static final int IMAGE_PANEL_WIDTH = 500;
 
	//-----------------------------instance variables---------------
	private JPanel imagePanel = new JPanel( ); // holds all image panels
		
	private SodaBottle sodaBottle = new SodaBottle( );
	private SodaCan sodaCan = new SodaCan( );
	private OneBill oneBill = new OneBill( );
	private FiveBill fiveBill = new FiveBill( );
	private TenBill tenBill = new TenBill( );
	private TwentyBill twentyBill = new TwentyBill( );
		
	//*****************************************************************
	// constructor method
	ImagePanel( )
	{
		this.setBackground( Color.WHITE );
		
		//displayImage( 1 );
	
	}// end constructor method
	
	
	// displayImage method
	public void displayImage( int passedChoice )
	{
		System.out.println( "\ndisplayImage method \n" );
	
		//this.setBackground( Color.WHITE );
		
		int choice = passedChoice;
		System.out.println( "\nchoice = " + choice );
	
		// add image to imagePanel	
		switch( choice )
		{
			case 1:		System.out.println( "\ncase 1" );
							this.add( sodaBottle );
							break;
							
			case 2:		System.out.println( "\ncase 2" );
							this.add( sodaCan );
							break;	
 
			case 3:		this.add( oneBill );
							break;
							
			case 4:		this.add( fiveBill );
							break;
 
			case 5:		this.add( tenBill );
							break;
							
			case 6:		this.add( twentyBill );
							break;		
			
			//default
		}
		repaint( );
	}// end constructor method
 
	
	// getPreferredSize method
	public Dimension getPreferredSize( )
	{
		return new Dimension( IMAGE_PANEL_WIDTH, IMAGE_PANEL_HEIGHT );
	}// end getPreferredSize method
	
	// getMinimumSize method
	public Dimension getMinimumSize( )
	{
		return getPreferredSize( );
	}// end getMinimumSize method
}// end ImagePanel class
 
 
 
 
 
//////////////////////////////////////////////////////
//////////////////////////////////////////////////////
//ImagePanelMain class
import javax.swing.*;
 
//***************************************************************
// ImagePanelMain class
class ImagePanelMain extends JFrame
{
	// main method
	public static void main( String[] args )
	{
		JFrame imagePanel = new JFrame( "Image Panel" );
		imagePanel.getContentPane().add( new ImagePanel( ) );
		imagePanel.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		//imagePanel.setSize( 650, 650 ); // an alternative is to use pack( )
		imagePanel.pack( );					 // an alternative is to use setSize( )
		//imagePanel.setResizable( false );
		imagePanel.show( );
	}// end main
	//==========================================================	
}// end ComponentPanelMain class
//***************************************************************
 
 
 
 
 
//////////////////////////////////////////////////////
//////////////////////////////////////////////////////
//SodaMachineWindow class
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
 
//***************************************************************
class SodaMachineWindow extends JPanel/*implements ActionListener*/
{
	/* 
		The width of the leftPanel is 366
		The height of the leftPanel is 510	
		The width of the rightPanel is 510
		The height of the rightPanel is 510		
	*/
	//---------------------------constants--------------------------
	private static final int SODA_MACHINE_WINDOW_WIDTH = 886;
	private static final int SODA_MACHINE_WINDOW_HEIGHT = 517;
	
	//-----------------------------instance variables----------------------	
	private JPanel sodaMachineWindow = new JPanel( ); // holds component panel and image panel
	private LeftPanel leftPanel = new LeftPanel( );
	private RightPanel rightPanel = new RightPanel( );
		
	//*****************************************************************
	// constructor method
	SodaMachineWindow( )
	{
		this.setBackground( Color.YELLOW );
		//this.setLayout( new GridLayout( 1, 2 ) );
		this.add( leftPanel );
		this.add( rightPanel );
		
	}	
	public Dimension getPreferredSize( )
	{
		return new Dimension( SODA_MACHINE_WINDOW_WIDTH, SODA_MACHINE_WINDOW_HEIGHT );
	}
	
	public Dimension getMinimumSize( )
	{
		return getPreferredSize( );
	}	
}
 
 
 
 
 
//////////////////////////////////////////////////////
//////////////////////////////////////////////////////
//SodaMachineWindowMain class
import javax.swing.*;
 
//***************************************************************
// SodaMachineWindowMain class
class SodaMachineWindowMain extends JFrame
{
	// main method
	public static void main( String[] args )
	{
		JFrame sodaMachineWindow = new JFrame( "Soda Machine Window" );
		sodaMachineWindow.getContentPane().add( new SodaMachineWindow( ) );
		sodaMachineWindow.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		//sodaMachineWindow.setSize( 650, 650 ); // an alternative is to use pack( )
		sodaMachineWindow.pack( );					  // an alternative is to use setSize( )
		sodaMachineWindow.setResizable( false );
		sodaMachineWindow.show( );
	}// end main
	//==========================================================	
}// end ComponentPanelMain class
//***************************************************************

Open in new window

Java

Avatar of undefined
Last Comment
CharleneS77

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Mick Barry

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
CharleneS77

ASKER
I should have also given this scenario:

Use an explicit value in the ImagePanel constructor to call the displayImage method.
Run the SodaMachineWindowMain.
The image is displayed.
CharleneS77

ASKER
My imagePanel gets added to my rightPanel.
My componentPanel gets added to my leftPanel.
My rightPanel gets added to my SodaMachineWindow.
My leftPanel gets added to my SodaMachineWindow.
Mick Barry

call revalidate(); before calling repaint();

Your help has saved me hundreds of hours of internet surfing.
fblack61
CharleneS77

ASKER
Where?
Mick Barry

before the call to repaint() in displayImage()
CharleneS77

ASKER
I put a call to revalidate( ) before repaint( ), and I still do not see an image when the button is pressed.  
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
CharleneS77

ASKER
Thank you for pointing me in the right direction.