Hello everyone on Daniweb!:)
I'm all new with working with swing and I'm having some problems making my paint method work:/. Atm I'm just tryin to make it write a ball, which I can control into different directions.
Right now there is quite alot of unnecessary stuff in my code but please don't bother about that:P.
Here's the code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.awt.event.KeyEvent.*;
import java.awt.event.KeyListener.*;
import java.awt.Graphics.*;

public class Boll
{
	JFrame frame=new JFrame("hej");
	JLabel panel=new JLabel();
	private int x=250, y=250;
	private boolean ball=true;
	private boolean jump=false;
	private final int MAXX=500, MAXY=500;
	private int left, right, up, down, side;
	private Graphics gr;
	private int g, m, v, E, p, a, t; //fysikaliska element
	
	public static void main(String[] skit)
	{
		Boll b=new Boll();
	}
	
	public Boll()
	{
		frame.setVisible(true);
		frame.pack();
		frame.add(panel);
		side=5;
		panel.setVisible(true); //sätter ut
		panel.setSize(MAXX,MAXY);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		panel.addKeyListener(new KeyThing());
		panel.paint(gr);
		
		
		
			
	}
	
	
	public class KeyThing implements KeyListener
	{
		public void keyPressed(KeyEvent e)
		{
			if(e.getKeyLocation()==KeyEvent.VK_LEFT)
			{
				x-=side;
				panel.repaint();
			}
			
			else if(e.getKeyLocation()==KeyEvent.VK_RIGHT)
			{
				x+=side;
				panel.repaint();
			}
			
			else if(e.getKeyLocation()==KeyEvent.VK_UP)
			{
				y+=side;
				panel.repaint();
			}
			
			else if(e.getKeyLocation()==KeyEvent.VK_DOWN)
			{
				y-=side;
				panel.repaint();
			}
		}
		
		public void keyTyped(KeyEvent e)
		{
			
		}
		
		public void keyReleased(KeyEvent e)
		{
			
		}
		
	}

	
	
	
	
	
	
	
	public void paint(Graphics g)
	{
		if (ball==true)
		{
			g=panel.getGraphics();
			g.setColor(Color.green);
			g.fillOval(x, y, 30, 30);
			g.dispose();
		}
	}
	
}

Huge thanks to everyone for taking time to help me!:) I really appreciate it.

//Angelika

1. You are expected to use the Graphics object passed to you as a parameter
2. aBoolean == true is just like anInt + 0, ie horribly redundant
3. Override paintComponent, not paint
4. panel.paint(gr); never call paint directly. call repaint() and let Swing do the rest

public void paintComponent(Graphics g){
  if (ball){
    g.setColor(Color.green);
    g.fillOval(x, y, 30, 30);
  }
}

It still isn't showing:/ The only thing coming up is a big white box.
Here's the changes that I've made:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.awt.event.KeyEvent.*;
import java.awt.event.KeyListener.*;
import java.awt.Graphics.*;

public class Boll
{
	JFrame frame=new JFrame("hej");
	JLabel panel=new JLabel();
	private int x=250, y=250;
	private boolean ball=true;
	private boolean jump=false;
	private final int MAXX=500, MAXY=500;
	private int left, right, up, down, side;
	private Graphics gr;
	private int g, m, v, E, p, a, t; //fysikaliska element
	
	public static void main(String[] skit)
	{
		Boll b=new Boll();
	}
	
	public Boll()
	{
		frame.setSize(MAXX, MAXY);
    	frame.setLayout(new GridLayout(1, 1));
		frame.setVisible(true);
		frame.add(panel);
		side=5;
		panel.setVisible(true); //sätter ut
		panel.setSize(MAXX, MAXY);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		panel.addKeyListener(new KeyThing());
		panel.repaint();
		
		
		
			
	}
	
	
	public class KeyThing implements KeyListener
	{
		public void keyPressed(KeyEvent e)
		{
			if(e.getKeyLocation()==KeyEvent.VK_LEFT)
			{
				x-=side;
				panel.repaint();
			}
			
			else if(e.getKeyLocation()==KeyEvent.VK_RIGHT)
			{
				x+=side;
				panel.repaint();
			}
			
			else if(e.getKeyLocation()==KeyEvent.VK_UP)
			{
				y+=side;
				panel.repaint();
			}
			
			else if(e.getKeyLocation()==KeyEvent.VK_DOWN)
			{
				y-=side;
				panel.repaint();
			}
		}
		
		public void keyTyped(KeyEvent e)
		{
			
		}
		
		public void keyReleased(KeyEvent e)
		{
			
		}
		
	}

	
	
	
	
	
	
	
	public void paintComponent(Graphics g)
		{  
			if (ball)
			{    
				g.setColor(Color.green);    
				g.fillOval(x, y, 30, 30);
			}
		}
	
}

Thank you!
//Angelika

