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);
}
}
JamesCherrill
Posting Genius
6,370 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
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.
JamesCherrill
Posting Genius
6,370 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
plus this line must be moved on last line in the current method, because you showed container before its painting ends
panel.setVisible(true);
mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
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.
JamesCherrill
Posting Genius
6,370 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
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)
mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
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.
JamesCherrill
Posting Genius
6,370 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
@ 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 Java user since version 1.1 , because I jumped to the Java's waves at 1.6.05
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 :-)
mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
@ 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 ...
mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
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?
JamesCherrill
Posting Genius
6,370 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
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.
JamesCherrill
Posting Genius
6,370 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073