Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

problem of MediaTracker and paint(Graphics)

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I use MediaTracker in 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 ;
}
}
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't have time to look through all your code, but I see there is a repaint(boolean) method that calls repaint(). Are you sure you are setting the boolean for this method correctly at all times, as if the boolean is true and repaints, but is not then set to false, it may continue repainting forever, even though it has been repainted!
Hope this is a help...
 
    Bookmark Topic Watch Topic
  • New Topic