Results 1 to 2 of 2
  1. #1
    habester is offline Member
    Join Date
    Nov 2009
    Posts
    5
    Rep Power
    0

    Default Trying to Draw Multiple Components

    This is a project that I have started for my class. I am currently trying to draw two components, an @ sign and a rectangle, from two separate classes. Currently, I have them each added to a separate JPanel and then added to the main frame, however at this point they both disappear. What am I missing here?

    Any help or pointers in the right direction would be greatly appreciated!


    PHP Code:
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.geom.*;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Color;
    import java.awt.*;
    
    public class TestKeys2 {
    
    	public static void main(String[] args) {
    		
    		MyGame makeGame = new MyGame();
    		
    	}
    
    }
    
    class MyGame extends JPanel implements KeyListener {
    
    	Player myPlayer = new Player();
    	JPanel levelPanel = new JPanel();
    	JPanel playerPanel = new JPanel();
    
    	public MyGame() {
    		JFrame frame = new JFrame();
    		frame.setSize(500,500);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		levelPanel.add(new Level());
    		playerPanel.add(myPlayer);
    		frame.add(playerPanel, BorderLayout.CENTER);
    		frame.add(levelPanel, BorderLayout.WEST);
    		frame.setVisible(true);
    		frame.setFocusable(true); 
    		frame.addKeyListener(this);
    	}
    	
    	public void keyPressed(KeyEvent e) {
    		// System.out.println("Key down");
    		
    		int key = e.getKeyCode();
    		
    		if(key == KeyEvent.VK_LEFT)
    			myPlayer.moveLeft();
    		if(key == KeyEvent.VK_RIGHT)
    			myPlayer.moveRight();
    		if(key == KeyEvent.VK_UP)
    			myPlayer.moveUp();
    		if(key == KeyEvent.VK_DOWN)
    			myPlayer.moveDown();
    	}
    	public void keyReleased(KeyEvent e) {
    		// System.out.println("Key released");
    	}
    	public void keyTyped(KeyEvent e) {
    		// System.out.println("Key typed");
    	}
    }
    
    class Level extends JComponent {
    
    	public void paintComponent(Graphics g) {
    		Graphics2D g2 = (Graphics2D)g;
    		g2.fillRect(100, 100, 200, 200);
    	}
    	
    }
    
    class Player extends JComponent {
    
    	private float playerx = 50;
    	private float playery = 50;
    	private int playerspeed = 3;
    	private boolean falling = true;
    
    	public void paintComponent(Graphics g) {
    		Graphics2D g2 = (Graphics2D)g;
    		g2.drawString("@", playerx, playery);
    	}
    	
    	public void moveLeft() {
    		playerx -= playerspeed;
    		repaint();
    	}
    	public void moveRight() {
    		playerx += playerspeed;
    		repaint();
    	}
    	public void moveUp() {
    		playery -= playerspeed;
    		repaint();
    	}
    	public void moveDown() {
    		playery += playerspeed;
    		repaint();
    	}
    	public void moveFall() {
    		if(playery < getHeight()-10) {
    			falling = true;
    			playery += playerspeed*2;
    			repaint();
    		}
    		else if(playery >= 490) {
    			playery = getHeight()-10;
    			repaint();
    			falling = false;
    		}
    	}
    }
    Last edited by habester; 11-10-2009 at 12:05 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Your Level object and myPlayer object both are added to JPanels that use FlowLayout. These JComponents will be sized at their default size which will be zero. Likewise your levelPanel which is added BorderLayout.WEST has 0 width for much the same reason. You may wish to set some preferred sizes here or use different layout managers for the JPanels that are acting as containers.

    e.g.,
    Java Code:
      public MyGame() {
        JFrame frame = new JFrame();
        frame.getContentPane().setPreferredSize(new Dimension(800, 500));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        levelPanel.setLayout(new BorderLayout());
        playerPanel.setLayout(new BorderLayout());
        levelPanel.add(new Level());
        levelPanel.setPreferredSize(new Dimension(300, 300));
        playerPanel.add(myPlayer);
        frame.add(playerPanel, BorderLayout.CENTER);
        frame.add(levelPanel, BorderLayout.WEST);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setFocusable(true);
        frame.addKeyListener(this);
        // don't use a key listener, use key binding!
      }
    After reading the Sun Swing tutorials on layout managers, you should next turn your attention to the key binding section which should replace your key listener.

    Much luck.
    Last edited by Fubarable; 11-10-2009 at 12:09 AM.

Similar Threads

  1. Multiple components as one node in JTree
    By Amnuriak in forum AWT / Swing
    Replies: 8
    Last Post: 11-04-2009, 03:47 PM
  2. how to draw an arc
    By Baker in forum New To Java
    Replies: 1
    Last Post: 04-16-2009, 10:05 PM
  3. Printing Swing components in multiple pages
    By Rajeswari in forum New To Java
    Replies: 1
    Last Post: 07-27-2007, 06:49 PM
  4. Printing Swing components in multiple pages
    By Rajeswari in forum AWT / Swing
    Replies: 0
    Last Post: 07-27-2007, 06:01 PM
  5. Gui Components
    By Marty in forum New To Java
    Replies: 1
    Last Post: 05-28-2007, 05:04 AM

Posting Permissions

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