Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring 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

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

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
}
}
 
You can't expect to wield supreme executive power just because
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic