Painting an Image object on a BufferedImage for later use - java

I need some help with painting an Image object upon/inside/on a BufferedImage and then painting that BufferedImage on a JPanel.
I've prepared a small program to illustrate my problem. Just a frame with a panel accompanied by an ImageLoader.
The image is placed in the same folder as the code. The sten Image is painted successfully when just painted, but not when I try painting it with the BufferedImage, which you'll notice if you try to run the program. just create the Test object and the constructor does the rest.
Thanks in advance!
My code:
public class Test extends JFrame{
static class ImageLoader {
public static Image loadImage(String name){
Image img = null;
img = Toolkit.getDefaultToolkit().getImage(ImageLoader.class.getResource(name));
return img;
}
}
class Panel extends JPanel{
Image sten;
BufferedImage bf = new BufferedImage(500,500,BufferedImage.TYPE_INT_RGB);
public Panel(Image sten){
super();
this.sten = sten;
initBF();
}
private void initBF(){
Graphics2D g = (Graphics2D) bf.createGraphics();
g.drawImage(sten, 0,0,this);
}
public void paintComponent (Graphics g)
{
g.drawImage(bf, 100,100,null);
g.drawImage(sten, 0,0,null);
repaint();
}
}
public Test(){
setSize(new Dimension(500, 500));
setEnabled(true);
this.setBounds(50, 150, 500, 500);
setVisible(true);
Image sten = ImageLoader.loadImage("sten.png");;
Panel panel = new Panel(sten);
panel.setBackground(Color.GREEN);
panel.setSize(500, 500);
this.add(panel);
setDefaultCloseOperation(EXIT_ON_CLOSE);
panel.paintComponent(this.getGraphics());
}
}

The problem is that Toolkit.getDefaultToolkit().getImage loads images asychronously so will not be loaded when paintComponent is invoked. Use a MediaTracker to block until the image has been loaded:
public Image loadImage(String name) {
Image img = null;
MediaTracker tracker = new MediaTracker(myPanel); // pass the panel from ctor
img = Toolkit.getDefaultToolkit().getImage(ImageLoader.class.getResource(name));
tracker.addImage(img, 0);
try {
tracker.waitForID(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
return img;
}
or far simpler:
img = ImageIO.read(ImageLoader.class.getResource(name)));
This will eliminate the need to use MediaTracker.
Some notes:
Don't call paintComponent directly, request repaints by invoking repaint.
Don't use getGraphics for custom painting - this uses a transient Graphics reference.
When using a custom Graphics reference, make sure to call Graphics#dispose when finished using the reference.

In addition of Reimeus' aswer, notice that Toolkit.getDefaultToolkit().getImage() is non-blocking, it loads images asynchronously, so at the point when you call g.drawImage(sten, 0,0,this); sten will probably not still actually be loaded (see the doc of the method). See also this (false) bug report.

Related

Draw on Jframe image

I want to read a file to get some points and then draw these points on an image. Currently, I am able to draw values on the image, but the file is read three times and the rectangles are drawn three times. I don't know where is the problem. Below is the code. The Read() function works fine seperately so I didn't include it in the code.
P.S: I am beginner in JAVA and don't know much about JFrame and Jcomponent.
public class LoadImageApp extends JComponent {
BufferedImage img;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, null);
Read(g);// This is the function in which I read a file.
}
public LoadImageApp() {
try {
img = ImageIO.read(this.getClass().getResource("/New York.jpg"));
} catch (IOException e) {
}
}
public Dimension getPreferredSize() {
if (img == null) {
return new Dimension(100,100);
} else {
return new Dimension(img.getWidth(null), img.getHeight(null));
}
}
public static void main(String[] args) {
JFrame f = new JFrame("Load Image Sample");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
LoadImageApp img = new LoadImageApp();
f.add(img);
f.pack();
f.setVisible(true);
}
}
Do not, do not, DO NOT read from a file from within any painting method such as paintComponent(...). :)
That method should be for painting only. The more you slow it down, the less responsive your GUI will seem.
You cannot control how many times the method gets called, since it is not under direct control by you, the programmer.
You can't even control fully if the paintComponent method gets called, since the JVM might decide that too many requests for redraw are being stacked up, and it may not honor all of them.
Instead
read in the data once in a constructor or something similar.
I would create a read method that stores my points in an ArrayList<Point>, and then inside of the paintComponent method, iterate through that ArrayList using a for loop and draw them.
If the points don't change during your program's run, you could even draw them directly on to the BufferedImage by getting its Graphics context and using that to paint the points on to the image, and then show the new BufferedImage in your paintComponent method.
Other suggestions:
That empty catch block where you read your image is a dangerous thing to do. It's the coding equivalent of driving a motorcycle with your eyes closed. At least print out a stacktrace.
The WindowListener is not needed. Instead simply set your JFrame's defaultCloseOperation to JFrame.EXIT_ON_CLOSE: f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Image inside JFrame - Swing/AWT

