Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Adding a Panel from a different class

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Location
    London
    Posts
    15
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Adding a Panel from a different class

    I'm having this problem figuring out how to add a Panel that has been set up in a different class into another class that has a Frame. Basically when the program starts the use just sees a Frame, they then select "new game" from the menu and what I want is a panel to show up with the Polygon Kanji game I've set up. When I make a new object of the class my panel is in, it pops up but in a seperate frame (not sized because I didn't set that up) that I had to put in because it was showing an error for doing repaint() which is needed for the polygons, so I know that I'm probably doing this wrong.
    I tried experimenting abit but I'm not getting anywhere, however I've never seen let alone done something like this and google is giving me programs that although similar are not what I want. Can please someone help me?

    This is my full code:

    import javax.swing.*;
    import java.awt.* ;
    import java.awt.event.* ;
    import java.util.* ;
     
     
    public class KanjiGame {
    		public static void main(String args[]) {
     
    		   new KanjiInterface ();
     
    		}
     
     
    }

    import javax.swing.* ;
    import java.awt.* ;
    import java.awt.event.* ;
    import java.util.* ;
     
    public class KanjiInterface extends JFrame implements ActionListener {
     
    	JMenuBar menuBar ;
    	JMenu menuSession ;
    	JMenuItem subNewGame, subLoadGame ;
    	KanjiRoku roku ;
     
     
    	KanjiInterface(){
    		//Creates the window
    		super("Kanji Game") ;
    		setLocation( new Point(100, 100) ) ;
    		//this.getContentPane().setBackground( Color.white ) ;
    		setSize( 600, 600 ) ;
    		setResizable( false ) ;
     
    		//Creates the Menu Bar along with sub-menus
    		menuBar = new JMenuBar() ;
    		menuSession = new JMenu("Session") ;
    		subNewGame = new JMenuItem("New Session") ;
    		subNewGame.addActionListener(this) ;
    		subLoadGame = new JMenuItem("Load Session") ;
    		menuBar.add(menuSession) ;
    		menuSession.add( subNewGame ) ;
    		menuSession.add( subLoadGame ) ;
    		this.setJMenuBar(menuBar) ;
    		/*
    		//Create Panel for graphics
    		graphArea = new MyComponent() ;
    		graphArea.setBackground(Color.WHITE) ;
    	 	add(graphArea, BorderLayout.CENTER) ;
    	 	*/
    	 	setVisible( true ) ; 	
    	 	addWindowListener( new WindowCatcher() ) ;
    	}	
     
    	public void actionPerformed( ActionEvent evt ){
    		if ( evt.getSource() == subNewGame ){
    			roku = new KanjiRoku() ;
    			//add(roku.graphArea, BorderLayout.CENTER) ;
    		}
    	}
     
    	class WindowCatcher extends WindowAdapter{
    		public void windowClosing( WindowEvent evt ){
    			evt.getWindow().dispose();
    			System.exit(0) ;
    		}
    	}
     
     
    	}

    import javax.swing.* ;
     
    import java.awt.* ;
    import java.awt.event.* ;
    import java.util.* ;
     
    public class KanjiRoku extends JFrame{
    	MyComponent graphArea ;
    	Color one = Color.blue ;
    	Color two = Color.blue ;
    	Color three = Color.blue ;
    	Color four = Color.blue ;
    	int firstx[]={275,325,325,275} ;
    	int firsty[]={100,100,175,175} ;
    	int secondx[]={150,450,450,150} ;
    	int secondy[]={180,180,230,230} ;
    	int thirdx[]={255,292,180,143} ;
    	int thirdy[]={263,300,412,375} ;
    	int fourthx[]={345,308,420,457} ;
    	int fourthy[]={263,300,412,375} ;
    	Polygon firstStroke = new Polygon(firstx,firsty,4) ;
    	Polygon secondStroke = new Polygon(secondx,secondy,4) ;
    	Polygon thirdStroke = new Polygon(thirdx,thirdy,4) ;
    	Polygon fourthStroke = new Polygon(fourthx,fourthy,4) ;
    	ArrayList<Integer> strokes = new ArrayList<Integer>() ;
    	int roku[]={1,2,3,4} ;
    	int taker ;
    	int result = 0 ;
    	Boolean complete = false ;
     
    	KanjiRoku(){	
    		//Create Panel for graphics
    		graphArea = new MyComponent() ;
    		graphArea.setBackground(Color.WHITE) ;
    	 	add(graphArea, BorderLayout.CENTER) ;
    	 	setVisible( true ) ;
     
     
    	 	//Adding listener
    	 	graphArea.addMouseListener(new MouseCatcher ()) ;
     
     
    	}
     
    	class MyComponent extends JPanel{
    	public void paintComponent(Graphics g){
    		super.paintComponent(g) ;
    	    g.setColor(one) ;
    	    g.fillPolygon(firstStroke) ;
    	    g.setColor(two) ;
    	    g.fillPolygon(secondStroke) ;	
    	    g.setColor(three) ;
    	    g.fillPolygon(thirdStroke) ;	
    	    g.setColor(four) ;
    	    g.fillPolygon(fourthStroke) ;	
     
    	}
    	}
     
    	public class MouseCatcher extends MouseAdapter {
     
    		public void mouseClicked(MouseEvent e)
    		{
     
        		int xpos = e.getX();
    			int ypos = e.getY();
    			if(firstStroke.contains(xpos,ypos)){
    			    one = Color.red ;
    			    strokes.add(1) ;
    			   // StrokeChecker() ;
    			    taker++ ;
    			}
    			if(secondStroke.contains(xpos,ypos)){
    			    two = Color.red ;
    			    strokes.add(2) ;
    			   // StrokeChecker() ;
    			    taker++ ;
    			}
    			if(thirdStroke.contains(xpos,ypos)){
    			    three = Color.red ;
    			    strokes.add(3) ;
    			  //  StrokeChecker() ;
    			    taker++ ;
    			}
    			if(fourthStroke.contains(xpos,ypos)){
    			    four = Color.red ;
    			    strokes.add(4) ;
    			  //  StrokeChecker() ;
    			    taker++ ;
    			}
    		    repaint() ;
    		    if(taker == 4){
    		    	StrokeChecker() ;
    		    }
    		    }
    		}
     
     
     
    	public void StrokeChecker(){
    	 int firstCom1 = strokes.get(0) ;
    	 int firstCom2 = roku[0] ;
    	 int secondCom1 = strokes.get(1) ;
    	 int secondCom2 = roku[1] ;
    	 int thirdCom1 = strokes.get(2) ;
    	 int thirdCom2 = roku[2] ;
    	 int fourthCom1 = strokes.get(3) ;
    	 int fourthCom2 = roku[3] ;
    	 if(firstCom1 == firstCom2){
    		 result += 1 ;
    	 }
    	 if(secondCom1 == secondCom2){
    		 result += 1 ;
    	 }
    	 if(thirdCom1 == thirdCom2){
    		 result += 1 ;
    	 }
    	 if(fourthCom1 == fourthCom2){
    		 result += 1 ;
    	 }
    	 switch(result) {
    	 case 0:
    		 JOptionPane.showMessageDialog( null, "Not even one right...", "Result", JOptionPane.PLAIN_MESSAGE ) ;
    		 break ;
    	 case 1:
    		 JOptionPane.showMessageDialog( null, "You need more practice", "Result", JOptionPane.PLAIN_MESSAGE ) ;
    		 break ;
    	 case 2:
    		 JOptionPane.showMessageDialog( null, "Good try", "Result", JOptionPane.PLAIN_MESSAGE ) ;
    		 break ;
    	 case 3:
    		 JOptionPane.showMessageDialog( null, "Nearly! Try again!", "Result", JOptionPane.PLAIN_MESSAGE ) ;
    		 break ;
    	 case 4:
    		 JOptionPane.showMessageDialog( null, "You did it!", "Result", JOptionPane.PLAIN_MESSAGE ) ;
    		 break ;
     
    	 }
    	 }
     
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,357
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: Adding a Panel from a different class

    I'm not really sure what your question is, but if you want to pass a reference between classes, simply call a getter or a setter with that JPanel.

    It sounds like CardLayout might be your friend here. But again, I'm not really sure what the question is, so that's just a guess.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Location
    London
    Posts
    15
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Adding a Panel from a different class

    I want to know is there is a way I can put the panel I have created in my KanjiRoku class into the frame that is in KanjiInterface? I could try the CardLayout but I want to see if its possible to try it this way first which would make things easier for me in the long run.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,357
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: Adding a Panel from a different class

    I guess I'm just confused by what's stopping you. Just add a getter or a setter to pass the JPanel, then add it wherever you want.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Feb 2011
    Location
    London
    Posts
    15
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Adding a Panel from a different class

    Really? Is it that easy? I'll try it out, thank you very much!

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,357
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: Adding a Panel from a different class

    Quote Originally Posted by DudeJericho View Post
    Really? Is it that easy? I'll try it out, thank you very much!
    Well yeah, the other half is to actually add it to the JFrame- which, if I'm understanding your question right, is where CardLayout comes into play.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Feb 2011
    Location
    London
    Posts
    15
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Adding a Panel from a different class

    Oh, my intention had nothing to do with CardLayout, my original idea was that there would be a called to the JPanel via a constructor when the user clicks on "New Session" via JMenu, however that idea didn't work and I was wondering how it could be done. If I have to use CardLayout then can I still use JMenu to get the Panel?

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,357
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: Adding a Panel from a different class

    If you want the JPanel to appear and disappear, then just set its visibility via the menu. If you want different JPanels to be in the same location at different times, then you want CardLayout. Either way, you have to pass the JPanel into the class that's going to be using it.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member
    Join Date
    Feb 2011
    Location
    London
    Posts
    15
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Adding a Panel from a different class

    Thank you for your advice but I've decided to try something a bit more simpler, seems to be working alright.

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,357
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: Adding a Panel from a different class

    Out of curiosity, what did you try?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Adding data to a text box on another class
    By Qualitystreet in forum AWT / Java Swing
    Replies: 3
    Last Post: April 5th, 2011, 02:38 AM
  2. Adding fixed size picture and button to panel
    By Javabeginner in forum AWT / Java Swing
    Replies: 10
    Last Post: August 23rd, 2010, 06:07 PM
  3. Adding panels to a central panel.
    By Johannes in forum AWT / Java Swing
    Replies: 3
    Last Post: July 4th, 2010, 05:31 PM
  4. CashRegister class- adding a method getItemCount
    By etidd in forum Java Theory & Questions
    Replies: 2
    Last Post: January 21st, 2010, 07:29 PM
  5. adding get mothods to a class extending thread
    By aliaa2a in forum Object Oriented Programming
    Replies: 6
    Last Post: August 3rd, 2009, 06:41 AM