paddy
paddy

Reputation: 123

Drawing Strings over PaintedImage Java

I need to draw strings on a painted image. The project I am working on also requires the string to move around the screen at least 22 times in a second. Hence, its position can be anywhere on the image. So, redrawing the image with the string on it won't be possible as I feel there are better ways of doing this and that would unnecessarily consume resources redrawing the whole image. I have also tried using panel.getGraphics and then painting on the image but then all the drawn text is all over the screen(the code is below). I was wondering if someone could guide me in the right direction on how I can draw text over a paintedImage but it also needs to reset its position when required. The code I have tried that doesn't reset its previous position is below.

Original Panel with the Image:

public class PanelForImages extends JPanel{

private BufferedImage image;

public PanelForImages(File image) throws IOException{
    //this.image = image;
    //URL resource = getClass().getResource("so2.jpg");
    this.image = ImageIO.read(image);

}

 @Override
 public void paintComponent(Graphics g){
    //super.paint(g);
    //super.paintComponents(g);
    super.paintComponent(g);
    //g.drawImage(image, 3, 4, this);
    g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
    g.drawString("HELLOOOOOOOOOOOOOOOOOOOOOOOO", 400, 400);
    //repaint();

 }
}

Method with which I am trying to draw the string over the image.

public void drawFixationsOnFrame(GazeData gazeData){

    Graphics g = this.panelForImages.getGraphics();


    g.drawString("TEsting 123", (int)gazeData.smoothedCoordinates.x, (int)gazeData.smoothedCoordinates.y);

    g.dispose();

    jF.revalidate();
}

I have also tried making a new panel and then adding it to the current one but it doesn't seem to work. I am not sure how I can make it so it comes on top of the panelForImages without it hiding panelForImages.

Upvotes: 0

Views: 49

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

The project I am working on also requires the string to move around the screen at least 22 times in a second. Hence, its position can be anywhere on the image. So, redrawing the image with the string on it won't be possible as I feel there are better ways of doing this and that would unnecessarily consume resources redrawing the whole image.

Then don't redraw the whole image. The JComponent repaint(...) method has an override, one that allows you to repaint only a select rectangle (check out the JComponent API for more on this). You will want to move the image in a Swing Timer, then repaint its old location (to get rid of the old String image) and repaint its new location. If you need the dimensions of the String, then use FontMetrics to help you get its bounding rectangle.

I note that your code has two other issues that are worrisome, some of it commented out:

  • Calling getGraphics() on a component to get its graphics component and draw with it -- you don't want to do this as this will result in a Graphics object that is short-lived risking broken images or a NPE
  • Calling repaint() from within the paintComponent. This is a bad and completely uncontrolled way of doing animation. Instead use a Swing Timer so you can have complete control of the animation and so you don't use paintComponent for something it was not intended to be used for.

Upvotes: 1

Related Questions