Some days ago, I wasted a lot of time searching some way to show a image in a JFrame. And here is my final solution:
jPanel1 = new javax.swing.JPanel(){
#Override
public void paintComponent(Graphics g) {
BufferedImage image = null;
try {
BufferedImage in = ImageIO.read(Startup.class.getResource("imagem.jpg"));
image = new BufferedImage(in.getWidth(), in.getHeight(), BufferedImage.TYPE_INT_ARGB);
g.drawImage(in, 0, 0, null);
} catch (Exception ex) {}
super.paintComponents(g);
}
};
I just want to know, if it is the one way to do that, or exists another solutions like a image component on Swing o AWT that can be easily used?
You can show an image using JLabel, which is much simpler than your solution. For example:
label.setIcon(new ImageIcon("Path/to/your/image.jpg"));

Display image on applet

I am using Jtree for listing various images of a directory, I want to display image on applet when the user click on the image name displayed in the Tree, the code i'm using is as below, ta is an object of the applet because i'm using it in another class.
private void displayImage(URL furl, String fname) {
ta.Picture = ta.getImage(furl, fname);
prepareImage(ta.Picture, this);
Graphics g = ta.imageCanvas.getGraphics();
g.clearRect(10, 10, 800, 800);
g.drawImage(ta.Picture, 10, 10, this);
} // displayImage
public void valueChanged(TreeSelectionEvent e)
{
// TODO Auto-generated method stub
FileTreeNode node = (FileTreeNode) tree.getLastSelectedPathComponent();
System.out.println("slecte asldf " + node.isLeaf());
if (node.isLeaf())
{
currentFile = node.file;
System.out.println("File name " + currentFile.getName());
try
{
URL furl = new URL("file:/F:/photos");
displayImage(furl, currentFile.getName());
}
catch (MalformedURLException mle)
{
System.out.println("Exception::::::" + mle);
}
}
else
currentFile = null;
}
But its not working.
As you are showing files from the local filesystem, working with URLs is not required. Use
displayImage(currentFile);
and rewrite that method as following:
private void displayImage(File file) {
BufferedImage image = ImageIO.read(file);
ta.image = image;
ta.repaint();
}
where the paint method of the (I an assuming) component ta must be like
BufferedImage image;
public void paint(Graphics g) {
g.clearRect(10, 10, 800, 800);
g.drawImage(ta.Picture, 10, 10, this);
}
Because of security reasons, the applet will only be able to access the file system if signed or running without security manager (most often on the same computer).
But its not working.
This is in no way helpful, do you get exceptions? What happens? Please post an SSCCE for better help sooner
I want to display image on applet when the user click on the image
name displayed in the Tree, the code i'm using is as below, ta is an
object of the applet because i'm using it in another class.
IMO you are going about it wrong using the JPanel object and Component#getGraphics.
Dont use Component#getGraphics() as its not good practice and not persistent thus on next call to repaint() the screen will be cleared.
Dont use Applet with Swing components rather use JApplet.
Add a custom JPanel with getters and setters for BufferedImage variable to the container and than override paintComponnet and draw the BufferedImage there.
Now to change the BufferedImage simply call the setter i.e setBackgroundImage(BufferedImage img) and than call repaint() on JPanel to show the changes. Like so:
public class MyPanel extends JPanel {
private BufferedImage bg;
public MyPanel(BufferedImage bi) {
bg=bi;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d=(Graphics2D)g;
g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
g2d.drawImage(bg,0,0,this);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(bg.getWidth(),bg.getHeight());
}
public BufferedImage setBackgroundImage(BufferedImage bi) {
bg=bi;
}
}
Now we use it like so:
MyPanel mp=new MyPanel(...);//create the panel with an image
...
add(mp);//add to container
...
mp.setBackgroundImage(..);//change the image being displayed
mp.repaint();//so the new image may be painted

My JComponent will only repaint() once I manually resize the window, how do I get it to work as it should?

