aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Background image Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Background image" Watch "Background image" New topic
Author

Background image

Patrick Thorse
Greenhorn

Joined: Jun 15, 2001
Posts: 5
I'm new to Java. I created a simple drawing tool. I want to display a background image, and I do that now in the paint( ) method, but then the image is refreshed every time the paint method is called. How can I display the image only one time possibly in the init( ) at startup?
Thanks.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

I do not understand what you are talking about... when you mention a "background image" are you talking about a gif/jpg image that you are drawing to the screen, or are you talking about the off screen buffer, as used in double-buffering? If you are trying to draw on top of an image, the method you described will do that, however, there are problems with it... The problem with only drawing the image in the init method is that if the graphics context gets invalidated ( i.e. minimized window, other windows on top of, etc. ) then the image will have been lost... Since I don't know that much about what you are trying to do, I can only suggest that perhaps you should have the code for drawing the image before anything else in the paint method, or perhaps use some sort of modified double-buffering technique where the background image is saved seperately from what is drawn on top of it... I can't really help out more than this without more info about what you are doing...


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Patrick Thorse
Greenhorn

Joined: Jun 15, 2001
Posts: 5
I need to be able to see the picture of a farm field in the background and then trace a simple line around each field. I can do that now, but each time I click to add a line, the paint() method is called and adds a line but also redraws the "background" image. Is there a way to add the image once and then just add grapics(lines/points) on top of that image?

Originally posted by Nathan Pruett:
I do not understand what you are talking about... when you mention a "background image" are you talking about a gif/jpg image that you are drawing to the screen, or are you talking about the off screen buffer, as used in double-buffering? If you are trying to draw on top of an image, the method you described will do that, however, there are problems with it... The problem with only drawing the image in the init method is that if the graphics context gets invalidated ( i.e. minimized window, other windows on top of, etc. ) then the image will have been lost... Since I don't know that much about what you are trying to do, I can only suggest that perhaps you should have the code for drawing the image before anything else in the paint method, or perhaps use some sort of modified double-buffering technique where the background image is saved seperately from what is drawn on top of it... I can't really help out more than this without more info about what you are doing...

Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

I think that the background image has to be redrawn each time because the default background is white... if you draw the background, and then try to draw a buffer on top of it, you will end up with the lines drawn on a white background ( because the white background of the buffer is drawn over the image ).

You could draw the image once and draw on top of it without buffers, but then the lines would disappear if the drawing area is invalidated ( i.e. minimized, frame moves over it, etc. ).

If you are having problems because the image is large and is taking a long time to redraw, think about cutting it into smaller pieces and using a GridLayout to display them...

-Nate
[This message has been edited by Nathan Pruett (edited June 18, 2001).]
Patrick Thorse
Greenhorn

Joined: Jun 15, 2001
Posts: 5
New question. Look at the following code. I am able to draw a polygon. When I click on the 'Polygon' button, it erases the current polygon and I'm able to draw a new polygon. How can I draw a polygon, and then draw another polygon without the first one being erased? I'm thinking that the paint method maybe isn't what I'm looking for?
Thanks. See code below.
import java.awt.*;
import java.applet.Applet;
import java.io.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
//****************************
// class polygon
//****************************
public class polygon3 extends Applet
{
final int NUMPOINTS = 100;
Button polygonButton;
int coordx[];
int coordy[];
int numCoord = 0;
int ctr = 0;
int i = 0;
int isFinish = 0; // 0 = false, 1 = true
int isNew = 0; // 0 = false, 1 = true
int isFirst = 1; //
//**************************
// init
//**************************
public void init()
{
polygonButton = new Button ("Polygon");
add(polygonButton);
coordx = new int[NUMPOINTS];
coordy = new int[NUMPOINTS];
for (i = 0; i < NUMPOINTS; i++)
{
coordx[i] = 0;
coordy[i] = 0;
}
}//end of method init
//****************************
// action
//****************************
public boolean action(Event e, Object o)
{
if (e.target == polygonButton)
{
isFinish = 0;
isNew = 1;
isFirst = 1;
for (i = 0; i < NUMPOINTS; i++)
{
coordx[i] = 0;
coordy[i] = 0;
numCoord = 0;
}// end for i
}// end if polygonButton
repaint();
return true;
}//end of method action
//****************************
// mouseDown
//****************************
public boolean mouseDown (Event evt, int x, int y)
{
if (isNew == 0)
{
if (x-coordx[0] < 2)
{
if (y-coordy[0] < 2)
{
isFinish = 1;
}
}
}
if (isFinish == 0) {
coordx[numCoord] = x;
coordy[numCoord] = y;
numCoord++;
}
repaint();
return true;
}// end mouseDown
//****************************
// mouseDrag
//****************************
public boolean mouseDrag (Event evt, int x, int y)
{
return true;
}// end mouseDrag
//****************************
// mouseUp
//****************************
public boolean mouseUp (Event evt, int x, int y)
{
return true;
}// end mouseUp
//****************************
// paint
//****************************
public void paint(Graphics g)
{
g.setColor(Color.white);
g.drawString("test",10,10);
// if (isNew == 1) {
//Image theImage = getImage(getCodeBase(), "field.jpg");
//Graphics2D g2D = (Graphics2D) g;
//g2D.drawImage(theImage, 0, 0, this);
// }
if (isFirst == 0)
{
if (coordx[i+1] == coordx[0])
{
if (coordy[i+1] == coordy[0])
{
isFinish = 1;
}
}
}
if (isNew == 1)
{
isNew = 0;
}
String myVar = String.valueOf(numCoord);
g.drawString(myVar,25,45);
for (i = 0; i < numCoord-1; i++)
{
if (isFinish == 0) {
isFirst = 0;
g.setColor(Color.green);
g.drawLine(coordx[i],coordy[i],coordx[i+1],coordy[i+1]);
g.setColor(Color.red);
g.setColor(Color.red);
g.drawRect(coordx[i] - 1,coordy[i] -1,2,2);
}
}
if (isFinish == 1)
{
g.setColor(Color.black);
g.drawPolygon(coordx,coordy,numCoord);
}
}//end of method paint
}//end of class polygon
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Background image
 
Similar Threads
Painting over a image
Confused on how to make 3 random images in an applet
Java Applet, Moving a ball
avoiding flickering
with what can i show images!?