Salve a tutti! Avrei bisogno di qualche consiglio per risolvere un problema (davvero stupido) ma piuttosto fastidioso!
Ho bisogno di disegnare una griglia su un'immagine e di colorarne delle caselle seguento particolari criteri (alla fine deve risultare una specie di mappa con i colori colegati alle temperature). Tralasciando per ora la parte dei "criteri" per la colorazione, ho scritto il codice che carica l'immagine, disegna la griglia e risalva l'immagine con la griglia. Il problema è che usando "SetBeckground" non riesco a colorare nessuna casella (satvo facendo delle prove con una casella a caso). Vi allego il codice..qualcuno ha suggerimenti? Grazie!
import javax.swing.*;
import java.awt.*;
import java.io.*;
import javax.imageio.*;
import java.awt.image.*;
public class temperatura extends JPanel{
static final int SIZE = 25;
JLabel[][] cell;
JLabel[][] cell2;
BufferedImage large;
public temperatura(){
setLayout(new GridLayout(SIZE, SIZE,0,0));
cell = new JLabel[SIZE][SIZE];
for(int x = 0; x < SIZE; x++){
for(int y=0; y < SIZE; y++){
cell[x][y] = new JLabel();
cell[x][y].setPreferredSize(new Dimension(20, 20));
cell[x][y].setBorder(BorderFactory.createLineBorder(Color.bl ack));
if (x == SIZE / 2 && y == SIZE / 2){
cell[x][y].setForeground(Color.white);
}
if (x==5 && y==10){
cell[x][y].setBackground(new Color(10180255));
}
add(cell[x][y]);
}
}
}
public void paintComponent(Graphics g){
super.paintComponent(g);
try {
if (large == null){
large = ImageIO.read(new File("YELGA.bmp"));
}
}
catch(Throwable thr){
thr.printStackTrace();
}
g.drawImage(large, 0, 0, this);
}
private RenderedImage comp2Image(Component cmp){
int width, height;
width = cmp.getWidth();
height = cmp.getHeight();
BufferedImage bImage
= new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bImage.createGraphics();
cmp.paint(g2d);
g2d.dispose();
return bImage;
}
public static void main(String[] args){
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
temperatura v = new temperatura();
f.getContentPane().add(v, BorderLayout.CENTER);
f.pack();
f.setVisible(true);
RenderedImage ri = v.comp2Image(v);
try{
ImageIO.write(ri, "jpg", new File("withGrid.jpg"));
}
catch (Exception e){
e.printStackTrace();
}
}
}

Rispondi quotando
