This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes When I use MediaTracker  , the method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "When I use MediaTracker  , the method "paint(Graphics)" will be invoked ceaselessly!!" Watch "When I use MediaTracker  , the method "paint(Graphics)" will be invoked ceaselessly!!" New topic
Author

When I use MediaTracker , the method "paint(Graphics)" will be invoked ceaselessly!!

qs Wang
Greenhorn

Joined: Nov 04, 2002
Posts: 7
When I use MediaTracker in my program the method
"paint(Graphics)" will be invoked ceaselessly! why ??

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
public class test extends JFrame
{
public test()
{
super("welcome");
//this.addNotify();
setPane();
pack();
setSize(800, 600);
show();
}

private void setPane()
{
MenuPane menuPane = new MenuPane();
MediaTracker tracker = new MediaTracker(this);
Image img = Toolkit.getDefaultToolkit().getImage("icon.gif");
tracker.addImage(img, 0);
try
{
tracker.waitForID(0);
}
catch(InterruptedException e)
{
System.out.println(e);
}
MenuUnit unit= new MenuUnit(img, "A", 100, 100, 100, 100);
unit.setActivable(true);
unit.setMovable(true);
menuPane.addUnit(unit);
menuPane.setSize(800, 600);
this.getContentPane().add(menuPane, BorderLayout.CENTER);
}

public static void main(String[] arg)
{
test p = new test();
}
}

class MenuPane extends JPanel
{
int unitNum;
MenuUnit units[]=new MenuUnit[4];
protected boolean fresh = false;

public MenuPane()
{
this(0,0) ;
}

public MenuPane(int w , int h )
{
super() ;
setDoubleBuffered(true);
setLayout(null) ;
setSize(w,h) ;
}

public void addUnit(MenuUnit mu)
{
addUnit(mu, -1) ;
}
private void addUnit(MenuUnit mu, int index )
{
if (unitNum == units.length)
{
MenuUnit newunits[] = new MenuUnit[unitNum * 2];
System.arraycopy(units, 0, newunits, 0, unitNum);
units = newunits;
}
if (index == -1 || index == unitNum)
{
mu.index = unitNum;
units[unitNum++] = mu;
}
else
{
System.arraycopy(units, index, units, index+1, unitNum-index);
mu.index = index;
units[index] = mu;
unitNum++;
}
mu.setParent(this);
add(mu);
}

public MenuUnit getUnit(int index)
{
if ( (index<0) || (index>=unitNum) )
return null ;
else
return units[index] ;
}
int i = 0;
public void repaint(boolean _fresh)
{
System.out.println("repaint" + (++i));
fresh = _fresh ;
repaint() ;
}

public void update(Graphics g)
{
paint(g) ;
}

int j = 0;
public void paint(Graphics g)
{
System.out.println("menu paint" + (++j));
for(int i=0; i<units.length; i++)
{
if(units[i] != null)
units[i].paint(g) ;
}
}
}
class MenuUnit extends JComponent
{
public static final int LINE=0, IMAGE=1;

protected int index;
protected int type;
protected Container parent;
protected Image image;
protected Image altImage;
protected String text;
protected Font font;
protected Color foreColor=null;
protected boolean movable=true;
protected boolean activable=true;
protected boolean activated=false;
protected int x, y;
protected int textX=0, textY=0;
protected int width,height;

public MenuUnit()
{
super();
}

public MenuUnit(String _text, int _x, int _y, int _w, int _h)
{
this(null,_text,_x,_y,_w,_h);
}
public MenuUnit(Image img, int _x, int _y, int _w, int _h)
{
this(img, null, _x, _y, _w, _h);
}
public MenuUnit(Image img, String _text, int _x, int _y, int _w, int _h)
{
super();
setDoubleBuffered(true);
image = img;
altImage = img;
text = _text;
x = _x;
y = _y;
width = _w;
height = _h;
super.setBounds(x, y, width, height);
}

public void setParent(Container _parent) {
parent = _parent ;
}

public void setMovable(boolean m)
{
movable = m ;
}

public boolean isMovable()
{
return movable ;
}

public void setActivable(boolean a)
{
activable = a ;
}

public boolean isActivable()
{
return activable ;
}

public void setActivated(boolean a)
{
if(isActivable())
activated = a ;
else
activated = false ;
}

public boolean isActivated()
{
return activated ;
}

public void paint(Graphics g)
{
if (image!=null)
{
if (isActivated())
{
if ( altImage==image)
{
g.drawImage(altImage, x+width*5/100, y+height*5/100, width*9/10, height*9/10, parent) ;
}
else
{
g.drawImage(altImage, x,y,width,height,this) ;
}
}
else
g.drawImage(image,x,y,width,height,this) ;
}

if ( text!=null)
{
Font f = g.getFont() ;
if (font!=null)
{
g.setFont(font) ;
}
Color c = g.getColor() ;
if ( foreColor!=null)
{
g.setColor(foreColor) ;
}
Shape s= g.getClip() ;
g.setClip(x,y,width,height) ;

FontMetrics fm = g.getFontMetrics() ;
int ascent = fm.getAscent() ;
g.drawString(text,x+textX,y+textY+ascent) ;
if (isActivated())
{
Color cc = g.getColor() ;
g.setColor(new Color( 255-cc.getRed(), 255-cc.getGreen(), 255-cc.getBlue() ) );
g.drawString(text, x+textX, y+textY+ascent) ;
}
g.setClip(s) ;
g.setFont(f) ;
g.setColor(c) ;
}
}
public boolean inSelectArea(int _x, int _y)
{
if (!isActivable() )
return false ;
if ( (_x>=x)&&( _x< x+width ) && (_y>=y) && (_y<y+height) )
return true ;
else
return false ;
}
}
qs Wang
Greenhorn

Joined: Nov 04, 2002
Posts: 7
you can run this code , there are the same problem : paint is invoked ceaselessly .. why ??

import java.awt.event.*;
import java.awt.*;
public class test1 {
public static void main(String args[]) {
new SplashWindowFrame();
}
}
class SplashWindowFrame extends Frame {
SplashWindow sw;
Image splashIm;
SplashWindowFrame() {
super();
/* Add the window listener */
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
dispose();
System.exit(0);
}});
/* Size the frame */
setSize(200,200);
/* Center the frame */
Dimension screenDim =
Toolkit.getDefaultToolkit().getScreenSize();
Rectangle frameDim = getBounds();
setLocation((screenDim.width - frameDim.width) / 2,
(screenDim.height - frameDim.height) / 2);
MediaTracker mt = new MediaTracker(this);
splashIm = Toolkit.getDefaultToolkit(
).getImage("test.gif");
mt.addImage(splashIm,0);
try {
mt.waitForID(0);
} catch(InterruptedException ie){}
sw = new SplashWindow(splashIm);
this.add(sw, BorderLayout.CENTER);
try {
Thread.sleep(3000);
} catch(InterruptedException ie){}
/* Show the frame */
setVisible(true);
}
}
class SplashWindow extends Panel {
Image splashIm;
SplashWindow(Image splashIm)
{
this.splashIm = splashIm;
setSize(200,200);
/* Center the window */
Dimension screenDim =
Toolkit.getDefaultToolkit().getScreenSize();
Rectangle winDim = getBounds();
setLocation((screenDim.width - winDim.width) / 2,
(screenDim.height - winDim.height) / 2);
setVisible(true);
}
public void paint(Graphics g) {
System.out.println("paint");
if (splashIm != null) {
g.drawImage(splashIm,0,0,this);
}
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When I use MediaTracker , the method "paint(Graphics)" will be invoked ceaselessly!!