Page 1 of 2 12 LastLast
Results 1 to 20 of 39
Like Tree2Likes

Thread: Adding multiple components to 1 single frame/panel

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

    Default Adding multiple components to 1 single frame/panel

    Title says it all i want to get these two components:

    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(300, 300));
    		
    		}
    
    	
    			public void paintComponent(Graphics g) {
    				super.paintComponent(g);
    				setBackground(Color.GREEN);
    				
    				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.*;
    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 MatWood(){
    		setPreferredSize(new Dimension(300, 300)); // Panel needs a size;
    	}
    	
    	@Override
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		g.setColor(Color.BLACK);
    		g.fillRect(woodX, woodY, woodWidth, woodHeight);
    	}
    }
    To the frame/panel what this class creates:

    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.setBackground(Color.GREEN);
        	frame.pack();
        	frame.setVisible(true);
        	
        	//make sure the JPanel has the focus
        	gameWorld.requestFocusInWindow();
        	
        }
    }

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

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

    The title says nothing. What problem are you having?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

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

    Can you explain what problems you are having?
    If you don't understand my response, don't ignore it, ask a question.

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

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

    if you try those classes out you can see that the code adds 2 different areas, one has red background and one has white. so that's the problem i don't want that to happen

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

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

    i don't want that to happen
    What do you want to happen?
    If you don't understand my response, don't ignore it, ask a question.

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

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

    so that there is ONLY the red background and that the both objects are showing

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

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

    When I execute the code, I get two sections shown, one above the other. The top one is green and the bottom one is gray. The green one has a red circular shape. The gray one has a black rectangle

    I don't see red or white sections.
    If you don't understand my response, don't ignore it, ask a question.

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

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

    i must have remembered it wrong

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

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

    Ok, now what is it you want to be shown?
    If you don't understand my response, don't ignore it, ask a question.

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

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

    green background and the both red and gray objects

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

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

    Look at the code that was posted. Does one of the panels do what you want? Copy the technique from that one to the other one so they both do the same thing.
    If you don't understand my response, don't ignore it, ask a question.

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

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

    No...

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

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

    No...
    Please explain. I have no idea what that means.
    Nozz likes this.
    If you don't understand my response, don't ignore it, ask a question.

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

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

    the GameWorld class does what i want it to do but when i copy that part to the other class nothing happens

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

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

    when i copy that part to the other class nothing happens
    What is "that part"?
    What does "nothing happens" mean?

    Make a small, complete program that compiles, executes and shows the problem.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

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

    Quote Originally Posted by Norm View Post
    What is "that part"?
    What does "nothing happens" mean?

    Make a small, complete program that compiles, executes and shows the problem.
    (containing the modified code where "nothing happens")
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

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

    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)); //this row does what i want it to do but if i copy this to the other one it fails
    		
    		}
    
    	
    			public void paintComponent(Graphics g) {
    				super.paintComponent(g);
    				setBackground(Color.GREEN);
    				
    				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
    				
    			}
    
    
    
    }
    This happens when i copy that technique told above to the MatWood class:
    Adding multiple components to 1 single frame/panel-problem.png

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

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

    What about the code in the main() method that sets the color for the top component? Did you add code there to set the color of the bottom component?
    If you don't understand my response, don't ignore it, ask a question.

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

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

    Okay i set it but nothing happened (i set the rect color to brown with:

    Java Code:
    	public Color woodColor = new Color(35, 30, 0);
    and

    Java Code:
    g.setColor(woodColor);
    But nothing different happens
    Last edited by NitroDev; 04-18-2014 at 01:21 PM.

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

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

    Look at all the code. Where are there references to "Color.GREEN"? Make sure the statements using Color.GREEN are done for both components in the same way.

    BtW setBackground() should NOT be called from inside the paintComponent() method.
    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

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
  •