Hej
Nu har jeg i lang tid siddet med grå hår og spekuleret på, hvorfor dette
lille program, der skal vise et indeks' billedefiler ikke virker. Programmet
nægter at tegne andet end nogle bestemte filer og kun fra bestemte indeks???
Hvis der er nogen der gider se koden igennem, og komme med forslag til
rettelser vil jeg blive meget glad.
Mvh Poul
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
public class TestGetImage extends Frame implements ActionListener {
private ImageIcon[] icons;
private Vector vector = new Vector();
private TextField textField = new TextField(20);
private Button knap = new Button ("Skift indeks");
TestGetImage() {
super("Tester");
setLayout(new FlowLayout());
textField.addActionListener(this);
add(textField);
knap.addActionListener(this);
add(knap);
setSize(600,400);
setVisible(true);
repaint();
}//constr
public void update(Graphics g) {
paint(g);
}
public void search (String s) {
File name = new File(s);
if (name.isDirectory() ) {
String index[] = name.list();
vector.removeAllElements();
icons = new ImageIcon[index.length];
for (int i = 0; i < index.length;i++) {
if (index[i].endsWith(".gif")|| index[i].endsWith(".GIF"))
vector.add(index[i]);
// if (index[i].endsWith(".jpg")|| index[i].endsWith(".JPG"))
// vector.add(index[i]);
}
icons = new ImageIcon[vector.size()];
for (int x = 0; x < vector.size();x++) {
icons[x] = new ImageIcon( (String) vector.elementAt(x) );
System.out.println(""+ icons[x].getDescription());
}
}//if
repaint();
}//search
public void actionPerformed (ActionEvent e) {
String index = new String();
if (e.getSource() == textField) {
index = textField.getText();
}
if (e.getSource() == knap) {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(this);
File name = new File(chooser.getCurrentDirectory().toString());
index = name.getAbsolutePath();
textField.setText(index);
}
search(index);
}
public void paint(Graphics g) {
g.clearRect(0,0,this.getSize().width,this.getSize().height);
int xPos = 10;
int yPos = 70;
if (!vector.isEmpty())
for (int i = 0; i < icons.length;i++) {
icons[i].paintIcon(this,g,xPos,yPos);
xPos += icons[i].getIconWidth()+20;
if (xPos >= this.getSize().width-
xPos = 20;
yPos += icons[i].getIconHeight()+10;
}
}
}
public static void main(String args[]) {
TestGetImage g = new TestGetImage();
g.addWindowListener(new CloseWindowAndExit());
}
}//class
class CloseWindowAndExit extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}//class
|