I have tried my best to read around on the topic but I cannot find/understand how to apply the answers to my piece code (and those I have applied don't work)
I have used an example from "Ivor Horton's Beginning Java 2 JDK book" and it is the first time I'm using repaint() but it doesn't seem to work unless I resize the window. It tries to repaint but leaves the screen blank.
Here's the code please let me know if there is something I'm doing wrong.
public class CurveDrawings extends JFrame{
public CurveDrawings (String title){
setTitle(title);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pane = new CurvePane(); //Create pane containing curves
Container content = getContentPane(); //Get the content pane
//Add the pane containing the curves to the content pane for the applet
content.add(pane);
MouseHandler handler = new MouseHandler();
pane.addMouseListener(handler);
pane.addMouseMotionListener(handler);
}
//Class defining pane on which to draw
class CurvePane extends JComponent{
public CurvePane(){
quadCurve = new QuadCurve2D.Double(
startQ.x, startQ.y, control.x, control.y, endQ.x, endQ.y
);
cubicCurve = new CubicCurve2D.Double(
startC.x, startC.y, controlStart.x, controlStart.y,
controlEnd.x, controlEnd.y, endC.x, endC.y
);
}
}
class Marker{//not needed for my problem}
class MouseHandler extends MouseInputAdapter{
public void mousePressed(MouseEvent e){
if(ctrlQuad.contains(e.getX(),e.getY())){
selected = ctrlQuad;}
else if(ctrlCubic1.contains(e.getX(),e.getY())){
selected = ctrlCubic1;}
else if(ctrlCubic2.contains(e.getX(),e.getY())){
selected = ctrlCubic2;}
}
public void mouseReleased (MouseEvent e){
selected = null;
}
public void mouseDragged (MouseEvent e){
System.out.println("DRAGGED");
if(selected != null){
//Set the marker to current cursor position
selected.setLocation(e.getX(),e.getY());
pane.validate();
pane.repaint();
}
}
Marker selected = null;
}
public void paint (Graphics g){
Graphics2D g2D = (Graphics2D)g; //Get a 2D device context
//Code to draw each component
}
//Points for quadratic curve
//Points for cubic curve
//More unnecessary code}
Incase it is any help here's the 'Launcher' class for the application
Thanks in advance.
You will need to call
super.paint(g);
in your paint method in your CurveDrawings class to avail of the painting functionality already provided in the JFrame super class.
Note, the standard way of using custom painting in Swing is to use custom components that are based on javax.swing.JComponent that use and override paintComponent. This approach would leverage Swing's optimized painting model.
See: Performing Custom Painting

repaint() fails to work

I wanted to draw an image on my panel based on the data I receive from another thread. I am sure the data and consequent pixel array works well, but the repaint() would never work. Can anyone tell me what's going wrong here?
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
/** Create an image from a pixel array. **/
public class PicturePlaza extends JApplet
{
ImagePanel fImagePanel;
ReadCom readComPort;
Thread readPortThread;
public void init () {
// initiate the read port thread so that it can receive data
readComPort = new ReadCom();
readPortThread = new Thread(readComPort,"ReadCom");
readPortThread.start();
Container content_pane = getContentPane ();
fImagePanel = new ImagePanel ();
content_pane.add (fImagePanel);
}
// Tell the panel to create and display the image, if pixel data is ready.
public void start () {
while(true){
if(readComPort.newPic){
fImagePanel.go();
}
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/** Create an image from a pixel array. **/
class ImagePanel extends JPanel{
Image fImage;
int fWidth = ReadCom.row, fHeight = ReadCom.col;
void go() {
//update the image if newPic flag is set to true
fImage = createImage (new MemoryImageSource (fWidth, fHeight, ReadCom.fpixel, 0, fWidth));
repaint();
readComPort.newPic = false; //disable the flag, indicating the image pixel has been used
}
/** Paint the image on the panel. **/
public void paintComponent (Graphics g) {
super.paintComponent (g);
g.drawImage (fImage, 0, 0, this );
}
}
}
Thanks
Just a little note on repaint(). repaint() schedules a repaint of the screen, it won't always do it immediately in my experience. I found the best solution is to directly call paint() yourself.
Graphics g;
g = getGraphics();
paint(g);
I put this as a new function to call in my code when I wanted it to paint immediately. Also this will not erase the previous graphics on the screen, you will have to do that manually.
Try repaint(); and then validate(); in your Applet (PicturePlaza).

Categories

Resources