You need to override paintComponent for your panel, but your paintComponent method is in a class Boll that doesn't extend anything paintable.
To get this working the easiest way (you can improve it later) you need a structure like this:
class Boll extends JLabel { ... // now you can use a Boll instead of a JLabel
get rid of "JLabel panel" and use "this" instead of "panel" - ie your own subclass of JLabel - throughout.
Now when that JLabel needs to be painted, it will use your paintComponent method instead of the one in the JLabel class. Put a print statement in your method so you can see when its called.

plus this line must be moved on last line in the current method, because you showed container before its painting ends

panel.setVisible(true);

That's a good point from mKorbel - you make the frame visible before you have finished sizing it, adding stuff to it etc. Most of the time you'll get away with this because your method will finish executing before the Swing thread gets to paint anything on the screen. But with multiple threads you can't guarantee that, so its always possible that your frame will be painted on the screen without all its contents etc then re-painted correctly when your method finishes - which will give a horrible flickering/flashing effect.
The moral: always finish setting up the initial state of windows before making them visible.

now I see that JLabel replace with JPanel in line 12. (I just read panel, panel what's wrong with panel, heavens) isn't good for painting, I don't know similair JComponents, JLabel doesn't allowed custom painting

hmmm, anyway let's divide into coints

1/ create JComponents == JLabels, JTextFields

2/ create container JPanel

3/ add JComponents (JLabels, JTextFields) to container (JPanel)

4/ create topLayoutContainer (JFrame, JDialog, Window)

5/ add container (JPanel) to the topLayoutContainer (JFrame, JDialog, Window), better way is by using BorderLayout.CENTER

6/ Visible#true for topLayoutContainer (JFrame, JDialog, Window)

JLabel doesn't allowed custom painting

Not true. You can subclass JLabel and override paintComponent just like any other Swing JComponent. This is a recommended practice if you just want a single custom painted object without any children, especially if that is to be painted on top of something else.

@ recommended practice

I have with the JLabel only bad experiencies (painting, sure not/excluded Image or Icon), and I'm not alone on a few forums (it was not an argument), rather there I'll kick tons of JPanels (I upload/showed Images only in JPanel, its just my rule, and mybe only *** vice)

maybe I missed some history & chained rules from previous Java version <copy> Java user since version 1.1 </copy>, because I jumped to the Java's waves at 1.6.05 <unaffected previous history>

hmmm, but then is too hard to give sufficient and correct answer for posters from western (parts of the globe) which are daily sticks *** post with Applet instead of JApplet, Frame <> JFrame ...

I quite not sure in this case, let's opened that :-)

@ JamesCherrill

"especially if that is to be painted on top of something else"

I never tried overload something with JLabel.setOpaque(false), yes for that, its quite confortable way,

but GlassPane is more than better choise for me, because blablablbabla

anyway, thanks for your kind ...

i went through this with my teacher and we still couldn't solve it:/ Any more help would be apreciated

What do you mean "couldn't solve it"? What exactly can't you do? What does your latest code look like? What errors (if any) does it give you?

What do you mean "couldn't solve it"? What exactly can't you do? What does your latest code look like? What errors (if any) does it give you?

I have just tried working a little bit on it today. I have removed some unnecessary things in the code and I have made the JLabel a JPanel again(thats why the JLabel was called "panel" before). Not much have changed though:/. I'm not english and find it hard to understand sometimes so I'd love if you know what's wrong with my code that you'd change it in that place it needs to be changed so that I will understand it(: Thank you all so much for baring with me this far, I'm doing my best to understand this.

import javax.swing.*;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.awt.event.KeyEvent.*;
import java.awt.event.KeyListener.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Graphics.*;

public class Boll extends JFrame
{
	JPanel panel=new JPanel();
	private int x=250, y=250;
	private boolean ball=true;
	private final int MAXX=500, MAXY=500;
	
	public static void main(String[] skit)
	{
		Boll b=new Boll();
	}
	
	public Boll()
	{
		super.setSize(MAXX, MAXY);
		super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		super.add(panel);
		panel.setSize(MAXX, MAXY);
		panel.paint();
		panel.addKeyListener(new KeyThing());
		panel.setVisible(true);
		super.setVisible(true); //sätter ut
		
		
		
			
	}
	
	
	public class KeyThing implements KeyListener
	{
		public void keyPressed(KeyEvent e)
		{
			if(e.getKeyLocation()==KeyEvent.VK_LEFT)
			{
				x--;
				panel.repaint();
			}
			
			else if(e.getKeyLocation()==KeyEvent.VK_RIGHT)
			{
				x++;
				panel.repaint();
			}
			
			else if(e.getKeyLocation()==KeyEvent.VK_UP)
			{
				y++;
				panel.repaint();
			}
			
			else if(e.getKeyLocation()==KeyEvent.VK_DOWN)
			{
				y--;
				panel.repaint();
			}
		}
		
		public void keyTyped(KeyEvent e)
		{
			
		}
		
		public void keyReleased(KeyEvent e)
		{
			
		}
		
	}

	
	
	
	
	
	
	
	public void paintComponent(Graphics g)
	{	
		if (ball)
		{
			g=panel.getGraphics();
			g.setColor(Color.green);
			g.fillOval(x, y, 30, 30);
			g.dispose();
		}
	}
	
}

The error I am getting:

C:\Users\Annie\Desktop\Skolarbete\Java\Boll\Boll.java:31: cannot find symbol
symbol : method paint()
location: class javax.swing.JPanel
panel.paint();
^
1 error

Note: When I somehow make all errors disapear it tends to just show an empty grey box so I'm sure that error isn't the only problem

//Angelika

bearing* !!!! haha

//Angelika

This still has major errors in its design & construction - it's not just little mistakes.
You could start by doing all the other things I wrote about in my previous posts, then we can take it from there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.