Image Editor
Hej!Jeg har lavet en image editor, men jeg kan kun åbne jpg filer og jeg vil meget gerne kunne åbne tif filer. Hjælp!
Koden for image ediotor:
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.*;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.filechooser.FileFilter;
import javax.swing.text.EditorKit;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.rtf.RTFEditorKit;
public class QuickPaneProg extends JFrame
{
HTMLEditorKit htmlKit;
RTFEditorKit rtfKit;
JFileChooser jfc;
JEditorPane jep;
EditorKit currentKit;
public QuickPaneProg(){
super("Text Panes");
htmlKit = new HTMLEditorKit();
rtfKit = new RTFEditorKit();
jep = new JEditorPane();
jep.setEditorKit(rtfKit);
jep.setPreferredSize(new Dimension(100,100));
JScrollPane jsp = new JScrollPane(jep);
jfc = new JFileChooser();
FileFilter ff = new FileFilter() {
public boolean accept(File f)
{
String name = f.getName().toLowerCase();
if (name.endsWith(".jpg") || name.endsWith(".txt") || name.endsWith(".tif") ||f.isDirectory())
return true;
else
return false;
}
public String getDescription()
{
return "Viewable (*.txt, *.jpg, *.tif )";
}
};
jfc.setFileFilter(ff);
JMenuItem open = new JMenuItem("Open...");
open.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
int ret = jfc.showOpenDialog(QuickPaneProg.this);
if (ret == JFileChooser.APPROVE_OPTION){
File f = jfc.getSelectedFile();
String name = f.getName();
try
{
URL file = f.getAbsoluteFile().toURL();
if (name.toLowerCase().endsWith(".txt")){
jep.setEditorKit(rtfKit);
jep.setPage(file);
} else {
jep.setEditorKit(htmlKit);
jep.setText("<img src=\""+file.toString()+"\">");
}
} catch (MalformedURLException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
});
JMenu file = new JMenu("File");
file.add(open);
JMenuBar menu = new JMenuBar();
menu.add(file);
getContentPane().add(jsp);
setJMenuBar(menu);
setSize(new Dimension(800,500));
setDefaultCloseOperation(EXIT_ON_CLOSE);
show();
}
public static void main(String[] args)
{ new QuickPaneProg(); }
}