qwerty86
05-10-2009, 15:11
Salve a tutti per un progetto devo realizzare una sorta di scacchiera e nelle celle deve comparire o meno un immagine. Ho messo su questo :
import javax.swing.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
class Square extends JPanel
{
private static final Color white = new Color(255,255,255);
private static final Color black = new Color(4333838);
private static ImageIcon img = new ImageIcon("ant.gif");;
private JLabel ant = new JLabel(img);
private JLabel nonAnt = new JLabel();
public Square()
{
this.setBackground(white);
this.setBorder(BorderFactory.createLineBorder(Color.black));
}
public void setState(boolean even)
{
this.removeAll();
this.add(even ? ant: nonAnt);
//this.repaint();
//this.validate();
this.revalidate();
}
}
public class ChessBoard extends JFrame
{
/** Creates a new instance of ChessBoard */
public ChessBoard(int ii,int jj)
{
super("Simplest Chessboard Ever");
this.setLayout(new BorderLayout());
JPanel center = new JPanel();
center.setName("center");
center.setLayout(new GridLayout(ii,jj));
for (int i = 0; i < ii; i++)
{
for (int j = 0; j < jj; j++)
{
Square sq = new Square();
sq.setName(i+"-"+j);
center.add(sq);
}
}
this.add(center, BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(400,400);
this.setVisible(true);
}
public static void main (String[] args) throws IOException
{
File f=new File("antConfig.txt");
FileInputStream fis=new FileInputStream(f);
InputStreamReader isr =new InputStreamReader(fis);
BufferedReader br= new BufferedReader(isr);
ChessBoard grid = new ChessBoard(6,6);
JRootPane root = ((JRootPane)grid.getComponent(0));
JPanel center = ((JPanel)root.getComponent(1).getComponentAt(0,0).getComponentAt(0, 0));
for (int i = 0; i <6; i++)
{
String line = br.readLine();
for(int j = 0;j<6;j++)
{
if(line.charAt(j) == '0')
{
((Square) center.getComponent(i*6+j)).setState(false);
}
else
{
((Square) center.getComponent(i*6+j)).setState(true);
}
}
}
}
}
A funzionare funziona, ma pensate che il tutto deve girare su macchiene distribuite e facendo una prova è abbastanza lento. C'è un modo più semplice di visualizzare una griglia n*m con la possibilità di inserirvi o meno una piccola immagine?
Grazie a tutti.
import javax.swing.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
class Square extends JPanel
{
private static final Color white = new Color(255,255,255);
private static final Color black = new Color(4333838);
private static ImageIcon img = new ImageIcon("ant.gif");;
private JLabel ant = new JLabel(img);
private JLabel nonAnt = new JLabel();
public Square()
{
this.setBackground(white);
this.setBorder(BorderFactory.createLineBorder(Color.black));
}
public void setState(boolean even)
{
this.removeAll();
this.add(even ? ant: nonAnt);
//this.repaint();
//this.validate();
this.revalidate();
}
}
public class ChessBoard extends JFrame
{
/** Creates a new instance of ChessBoard */
public ChessBoard(int ii,int jj)
{
super("Simplest Chessboard Ever");
this.setLayout(new BorderLayout());
JPanel center = new JPanel();
center.setName("center");
center.setLayout(new GridLayout(ii,jj));
for (int i = 0; i < ii; i++)
{
for (int j = 0; j < jj; j++)
{
Square sq = new Square();
sq.setName(i+"-"+j);
center.add(sq);
}
}
this.add(center, BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(400,400);
this.setVisible(true);
}
public static void main (String[] args) throws IOException
{
File f=new File("antConfig.txt");
FileInputStream fis=new FileInputStream(f);
InputStreamReader isr =new InputStreamReader(fis);
BufferedReader br= new BufferedReader(isr);
ChessBoard grid = new ChessBoard(6,6);
JRootPane root = ((JRootPane)grid.getComponent(0));
JPanel center = ((JPanel)root.getComponent(1).getComponentAt(0,0).getComponentAt(0, 0));
for (int i = 0; i <6; i++)
{
String line = br.readLine();
for(int j = 0;j<6;j++)
{
if(line.charAt(j) == '0')
{
((Square) center.getComponent(i*6+j)).setState(false);
}
else
{
((Square) center.getComponent(i*6+j)).setState(true);
}
}
}
}
}
A funzionare funziona, ma pensate che il tutto deve girare su macchiene distribuite e facendo una prova è abbastanza lento. C'è un modo più semplice di visualizzare una griglia n*m con la possibilità di inserirvi o meno una piccola immagine?
Grazie a tutti.