Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
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  
  The MediaTracker inneficient w/o the ImageObserver ? (RE:)  (Read 782 times)
0 Members and 1 Guest are viewing this topic.
Offline broumbroum

Junior Member





« Posted 2007-10-01 08:42:47 »

Quote from: broumbroum
Hi!
I was wondering if someone's already checked this statement that says : Innefficiency of MediaTracker w/o ImageObserver and vice-versa.
Because of some reasons, some web articles (http://www.sdv.fr/pages/casa/html/java-image.html and other posts in various forums) discussed this topic and but miss-spelled the relationship between these 2 Java Classes, as the MediaTracker's would be used in place of ImageObserver's.
I'd rather state that nor the MT nor the IObs can be used standing alone. They are related ! As someone says, they're cousins and they can't live one w/o each other.
Do you agree ? Huh

Hence, buffered painting becomes easier only if BOTH of these classes are linked together with the SAME component. Let's have a unit test  : (...)
hence this be an assertion rule : the MediaTracker target (ImageObserver) must be equal to the Graphics ImageObserver otherwise painting will fail with no picture rendered. Can someone get this validated, too ?
Tested code is :
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  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
class MainBufferedPainting {
    class ImageComponent extends JComponent {
        public Component obs;
        public MediaTracker mt;
        public Image[] img;
        public void paint(Graphics g) {
            try {
                int rowIndex = 0;                        // wait on mediaTracker
               for(int index = 0; index < img.length; index++) {
                    if(img[index].getWidth(obs) != getWidth() || img[index].getHeight(obs) != (int)Math.floor((float)getHeight() / (float)img.length)) {
                        BufferedImage bi = new BufferedImage(getWidth(), (int)Math.floor((float)getHeight() / (float)img.length), BufferedImage.TYPE_INT_ARGB);
                        mt.addImage(img[index], index, getWidth(), (int)Math.floor((float)getHeight() / (float)img.length));
                        mt.waitForID(index);
                        bi.createGraphics().drawImage(
                                img[index],
                                AffineTransform.getScaleInstance((float)getWidth() / (float)img[index].getWidth(obs), Math.floor((float)getHeight() / (float)img.length) / (float)img[index].getHeight(obs)),
                                obs
                                );
                        mt.removeImage(img[index]);
                        img[index] = bi;
                        mt.addImage(img[index], index);
                    }
                    mt.waitForID(index); // wait on mediaTracker
                   try{
                        Graphics gI = img[index].getGraphics();
                        gI.setColor(Color.RED);
                        gI.drawOval(0, 0, img[index].getWidth(obs), img[index].getHeight(obs));
                    } catch(Exception e) {e.printStackTrace();} finally {
                        Point pos = new Point(
                                (int)((float)(getWidth() - img[index].getWidth(obs)) / 2.0),
                                rowIndex
                                );
                        g.drawImage(img[index], pos.x, pos.y, obs);
                        rowIndex += img[index].getHeight(obs);
                    }
                }
            } catch(InterruptedException e) { e.printStackTrace();}
        }
    }
    public ImageComponent getComponent() {
        return new ImageComponent();
    }
    public static void main(String[] args) {
        JFrame f = new JFrame("Buffered Painting");
        MainBufferedPainting.ImageComponent comp = new MainBufferedPainting().getComponent();
// our test is out of three different image implementations
       String picture = "";
        if(args != null) {
            if(args.length > 0)
                picture = args[0];
        }
       
        comp.setPreferredSize(new Dimension(150,300));
        f.getContentPane().removeAll();
        f.getContentPane().add(comp);
        f.pack();
        f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {
            System.exit(0);
        }});
        f.setLocationRelativeTo(null);
        f.setVisible(true);
        try {
            while(true) {
                comp.img = new Image[]{
                    new BufferedImage(50,50, BufferedImage.TYPE_INT_ARGB),
                    Toolkit.getDefaultToolkit().getImage(picture),
                    GraphicsEnvironment. getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatibleVolatileImage(50, 50)
                };
               
// for a given component c which we want to paint on
               MediaTracker mt = new MediaTracker(f); // cannot be null ! best is to give the exact component on which all images will be drawn
               comp.obs = f;//f;//null;//new JLabel("virtual"); // here we can say that the ImageObserver is kinda unused in new impl.'s
               comp.mt = mt;                
                for(int i = 0; i < comp.img.length; i++)
                    mt.addImage(comp.img[i], i);
                Thread.sleep(100);
                f.repaint();
            }
        } catch(InterruptedException e) { e.printStackTrace(); System.exit(0); }
    }
}

If the MediaTracker has no Component as a target, no loading can be done. But ImageObserver's impl. must be EXACT for the Component you give as target.
ImageObserver's are indeed quite unused in the drawImage() impl. but are best used WITH the MediaTracker, especially if you scale the images.  Grin

::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
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.

ctomni231 (34 views)
2014-07-18 06:55:21

Zero Volt (30 views)
2014-07-17 23:47:54

danieldean (25 views)
2014-07-17 23:41:23

MustardPeter (27 views)
2014-07-16 23:30:00

Cero (42 views)
2014-07-16 00:42:17

Riven (44 views)
2014-07-14 18:02:53

OpenGLShaders (33 views)
2014-07-14 16:23:47

Riven (34 views)
2014-07-14 11:51:35

quew8 (30 views)
2014-07-13 13:57:52

SHC (66 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!