W
W
wolf-98302014-06-07 20:04:16
Java
wolf-9830, 2014-06-07 20:04:16

How to get image from array in java swing?

I read somewhere that you can convert an array with numbers into an image, how to do it in java swing?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pi314, 2014-06-09
@wolf-9830

With what numbers exactly? To what image? And most importantly - what does Swing have to do with it?
If "numbers" means an array of bytes containing, for example, a JPEG-encoded image that you want to have in an ImageIcon (which seems to me the most likely interpretation of the question in the context of Swing), then, for example, like this:

byte[] arBytes;
...
ImageIcon icon = new ImageIcon(arBytes);

or like this:
If the numbers are really numbers, then, alas, there is no other way to "get a picture", otherwise how to take and draw them on the canvas, for example, like this:
int[] arInts = {0,1,2,3,4,5,6,7,8,9};
...
BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.setPaint(Color.red);
g2d.setFont(new Font("Serif", Font.BOLD, 18));
FontMetrics fm = g2d.getFontMetrics();
int x = 5;
for(int i : arInts){
    String s = (new Integer(i)).toString();    
    g2d.drawString(s, x, 5);
    x += fm.stringWidth(s) + 5;
}
g2d.dispose();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question