Noob question

Hi!
I have started to read the java 2D games tutorial, and I don’t understand a thing , following i post the code :

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class Board extends JPanel{
	Image Cattura;
public Board(){

	ImageIcon img= new ImageIcon("Cattura.jpg");
	Cattura= img.getImage();
}
public void paint(Graphics g){
	Graphics2D g2d= (Graphics2D) g;
	g2d.drawImage(Cattura,10,10,null);
}
}

This is the panel file ;

import javax.swing.JFrame;


public class Image extends JFrame {

    public Image() {

        add(new Board());

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(280, 240);
        setLocationRelativeTo(null);
        setTitle("Cattura");
        setVisible(true);
    }

    public static void main(String[] args) {
        new Image();
    }
}

And this is the main , I don’t understand how is possible the drawing of image, yes there is the drawing method “paint” but this is not recall in the main so what it the logic of this trick???

It isn’t annotated but paint(Graphics) overrides a method in JComponent (it might not be JComponent but certainly one of those higher level abstract swing objects - a superclass of JPanel is what I’m trying to get across). It gets called when the swing system decides it needs to be redrawn.

Really an overridden method should have the @Override annotation to avoid these kind of misconceptions if nothing else.

I believe you can call paint(Graphics) explicitly with repaint() but this isn’t necessary as far as I know.

EDIT:

This isn’t a noob question. Noobs would accept that it worked without wondering and remain forever in ignorance.

[quote] It gets called when the swing system decides it needs to be redrawn.
[/quote]
Ok , also if not is very clear… I believe in this , but is difficult for me accepted this magic ;D

[quote]I believe you can call paint(Graphics) explicitly with repaint() but this isn’t necessary as far as I know.
[/quote]
Uhm… Overriding a method consist in rewrite a method for an underclass maintaining same name and paramaters… repaint()??

[quote]Ok , also if not is very clear… I believe in this , but is difficult for me accepted this magic
[/quote]
Magic, but documented magic: http://docs.oracle.com/javase/tutorial/uiswing/painting/closer.html

[quote]Uhm… Overriding a method consist in rewrite a method for an underclass maintaining same name and paramaters… repaint()??
[/quote]
From the Component documentation:

[quote]public void repaint()
Repaints this component.
If this component is a lightweight component, this method causes a call to this component’s paint method as soon as possible. Otherwise, this method causes a call to this component’s update method as soon as possible.
[/quote]

pseudo-code:

 
Board board = new Board();
board.repaint();

repaint() doesn’t really force a repaint, it just add the “repaint-method to the stack”(that’s not the absolutely true, but I forgot when it’s exactly called ::)) ; so pseudo-code again:

 
board.repaint();
for(int i = 0; i < 100000 ; i++){
}

it won’t repaint until the loop is done.

Overriding:


class Foo{
public boolean fooIt(){
return true;
}
public String modifyName(String name){
return (name + this.getClass().getSimpleName());
}
}

class FooUltimate extends Foo{
@Override
public String modifyName(String name){
return (name + this.getClass().getSimpleName());
}
}

class Tester{
public void test0(){
Foo foo = new Foo();
String test = foo.modifyName("Peter: ");
//-> test is "Peter:  Foo"
}
public void test1(){
Foo foo = new FooUltimate();
String test = foo.modifyName("Peter: ");
//-> test is "Peter:  FooUltimate"
}
}

Without invokes paint() , he automatically invokes paintComponent() this is the sense ???
If yes , so if i for same motif in a part of game don’t want to draw the panel ??

it won’t be painted during your gameloop.
In addition you can disable automatical painting.
java2d is even able to draw at a buffer at first and draw it on the panel afterwards.

Ok repaint() is ok , the problem about this was about overidding , instead the problem was with paint() that automatically print the panel without invocation…

I might miss the question, but what about
1.

  1. JPanels are automatically painted if you move them out of the screen -> move them back, minimize them, change size etc.
    If you wanna know something else / If I missunderstood you, feel free to ask :slight_smile:

[quote] if you move them out of the screen -> move them back, minimize them, change size etc.
[/quote]
Ah! I think that this work like a framerate… Instaed is event-programmed , he was draw first when he was add an after when you said … If is so I have understand all ;D

That’s why you need a great game-loop for calculation/rendering like: http://www.java-gaming.org/index.php/topic,24220.0
you got it, but as I am no certified teacher/haven’t programmed it, it could be a little bit different but I haven’t recognised it yet :slight_smile:

Sorry for my bad english , but i don’t understand in the end the panel is painted like a framerate or like a event-programmed object(like you have say in previous post?)

Do you know what threads are?
lets say there is a thread associated for all swings components that takes care of user input ( keyboard mouse), that’s how when you click a button your method gets called as well as drawing.
It’s not magic.

Do you know what threads are?

No sorry i start to read this tutorial after have finished the lessons of Programming 2 in my University , i thought that it is enough…

Don’t need to know what threads are except that they’re basically mini-programs. To your original question, it is overriding - paint() is a method of JPanel, and you are just providing new behavior for your custom subclass.

Going back to the original question…

When you display a JFrame, as you do with the line:

setVisible(true);

then, all the contents of the JFrame are displayed automatically. In your case, the JFrame contains an instance of Board, because of the line

add(new Board());

So, the Board instance will be displayed due to the setVisible method being executed.

Your Board extends JPanel. The method paint() is one of several that are called whenever a JPanel (or, its parent JComponent) receives a command to be displayed. There are others automatically called to handle things like the border.

Programmatically, we ask for a redisplay by using the repaint() method, but there are automatic things that will trigger a repaint() as well, such as the earlier setVisible(true) line, but also certain things like resizing the window via the Mouse.

If a game loop repeatedly calls repaint(), that is known as “passive rendering”. Repaints occur on a specialized thread called the EDT (Event Dispatch Thread). This thread was created to prevent concurrency errors in the management of the GUI.

Sometimes, depending upon how you update, the code that automatically monitors this thread will decide that two events on it are ‘similar’ or ‘the same’ or something, and will collapse them into a single event. Usually, this is not a problem (that is, it won’t occur) if your game loop is in a different class than the display component receiving the repaint() command.

Ok thanks at all …
Only a thing how could know the method of swing that are like paint, where i must try in API?

http://docs.oracle.com/javase/6/docs/api/javax/swing/JPanel.html
Just google it :slight_smile:

Ok… I am not so stupid ;D
But this page don’t say to me the things that you said me in this post about the mechanism of swing …
It only list all method and costructor …

It lists methods and their functions, constructors, public variables. What else could you possibly need? If you can’t find a certain method that’s probably because it originated from a superclass - for example paintComponent() is inherited in JPanel from JComponent. Just search the superclasses.