0

I am very much new to Java so please bear with me.

I have a class that draws a picture called PictureRenderer, one of my methods is:

public class PictureRenderer extends JPanel implements MouseListener, MouseMotionListener, PropertyChangeListener {

//Begin First Method
private int drawPicture( int[] checks, int maxCheckCount, Graphics g) {
//code here
}

}

Then I have a rest controller:

@CrossOrigin(origins = "*", allowedHeaders = "*")
@GetMapping(value = "/servlet/checkRequest")
String getCheckRequest { 
    PictureRenderer renderPic = new PictureRenderer();

    try {
        renderPic.drawPicture(2, 2, g);
    } catch (Exception e) {
        System.out.println("Exception caught " + e);
    }
return "Good job?!"
}

My question is how do I get this to work? Ultimately what I want is since I have code that creates the picture already, I want to send it over to my website as I want to move away from JPanel. Please let me know if you need any other information.

Thank you for your help!!

Hobbitbob
  • 25
  • 5
  • What technology do you use in the frontend? – second Mar 20 '20 at 21:24
  • Just JS, HTML, CSS. I was able to pass a String over to my GUI so the servlet is up and it's connected. – Hobbitbob Mar 20 '20 at 21:26
  • 1
    Check this [answer](https://stackoverflow.com/a/9464137/11514534). Returing the image as base64 should work with a pure html frontend. – second Mar 20 '20 at 21:29
  • That's actually a great example. Thank you! The only thing is I still don't know how to create the image from Spring Boot, I get errors when I build it like how I've written it because it doesn't like 'g' as an argument. – Hobbitbob Mar 20 '20 at 21:36
  • Where does this `g` come from? Your code does not define it. You might want to add the error message if you have something more specific here. If you want to move away from the JPanel check this [example](https://stackoverflow.com/a/20425693/11514534) for how to draw on a graphics component using a `BufferedImage`. – second Mar 20 '20 at 21:45
  • The import java.awt.Graphics comes with this. Sorry, I left out my imports – Hobbitbob Mar 20 '20 at 21:47
  • Where do you declare and initialize the variable `g`? For all I can see you should get a compilation error. – second Mar 20 '20 at 21:48
  • Sorry, I'm not 100% sure. I didn't write the code that makes the image. I know that one of the args for the method drawPicture is Graphics g - and when I call drawPicture I can fill it in with fake args: drawPicture(2, 2, xxx) but I don't know what to put for xxx because that's where Graphics g is. Sorry I've I'm not making any sense :/ – Hobbitbob Mar 20 '20 at 21:51
  • Check out the BufferedImage answer I linked, that might answer your question where you get it from. I assume your render class just draws on this graphic component. Not sure why you would use a JPanel for this. – second Mar 20 '20 at 21:53

1 Answers1

1

This example

  • creates the image
  • encodes the image as a byte[]
  • encodes the byte[] as base64

as explained in the linked answers (return as base64 and how to create a image with java).

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.Base64;
import javax.imageio.ImageIO;
@GetMapping("/image")
@ResponseBody
public byte[] getImage() throws IOException {

    BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
    Graphics g = image.createGraphics();
    g.setColor(Color.blue);
    g.fillRect(0, 0, 100, 100);

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ImageIO.write(image, "png", bos);

    byte[] encoded = Base64.getEncoder().encode(bos.toByteArray());
    return encoded;
}
@GetMapping("/html")
@ResponseBody
public String getHtml() throws IOException {
   return "<img src=\"data:image/png;base64, " + new String(getImage(), "UTF8") + "\" alt=\"blue square\">";
}

Calling /image only returns the base64 data, while calling /html returns the complete html component. When done in a browser it should directly display the image.

second
  • 4,069
  • 2
  • 9
  • 24