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

How do I pass an object through a constructor?

I have a SodaMachineWindow class with a main method that creates a sodaMachineWindow.
I have a LeftPanel class and a RightPanel class. A leftPanel and rightPanel are added to the sodaMachineWindow.
I have a ComponentPanel class.  A componentPanel is added to the leftPanel.
I have a ImagePanel class.  An imagePanel is added to the rightPanel.

I am attempting to use a button in the componentPanel to display a painted icon on the imagePanel.
I have instantiated an ImagePanel object in my SodaMachineClass.  
I need to pass the ImagePanel object down to the ComponentPanel through the constructors so I have a reference to the real ImagePanel inside my ComponentPanel class.  
How do I accomplish this?
Java

Avatar of undefined
Last Comment
Muhammad Khan

8/22/2022 - Mon
silemone

If you're building this all in main, then you would pass it there....
meaning create a constructor in component Panel pass the image panel...


i.e.  ConstructionPanel  p = new ConstructionPanel(new ImagePanel i)
then changes to the image panel can be made in the Main be creating another ImagePanel with a reference...
ImagePanel P = p.getImagePanel();

///the contructionpanel class
ImagePanel p;
ConstructionPanel (ImagePanel p)
{
this.p = p;
}

ASKER CERTIFIED SOLUTION
Muhammad Khan

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.
silemone

oopss...to clean up a bit...
in main class:
ConstructionPanel  p = new ConstructionPanel(new ImagePanel ("c:\image.jpg");
/*make changes accordingly in the Control Panel class....then changes to the image panel can be made in the Main be creating another ImagePanel with a reference...make an accessor -- getImagePanel()*/
ImagePanel P = p.getImagePanel();  //the reference

///the contructionpanel class
ImagePanel p;
ConstructionPanel (ImagePanel p)
{
this.p = p;
}
CharleneS77

ASKER
>>>>ConstructionPanel  p = new ConstructionPanel(new ImagePanel i)

Was that meant to be ComponentPanel, or was ConstructionPanel just used to show an example?
Where is this created?

>>>>ImagePanel P = p.getImagePanel();

Where is this created?

I should clarify that I have a SodaMachineWindowMain class with the main method and I also have a SodaMachineWindow class.  The code for these classes is below.  I have eliminated the LeftPanel class and RightPanel class to simplify things.  So I now have the componentPanel and imagePanel being added directly to the sodaMachineWindow.


//-------------------------
// SodaMachineWindowMain class
//-------------------------
import javax.swing.*;
 
class SodaMachineWindowMain extends JFrame
{
	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.pack( );					  // an alternative is to use setSize( )
		sodaMachineWindow.setResizable( false );
		sodaMachineWindow.show( );
	}	
}
 
//-------------------------
// SodaMachineWindow class
//-------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
 
class SodaMachineWindow extends JPanel
{
	public static final int SODA_MACHINE_WINDOW_WIDTH = 886;
	public static final int SODA_MACHINE_WINDOW_HEIGHT = 517	private ImagePanel imagePanel = new ImagePanel( );
	private ComponentPanel componentPanel = new ComponentPanel( );
	
	SodaMachineWindow( )
	{
		this.setBackground( Color.YELLOW );
		this.add( componentPanel );
		this.add( imagePanel );		
	}	
	
	public Dimension getPreferredSize( )
	{
		return new Dimension( SODA_MACHINE_WINDOW_WIDTH, SODA_MACHINE_WINDOW_HEIGHT );
	}
	
	public Dimension getMinimumSize( )
	{
		return getPreferredSize( );
	}	
}

Open in new window

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
CharleneS77

ASKER
Thank you for a nice, clear answer.
silemone

oops...it was componentPanel.  I'm at work and was rushing...i gave you the same code as aiklamba 'cept for  typos...
Muhammad Khan

i am not aiklamba.... i am aiklamha :p another typo
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.