• 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

Background image

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Patrick Thorse
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
We're being followed by intergalactic spies! Quick! Take this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic