• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

: A gray color box appeare d when loading an image on JPanel

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
posted June 01, 2006 01:32 AM
--------------------------------------------------------------------------------
Hai All,

I m nikhil. I have a Problem that when I load an image in to jpanel using drawImage method of Graphics class(G.drawImage(Image img,int width,int higth))...It take more time to load. When loading firts a gray color box will be appear on Frame. Then image loaded.How to avaoid this gray color. How to make it fast.
I m attaching part of my program with this

import javax.swing.*;
import java.awt.*;

public class wbDisplay extends JPanel
{
Image WB=null;
int widthOfWB, heightOfWB;

wbDisplay()
{

}

public void createWB()
{
// This method is responsible for creating the off-screen image.
// It should be called before using the WB. It will make a new WB if
// the size of the panel changes.

if(WB == null || widthOfWB != getSize().width || heightOfWB != getSize().height)
{
// Create the WB, or make a new one if panel size has changed.
WB = null; // (If WB already exists, this frees up the memory.)
WB = createImage(getSize().width, getSize().height);
widthOfWB = getSize().width;
heightOfWB = getSize().height;
Graphics OSG = WB.getGraphics(); // Graphics context for drawing to WB.
OSG.setColor(getBackground());
OSG.fillRect(0, 0, widthOfWB, heightOfWB);
OSG.dispose();
}
}



public void paintComponent(Graphics G)
{
// Copy the off-screen image to the screen,
// after checking to make sure it exists. Then,
// if a shsape other than CURVE is being drawn,
// draw it on top of the image from the WB.

createWB();
G.drawImage(WB, 0, 0, this);

if (dragging && figure != CURVE )
{
G.setColor(foreColor);

drawFigure(G,figure,startX,startY,mouseX,mouseY);
}

}//paintComponent

//insert picture is calling from another class that extends JFrame
//File f stored image

public void insertPicture(File F, int Mx, int My,mainScreenPart ms)
{

createWB();
Graphics G = WB.getGraphics();
System.out.println("inside insert picture");
try
{
ImageIcon img=new ImageIcon(F.getPath());
int WID=img.getIconWidth();
int HEI=img.getIconHeight();
setPreferredSize(new java.awt.Dimension(WID,HEI));
G.drawImage(img.getImage(), Mx, My,WID,HEI, img.getImageObserver());

}
catch(Exception e)
{System.out.println(e);}
}
}


I think i will get a solution from javaranch.Thanks in advance.

Regards,
NIKHIL
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you were asked for code, you ought to have posted it as a reply, with code tags, not started a new thread.
And change your display name; the Ranch requires a full last name.
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic