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

Thread: Cannot draw graphics on JPanel

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Cannot draw graphics on JPanel

    Hi,
    I'm new here and am just starting programming Java. (Started last year but failed )
    I'm trying to figure out why my code doesn't work. I get the JFrame and JPanel but I just wanted to test whether or not I could draw 'something' (ex Oval) on my JPanel
    and that seems not to work. I don't understand why because in school we have done it in about the same way (I wanted to try it
    out this way) and it worked.

    (In school we worked with --> Bal extends JFrame AND BalPaneel extends JPanel)

    Can anybody point me out my mistake?

    Thank you in advance!

    package extraOef;
    import javax.swing.*;
     
    public class Bal
    {
    	JFrame venster;
     
    	public Bal()
    	{
     
    		venster = new JFrame();
    		venster.setSize(800,600);
    		venster.setResizable(true);
    		venster.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		venster.setTitle("Bal tekenen");
    		venster.setLocation(venster.getWidth()/2,venster.getHeight()/2);
     
    		BalPaneel BalPaneel = new BalPaneel();
    		venster.add(BalPaneel.BalPaneel);
     
    		venster.setVisible(true);
    	}
     
    	public static void main(String[] args)
    	{
    		new Bal();
    	}
     
    }
     
    package extraOef;
    import javax.swing.*;
     
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.util.Random;
     
    public class BalPaneel implements KeyListener, ActionListener
    {
    	JPanel BalPaneel;
    	JButton knop;
    	JTextField tekstveld;
    	int diameter;
     
    	public BalPaneel()
    	{
    		BalPaneel = new JPanel();
    		BalPaneel.add(new JLabel("Geef de diameter in"));
    		tekstveld = new JTextField(4);
    		tekstveld.addKeyListener(this);
    		knop = new JButton("Teken de bal");
    		knop.addActionListener(this);
     
    		BalPaneel.add(tekstveld);
    		BalPaneel.add(knop);
     
    	}
     
    	public void paintComponent(Graphics g)
    	{
    		int hoogte = BalPaneel.getHeight();
    		int breedte = BalPaneel.getWidth();
    		g.drawOval(200, 200, 100, 100);
     
    	}
     
    	@Override
    	public void keyPressed(KeyEvent k) 
    	{
    		int key = k.getKeyCode();
     
    		if (key == KeyEvent.VK_ENTER)
    		{
    			try
    			{
    				diameter = Integer.parseInt(tekstveld.getText());
    				BalPaneel.repaint();
    			}
    			catch(Exception ex)
    			{
    				JOptionPane.showMessageDialog(null, "Diameter moet een getal zijn", "Foute Ingave", JOptionPane.ERROR_MESSAGE);
    			}
    		}
     
     
    	}
     
    	@Override
    	public void keyReleased(KeyEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
    	@Override
    	public void keyTyped(KeyEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
    	@Override
    	public void actionPerformed(ActionEvent e) 
    	{
    		try
    		{
    			diameter = Integer.parseInt(tekstveld.getText());
    			BalPaneel.repaint();
    		}
    		catch(Exception ex)
    		{
    			JOptionPane.showMessageDialog(null, "Diameter moet een getal zijn", "Foute Ingave", JOptionPane.ERROR_MESSAGE);
    		}
     
    	}
     
    }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Cannot draw graphics on JPanel

    Your paintComponent method will never be called since it overrides no methods of the class's parent. You need for your BalPaneel class to extend JPanel, and you need to get rid of the BalPaneel variable that it holds since that will do nothing but confuse you and us. You need to give the paintComponent method an @Override annotation just above it so that you know that the method is in fact a proper override.

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Cannot draw graphics on JPanel

    Thank you, I think I will have to read more about paintComponent method. I wanted to try it with the BalPaneel variable since our teacher said that was one way you could make it work (the way we did it in school was with BalPaneel extends JPanel).

    Is there no way of making it work with the way i'm trying? (with BalPaneel variable)

    Thank you again for explaining this.
    I will now read a little more about @Override and paintComponent as it seems that I don't understand it properly.

Similar Threads

  1. [SOLVED] Using scanner and if/else statement to draw one of two graphics.
    By Potat in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 28th, 2013, 05:50 PM
  2. [SOLVED] Graphics Object Won't Draw To The Correct JFrame
    By NickNumero in forum AWT / Java Swing
    Replies: 7
    Last Post: October 27th, 2012, 01:07 PM
  3. Choosing the jpanel to draw on?
    By jm24 in forum AWT / Java Swing
    Replies: 17
    Last Post: February 16th, 2012, 08:44 PM
  4. Moving content (graphics) inside a JPanel
    By Nesh108 in forum Java Theory & Questions
    Replies: 6
    Last Post: February 1st, 2012, 01:21 PM
  5. How to add scrollbar to a JPanel with Graphics
    By Tanguo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2011, 08:53 PM