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 18 of 18

Thread: Choosing the jpanel to draw on?

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    22
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Choosing the jpanel to draw on?

    I'm trying to draw on the JPanel panel, but for some reason nothing shows up when I run the code. Is it because I have to set the panel on which to draw? If so, can someone please tell me how to do this, or just tell me what the problem is lol. Thanks

    (Btw I do have a main method that makes a new PongDrawer and runs the display() method)

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    @SuppressWarnings("serial")
    public class PongDrawer extends JPanel implements ActionListener, KeyListener {
    	JFrame table;
    	JLabel score;
    	JPanel panel;
    	JPanel scorePanel;
    	Timer ballTimer;
    	int ballSpeed;
     
    	public PongDrawer(){
    		table = new JFrame("Pong");
    		panel = new JPanel();
    		table.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		table.setContentPane(panel);
     
    		scorePanel = new JPanel();
    		scorePanel.setSize(700,50);
    		table.add(scorePanel, BorderLayout.NORTH);		
     
    		panel.setBackground(Color.GRAY);
     
    		score = new JLabel("score Test");
    		scorePanel.add(score, BorderLayout.CENTER);
     
    		ballSpeed = 500;
    		ballTimer = new Timer(500, this);
     
    		addKeyListener(this);
    	}
     
    	@SuppressWarnings("deprecation")
    	public void display(){
    		table.pack();
    		table.resize(700,500);
    		table.setVisible(true);
    		repaint();
    	}
     
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);	
     
    		g.setColor(Color.BLACK);
    		g.drawLine(0, 100, 700, 100);
    	}
     
    	public void actionPerformed(ActionEvent e){
     
    	}
     
    	public void keyPressed(KeyEvent e){
    		int keyCode = e.getKeyCode();
     
    		//does the corresponding action for each key pressed
    		switch(keyCode){
    		case KeyEvent.VK_UP:
     
    			break;
    		case KeyEvent.VK_DOWN:
     
    			break;
    		case KeyEvent.VK_W:
     
    			break;
    		case KeyEvent.VK_S:
     
    			break;
    		}
    	}
     
    	public void keyTyped(KeyEvent e){}
     
    	public void keyReleased(KeyEvent e){}
    }
    Last edited by jm24; February 16th, 2012 at 07:39 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Choosing the jpanel to draw on?

    How does anyone test this code? You need a main() method and a JFrame that compiles, executes and shows the problem.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    22
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Choosing the jpanel to draw on?

    Lol sorry I should have said that I already have all that stuff (I thought it would be implied lol)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Choosing the jpanel to draw on?

    Have you solved the problem now?
    If you want anyone to help you with the problem, you need to provide: main() method and a JFrame that compiles, executes and shows the problem.

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    22
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Choosing the jpanel to draw on?

    No as in I had that before you told me lol. I still have the problem, which is that it's not drawing on the JPanel panel for some reason.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Choosing the jpanel to draw on?

    Ok , if you don't want to provide code for testing, then good luck with it.

  7. #7
    Junior Member
    Join Date
    Feb 2012
    Posts
    22
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Choosing the jpanel to draw on?

    This is my main class

    public class PongMain {
     
    	public static void main(String[] args) {
    		PongDrawer game = new PongDrawer();
    		game.display();
    	}
     
    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Choosing the jpanel to draw on?

    Where do you add the the JPanel with the paintComponet method to the JFrame?
    What is the size of the JPanel when it is added to the JFrame?
    Add a println to the paintComponent method to show the size of the component and its location.

  9. #9
    Junior Member
    Join Date
    Feb 2012
    Posts
    22
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Choosing the jpanel to draw on?

    Well what I'm really asking is how I would add the paintComponent to the JPanel panel. Would I have to make the JFrame and JPanel and such in the main class to do that?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Choosing the jpanel to draw on?

    You have added a paintComponent method to the class.

  11. #11
    Junior Member
    Join Date
    Feb 2012
    Posts
    22
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Choosing the jpanel to draw on?

    Yea but I'm saying how would I specify so that it will draw on panel. I think I remember something about using the getGraphics() method, but I don't remember where to put it. Lol sorry for my noobishness.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Choosing the jpanel to draw on?

    Your code draws on the panel.
    However the panel is not visible on the JFrame. Where do you add it?

  13. #13
    Junior Member
    Join Date
    Feb 2012
    Posts
    22
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Choosing the jpanel to draw on?

    Ok I just added panel to the JFrame before I made it the content pane but it's still not drawing anything. I also did table.getContentPane().add(panel); instead of table.setContentPane(panel);

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Choosing the jpanel to draw on?

    Please post the new code.

  15. #15
    Junior Member
    Join Date
    Feb 2012
    Posts
    22
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Choosing the jpanel to draw on?

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    @SuppressWarnings("serial")
    public class PongDrawer extends JPanel implements ActionListener, KeyListener {
    	JFrame table;
    	JLabel scoreLabel;
    	JPanel panel;
    	JPanel scorePanel;
    	Timer ballTimer;
    	int ballSpeed;
    	int score;
     
    	public PongDrawer(){
    		table = new JFrame("Pong");
    		panel = new JPanel();
    		table.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		table.add(panel);
    		table.getContentPane().add(panel);
     
    		scorePanel = new JPanel();
    		scorePanel.setSize(700,50);
    		table.add(scorePanel, BorderLayout.NORTH);		
     
    		panel.setBackground(Color.GRAY);
    		scorePanel.setBackground(Color.GRAY);
     
    		scoreLabel = new JLabel("score Test");
    		scorePanel.add(scoreLabel, BorderLayout.CENTER);
     
    		ballSpeed = 500;
    		ballTimer = new Timer(500, this);
     
    		addKeyListener(this);
    	}
     
    	@SuppressWarnings("deprecation")
    	public void display(){
    		table.pack();
    		table.resize(700,500);
    		table.setVisible(true);
    		repaint();
    	}
     
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);	
     
    		g.setColor(Color.BLACK);
    		g.drawLine(0, 100, 700, 100);
     
    		g.setColor(Color.RED);
    		g.drawRect(100, 100, 50, 50);
    	}
     
    	public void actionPerformed(ActionEvent e){
     
    	}
     
    	public void keyPressed(KeyEvent e){
    		int keyCode = e.getKeyCode();
     
    		//does the corresponding action for each key pressed
    		switch(keyCode){
    		case KeyEvent.VK_UP:
     
    			break;
    		case KeyEvent.VK_DOWN:
     
    			break;
    		case KeyEvent.VK_W:
     
    			break;
    		case KeyEvent.VK_S:
     
    			break;
    		}
    	}
     
    	public void keyTyped(KeyEvent e){}
     
    	public void keyReleased(KeyEvent e){}
    }

    I know that the panel is definitely visible because I can see it when I run the program. It's just that nothing is drawing on it.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Choosing the jpanel to draw on?

    Where do you add an instance of the PongDrawer class to the JFrame?

  17. #17
    Junior Member
    Join Date
    Feb 2012
    Posts
    22
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Choosing the jpanel to draw on?

    Yea I figured that was my problem. I just wanted to make the whole thing in one class, but yea I guess that's not possible. Thanks for the help!

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Choosing the jpanel to draw on?

    I just wanted to make the whole thing in one class
    It is possible. That is how I have it now.
    Remember the this variable.

Similar Threads

  1. Choosing a bibliography style, Beginner Programmer question.
    By diagnonsense in forum Java Theory & Questions
    Replies: 6
    Last Post: February 11th, 2012, 05:34 PM
  2. [SOLVED] Pesky <JPanel>.getWidth() and <JPanel>.getHeight() Methods...
    By snowguy13 in forum AWT / Java Swing
    Replies: 1
    Last Post: December 31st, 2011, 03:35 PM
  3. Change the random draw line here for a mouseListener draw
    By Panda23 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2011, 03:29 AM
  4. How to copy image from one jpanel to another jpanel
    By ramanavarayuri1986 in forum AWT / Java Swing
    Replies: 0
    Last Post: February 15th, 2010, 02:36 AM
  5. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 08:02 AM