Originariamente inviato da fisico83
Ciao a tutti...qualcuno sa se è possibile trasformare un intero in immagine?
O meglio: ho memorizzato dei valori numerici in un array, e devo assegnare all'array una immagine.
È possibile creare dinamicamente una immagine e ci sono svariati modi per farlo:
Ad esempio:
codice:
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
public class ImageTestFrame extends JFrame
{
public ImageTestFrame ()
{
super ("Image Test Frame");
int width = 40;
int height = 40;
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
pixels[y*width+x] = x % 8 < 4 ? 0xFFFF0000 /*rosso*/ : 0xFF00FF00 /*giallo*/;
}
}
MemoryImageSource source = new MemoryImageSource (width, height, pixels, 0, width);
Toolkit toolkit = Toolkit.getDefaultToolkit ();
Image img = toolkit.createImage (source);
ImageIcon imgIcon = new ImageIcon (img);
JLabel label = new JLabel (imgIcon);
add (label);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
pack ();
}
public static void main (String[] args)
{
SwingUtilities.invokeLater (new Runnable ()
{
public void run ()
{
ImageTestFrame f = new ImageTestFrame ();
f.setVisible (true);
}
});
}
}
Oppure usare un BufferedImage:
codice:
int width = 40;
int height = 40;
BufferedImage img = new BufferedImage (width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = img.createGraphics ();
g2d.setColor (Color.RED);
g2d.fillRect (0, 0, width, height);
g2d.setColor (new Color (255, 192, 0));
g2d.fillRect (10, 10, 20, 20);
g2d.dispose ();
Comunque ci sono molti altri modi per creare le immagini, vedi qui solo per farti una idea.