Page 2 of 2 FirstFirst 12
Results 21 to 39 of 39
Like Tree2Likes

Thread: Adding multiple components to 1 single frame/panel

  1. #21
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: Adding multiple components to 1 single frame/panel

    BtW setBackground() should NOT be called from inside the paintComponent() method.
    Then where?

  2. #22
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: Adding multiple components to 1 single frame/panel

    The constructor if it is to be done only once.
    If you don't understand my response, don't ignore it, ask a question.

  3. #23
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: Adding multiple components to 1 single frame/panel

    the constructor you mean under "public MatWood()" or under "public class MatWood extends JPanel"

  4. #24
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: Adding multiple components to 1 single frame/panel

    "public MatWood()"
    That looks like the definition for the constructor. The other looks like the definition for the class.
    If you don't understand my response, don't ignore it, ask a question.

  5. #25
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,431
    Blog Entries
    7
    Rep Power
    20

    Default Re: Adding multiple components to 1 single frame/panel

    Quote Originally Posted by NitroDev View Post
    the constructor you mean under "public MatWood()" or under "public class MatWood extends JPanel"
    The first one, the second one is a class; you do know what a constructor is do you? Did you write that code yourself?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #26
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: Adding multiple components to 1 single frame/panel

    Okay and jos i did write the code by myself but i'm learning at the same time so partially from forums partially from me. and Norm that doesn't solve my problem i want the rectangle to be on the same panel/background as the red circle

  7. #27
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: Adding multiple components to 1 single frame/panel

    Post the current code so we can see what it does.

    Is this what you are trying to get?
    Attached Thumbnails Attached Thumbnails Adding multiple components to 1 single frame/panel-greenpanels.png  
    Last edited by Norm; 04-24-2014 at 08:39 PM.
    If you don't understand my response, don't ignore it, ask a question.

  8. #28
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: Adding multiple components to 1 single frame/panel

    Exactly that and here's the code:
    Java Code:
    package TestVersion;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    
    import javax.swing.JFrame;
    
    import TestVersion.CKeyListener;
    import TestVersion.GameWorld;
    import TestVersion.MatWood;
    
    public class MYCoreWorld {
    	
    	
        public static void main(String[] args) {   	
        	
        	
        	JFrame frame = new JFrame("My Core 0.0.1a");
        	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        	
        	GameWorld gameWorld = new GameWorld();
        	//make sure that the panel can detect keyevents
        	gameWorld.setFocusable(true);
        	gameWorld.setBackground(Color.GREEN);
        	
        	MatWood matWood = new MatWood();
        	
        	CKeyListener listener = new CKeyListener(gameWorld);
        	gameWorld.addKeyListener(listener);
        	
        	frame.add(matWood, BorderLayout.SOUTH);   	
        	frame.add(gameWorld, BorderLayout.CENTER);
        	
        	frame.isOpaque();
        	frame.setBackground(Color.GREEN);
        	frame.pack();
        	frame.setVisible(true);
        	
        	//make sure the JPanel has the focus
        	gameWorld.requestFocusInWindow();
        	
        }
    }
    Java Code:
    package TestVersion;
    
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    
    import javax.swing.JPanel;
    
    public class GameWorld extends JPanel {
    	
    	private int charX = 225;
    	private int charY = 225;
    	private int charDiameter = 25;
    	private int charDeltaX = -1;
    	private int charDeltaY = 3;
    	public Color playerColor = new Color(255,0,0);
    	private int noseX = 236;
    	private int noseY = 221;
    	private int noseWidth = 4;
    	private int noseHeight = 5;
    	
    	public void changePlayerColor(Color YELLOW){	
    		    this.playerColor = YELLOW;
    		    repaint();
    		}
    
    	public GameWorld() {
    		   setPreferredSize(new Dimension(500, 500));
    		   setBackground(Color.GREEN);
    		}
    
    			@Override
    			public void paintComponent(Graphics g) {
    				super.paintComponent(g);
    		
    				g.setColor(playerColor);
    				g.fillOval(charX, charY, charDiameter, charDiameter);
    				g.fillRect(noseX, noseY, noseWidth, noseHeight);
    			}
    			public void walkCharLeft() {
    				charX-=5;
    				repaint();
    				noseX-=5;
    				
    				
    			}
    			public void walkCharRight() {
    				charX+=5;
    				noseX+=5;
    				repaint();
    			}
    			public void walkCharUp() {
    				charY-=5;
    				noseY-=5;
    				repaint();
    			}
    			public void walkCharDown() {
    				charY+=5;
    				noseY+=5;
    				repaint();
    			}
    			public void runCharLeft() {
    				charX-=10;
    				noseX-=10;
    				repaint();
    				
    			}
    			public void runCharRight() {
    				charX+=10;
    				noseX+=10;
    				repaint();
    				
    			}
    			public void runCharUp() {
    				charY-=10;
    				noseY-=10;
    				repaint();
    				
    			}
    			public void runCharDown() {
    				charY+=10;
    				noseY+=10;
    				repaint();
    				
    			}
    
    
    
    			public void setColor(Color color) {
    				// TODO Auto-generated method stub
    				
    			}
    
    
    
    }
    Java Code:
    package TestVersion;
    
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    
    import javax.swing.JPanel;
    
    public class MatWood extends JPanel{
    	
    	private int woodX = 250;
    	private int woodY = 100;
    	private int woodWidth = 10;
    	private int woodHeight = 50;
    	public Color woodColor = new Color(35, 30, 0);
    	
    	public MatWood(){
    		setPreferredSize(new Dimension(500, 500)); // Panel needs a size;
    		
    	}
    	
    
    	@Override
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		g.setColor(woodColor);
    		g.fillRect(woodX, woodY, woodWidth, woodHeight);
    		
    		
    	}
    }

  9. #29
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: Adding multiple components to 1 single frame/panel

    One panel works: it sets its color to green. The other panel does not go green.
    What is different? Look at all the lines of code that contain: Color.GREEN and see what is different.
    If you don't understand my response, don't ignore it, ask a question.

  10. #30
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: Adding multiple components to 1 single frame/panel

    Okay but i want justa single panel not two

  11. #31
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: Adding multiple components to 1 single frame/panel

    i want justa single panel not two
    That sounds like a new problem. I thought the problem was to have the two panels be the same color.
    That will be a significant rewrite of the code.
    If you don't understand my response, don't ignore it, ask a question.

  12. #32
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: Adding multiple components to 1 single frame/panel

    significant
    Really???

  13. #33
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: Adding multiple components to 1 single frame/panel

    Go ahead and see what it takes.
    If you don't understand my response, don't ignore it, ask a question.

  14. #34
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: Adding multiple components to 1 single frame/panel

    how?

  15. #35
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: Adding multiple components to 1 single frame/panel

    Use an editor and a compiler.
    If you don't understand my response, don't ignore it, ask a question.

  16. #36
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: Adding multiple components to 1 single frame/panel

    i mean where can i get to learn how to fix it (the new problem)

  17. #37
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: Adding multiple components to 1 single frame/panel

    learn how to fix it
    Are you asking how to learn how to write programs?
    Read and practice.

    The tutorial is a good resource: http://docs.oracle.com/javase/tutori...ybigindex.html
    If you don't understand my response, don't ignore it, ask a question.

  18. #38
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: Adding multiple components to 1 single frame/panel

    i have searched all of the internet (not literally) for a answer to my problem and found NOTHING

  19. #39
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: Adding multiple components to 1 single frame/panel

    I wouldn't expect there to be the solution you are looking for. Not all of the possible programming problems have been posted on the internet (yet).

    Your problem may require you to learn how to write Swing GUI.
    gimbal2 likes this.
    If you don't understand my response, don't ignore it, ask a question.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Adding swing components to a tabbed panel
    By ras_oscar in forum New To Java
    Replies: 2
    Last Post: 08-25-2013, 04:18 AM
  2. Adding multiple components to 1 container index
    By mmcnitt in forum AWT / Swing
    Replies: 3
    Last Post: 03-07-2011, 03:59 PM
  3. Adding components to a panel
    By jboy in forum New To Java
    Replies: 1
    Last Post: 10-10-2009, 01:02 PM
  4. adding components to frame
    By roaan in forum New To Java
    Replies: 4
    Last Post: 07-05-2009, 03:30 PM
  5. Replies: 6
    Last Post: 07-28-2008, 06:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •