Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (426)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (466)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Noob question  (Read 1086 times)
0 Members and 1 Guest are viewing this topic.
Offline Beppe

Junior Member





« Posted 2013-05-23 22:30:41 »

Hi!
I have started to read the java 2D games tutorial, and I don't understand a thing , following i post the code :
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
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 ;

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
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???
Offline quew8

JGO Coder


Medals: 23



« Reply #1 - Posted 2013-05-23 23:31:58 »

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.
Offline Beppe

Junior Member





« Reply #2 - Posted 2013-05-24 17:26:46 »

Quote
It gets called when the swing system decides it needs to be redrawn.
Ok , also if not is very clear.... I believe in this , but is difficult for me accepted this magic  Grin
Quote
I believe you can call paint(Graphics) explicitly with repaint() but this isn't necessary as far as I know.
Uhm... Overriding a method consist in rewrite a method for an underclass maintaining same name and paramaters....  repaint()??
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline quew8

JGO Coder


Medals: 23



« Reply #3 - Posted 2013-05-24 17:55:44 »

Quote
Ok , also if not is very clear.... I believe in this , but is difficult for me accepted this magic
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()??

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.
Offline Phibedy

Senior Member


Medals: 8



« Reply #4 - Posted 2013-05-24 18:01:11 »

pseudo-code:
1  
2  
3  
 
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  Roll Eyes) ; so pseudo-code again:
1  
2  
3  
4  
 
board.repaint();
for(int i = 0; i < 100000 ; i++){
}

it won't repaint until the loop is done.

Overriding:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
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"
}
}


Offline Beppe

Junior Member





« Reply #5 - Posted 2013-05-24 18:09:14 »

Quote
    paint() invokes paintComponent().
    If the ui property is non-null, paintComponent() invokes ui.update().
    If the component's opaque property is true, ui.update() fills the component's background with the background color and invokes ui.paint().
    ui.paint() renders the content of the component.
Without invokes paint() , he automatically invokes paintComponent() this is the sense Huh
If yes , so if i for same motif in a part of game don't want to draw the panel ??
   
Offline Phibedy

Senior Member


Medals: 8



« Reply #6 - Posted 2013-05-24 18:14:27 »

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.
Offline Beppe

Junior Member





« Reply #7 - Posted 2013-05-24 19:27:51 »

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

Senior Member


Medals: 8



« Reply #8 - Posted 2013-05-24 23:30:43 »

I might miss the question, but what about
1.
In addition you can disable automatical painting.
2. 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 Smiley
Offline Beppe

Junior Member





« Reply #9 - Posted 2013-05-25 11:09:23 »

Quote
if you move them out of the screen -> move them back, minimize them, change size etc.
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  Grin
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Phibedy

Senior Member


Medals: 8



« Reply #10 - Posted 2013-05-25 16:26:35 »

That's why you need a great game-loop for calculation/rendering like: http://www.java-gaming.org/topics/game-loops/24220/view
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 Smiley
Offline Beppe

Junior Member





« Reply #11 - Posted 2013-05-26 10:37:17 »

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?)
Offline notk0

Senior Newbie





« Reply #12 - Posted 2013-05-26 10:40:39 »

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.
Offline Beppe

Junior Member





« Reply #13 - Posted 2013-05-26 22:44:38 »

1  
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.....
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 121
Projects: 4
Exp: 3 years



« Reply #14 - Posted 2013-05-27 01:16:53 »

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.
Offline philfrei
« Reply #15 - Posted 2013-05-27 02:46:20 »

Going back to the original question...

When you display a JFrame, as you do with the line:
1  
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
1  
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.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline Beppe

Junior Member





« Reply #16 - Posted 2013-05-27 18:17:45 »

Ok thanks at all .....
Only a thing how could know the method of swing that are like paint, where i must try in API?
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 121
Projects: 4
Exp: 3 years



« Reply #17 - Posted 2013-05-27 18:35:24 »

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

Junior Member





« Reply #18 - Posted 2013-05-27 19:36:31 »

Ok.... I am not so stupid  Grin
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 ....
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 121
Projects: 4
Exp: 3 years



« Reply #19 - Posted 2013-05-27 22:57:49 »

Ok.... I am not so stupid  Grin
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.
Offline philfrei
« Reply #20 - Posted 2013-05-28 08:17:37 »

I kind of agree that the info on JPanel's use of the paint() method is kind of buried on this API page. But it IS there: under the heading "Methods Inherited from the class javax.swing.JComponent". I would say it is easier to find if you already know it is there.

These API pages also usually have a good explanation, or a link to a relevant section of the official tutorial that covers the class. This entry is no exception.

After a while, navigating through these API's gets easier and makes more sense. I remember when I first looked at them. I was such a noob I couldn't figure out how to use the information because they didn't include code examples. Roll Eyes

Now, I find them hugely helpful.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline Beppe

Junior Member





« Reply #21 - Posted 2013-05-28 18:35:08 »

From API :
Quote

paint
public void paint(Graphics g)
Invoked by Swing to draw components. Applications should not invoke paint directly, but should instead use the repaint method to schedule the component for redrawing.
You have right, sorry.
Thanks at all to be patients , I will treasure of these advice.....
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (75 views)
2014-04-15 18:08:23

BurntPizza (68 views)
2014-04-15 03:46:01

UprightPath (80 views)
2014-04-14 17:39:50

UprightPath (65 views)
2014-04-14 17:35:47

Porlus (81 views)
2014-04-14 15:48:38

tom_mai78101 (105 views)
2014-04-10 04:04:31

BurntPizza (165 views)
2014-04-08 23:06:04

tom_mai78101 (261 views)
2014-04-05 13:34:39

trollwarrior1 (210 views)
2014-04-04 12:06:45

CJLetsGame (220 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!