import java.lang.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
import java.awt.image.*;
import java.util.*;
import java.util.List;
import javax.imageio.stream.*;
public class POST{
public static void main(String[] args){
MyFrame mf = new MyFrame();
mf.setBounds( 0, 0, 1024, 768);
mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mf.show();
}
}
class MyFrame extends JFrame {
private JMenuBar menuBar;
private JFileChooser fileChooser;
private BufferedImage[] images;
private static Set readerSuffixes = getReaderSuffixes();
private static Set writerFormats = getWriterFormats();
private final JPanel panel;
private Point2D.Double p1 = new Point2D.Double(); // 始点
private Point2D.Double p2 = new Point2D.Double(); // 終点
private Line2D.Double line = new Line2D.Double(); // 直線
private BufferedImage image = null;
private boolean flag = false;
private File[] files;
private static int imageCounter=0;
public MyFrame() {
super("PaintOpenSaveText");
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
splitPane.setLeftComponent(createPanel1());
splitPane.setRightComponent(createPanel2());
splitPane.setDividerSize(2);
splitPane.setDividerLocation(700);
Container content = this.getContentPane();
content.add(splitPane, BorderLayout.CENTER);
panel = new JPanel() {
public void paintComponent(Graphics g) {
super.paintComponent(g);
((Graphics2D)g).drawImage(image, 0, 0, this);
}
};
image = new BufferedImage(600,500,BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D)image.getGraphics();
g.setColor(panel.getBackground());
g.fillRect(0,0,600,500);
g.dispose();
this.getContentPane().add(new JScrollPane(panel), BorderLayout.CENTER);
panel.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
p1.setLocation(p2);
p2.setLocation((double)e.getX(), (double)e.getY());
if(image==null) {
Dimension d = panel.getSize();
int w = d.width;
int h = d.height;
image = (BufferedImage)panel.createImage(w,h);
}
Graphics2D imageGraphics = (Graphics2D)image.createGraphics();
imageGraphics.setStroke(new BasicStroke(3.0F));
imageGraphics.setPaint(Color.black);
if(flag) {
imageGraphics.drawImage(image, 0, 0, null);
flag = false;
}
if(!p1.equals(p2)) {
line.setLine(p1,p2);
imageGraphics.draw(line);
p1.setLocation(p2);
}
imageGraphics.dispose();
repaint();
}
});
panel.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
p1.setLocation((double)e.getX(), (double)e.getY());
p2.setLocation(p1);
}
});
JMenu fileMenu = new JMenu("File");
JMenuItem openItem = new JMenuItem("Open");
openItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
openFile();
}
});
fileMenu.add(openItem);
JMenu saveMenu = new JMenu("Save");
Iterator iter = writerFormats.iterator();
while (iter.hasNext()) {
final String formatName = (String)iter.next();
JMenuItem formatItem = new JMenuItem(formatName);
saveMenu.add(formatItem);
formatItem.addActionListener(new
ActionListener() {
public void actionPerformed(ActionEvent event) {
saveFile(formatName);
}
});
}
fileMenu.add(saveMenu);
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
fileMenu.add(exitItem);
JMenuBar menuBar = new JMenuBar();
menuBar.add(fileMenu);
setJMenuBar(menuBar);
}
public void openFile() {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setFileFilter(new
javax.swing.filechooser.FileFilter() {
public boolean accept(File f) {
if (f.isDirectory()) return true;
String name = f.getName();
int p = name.lastIndexOf('.');
if (p == -1) return false;
String suffix
= name.substring(p + 1).toLowerCase();
return readerSuffixes.contains(suffix);
}
public String getDescription() {
return "Image files";
}
});
int r = chooser.showOpenDialog(this);
if (r != JFileChooser.APPROVE_OPTION) return;
File f = chooser.getSelectedFile();
try{
image = ImageIO.read(f.toURL());
panel.setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
flag = true;
}catch(IOException exception) {
JOptionPane.showMessageDialog(this, exception);
}
panel.revalidate();
}
private class imageFileSaveListener implements ActionListener{
private BufferedImage images;
public void actionPerformed(ActionEvent evt){
JFileChooser chooser = new JFileChooser();
if(files[imageCounter-1]!=null){chooser.setCurrentDirectory(files[imageCounter-1]);}
final ExtensionFileFilter filter = new ExtensionFileFilter();
filter.addExtension("jpg");
filter.addExtension("jpeg");
filter.setDescription("jpg,jpeg");
int result= chooser.showSaveDialog(MyFrame.this);
if(result == JFileChooser.APPROVE_OPTION){
File f=chooser.getSelectedFile();
}
}
}
public void saveFile(final String formatName){
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
Iterator iter = ImageIO.getImageWritersByFormatName(formatName);
ImageWriter writer = (ImageWriter)iter.next();
chooser.setFileFilter(new
javax.swing.filechooser.FileFilter(){
public boolean accept(File f){
if (f.isDirectory()) return true;
String name = f.getName();
int p = name.lastIndexOf('.');
if (p == -1) return false;
String suffix = name.substring(p + 1).toLowerCase();
return writerFormats.contains(suffix);
}
public String getDescription() {
return "Image files";
}
});
int r = chooser.showSaveDialog(this);
if (r != JFileChooser.APPROVE_OPTION) return;
File f = chooser.getSelectedFile();
try {
ImageOutputStream imageOut = ImageIO.createImageOutputStream(f);
writer.setOutput(imageOut);
writer.write(image);
writer.dispose();
imageOut.close();
}catch (IOException exception) {
JOptionPane.showMessageDialog(this, exception);
}
}
public static Set getReaderSuffixes() {
TreeSet readerSuffixes = new TreeSet();
String[] informalNames = ImageIO.getReaderFormatNames();
for (int i = 0; i < informalNames.length; i++) {
Iterator iter = ImageIO.getImageReadersByFormatName(
informalNames[i]);
while (iter.hasNext()) {
ImageReader reader = (ImageReader)iter.next();
String[] s = reader.getOriginatingProvider()
.getFileSuffixes();
readerSuffixes.addAll(Arrays.asList(s));
}
}
return readerSuffixes;
}
public static Set getWriterFormats() {
TreeSet writerFormats = new TreeSet();
TreeSet formatNames = new TreeSet(Arrays.asList(
ImageIO.getWriterFormatNames()));
while (formatNames.size() > 0) {
String name = (String)formatNames.iterator().next();
Iterator iter = ImageIO.getImageWritersByFormatName(name);
ImageWriter writer = (ImageWriter)iter.next();
String[] names = writer.getOriginatingProvider().getFormatNames();
writerFormats.add(names[0]);
formatNames.removeAll(Arrays.asList(names));
}
return writerFormats;
}
private JPanel createPanel1() {
JPanel p = new JPanel();
p.setLayout(null);
return p;
}
private JPanel createPanel2() {
JPanel p = new JPanel();
p.setLayout(null);
labela = new JLabel("苗字");
labela.setLocation(10,10);
labela.setSize(100,25);
getContentPane().add(labela);
fielda = new JTextField();
fielda.setLocation(10,30);
fielda.setSize(150,25);
getContentPane().add(fielda);
labelb = new JLabel("ID");
labelb.setLocation(10,60);
labelb.setSize(100,25);
getContentPane().add(labelb);
fieldb = new JTextField();
fieldb.setLocation(10,80);
fieldb.setSize(150,25);
getContentPane().add(fieldb);
labelc = new JLabel("名前");
labelc.setLocation(10,110);
labelc.setSize(100,25);
getContentPane().add(labelc);
fieldc = new JTextField();
fieldc.setLocation(10,130);
fieldc.setSize(150,25);
getContentPane().add(fieldc);
labeld = new JLabel("コメント");
labeld.setLocation(10,160);
labeld.setSize(100,25);
getContentPane().add(labeld);
area = new JTextArea();
area.setLocation(10,180);
area.setSize(270,250);
area.setWrapStyleWord(true);
getContentPane().add(area);
return p;
}
}
class ExtensionFileFilter {
public void addExtension(String extension) {
if (!extension.startsWith("."))
extension = "." + extension;
extensions.add(extension.toLowerCase());
}
public void setDescription(String aDescription) {
description = aDescription;
}
public String getDescription() {
return description;
}
public boolean accept(File f){
if (f.isDirectory()) return true;
String name = f.getName().toLowerCase();
for (int i = 0; i < extensions.size(); i++)
if (name.endsWith((String)extensions.get(i)))
return true;
return false;
}
private String description = "";
private ArrayList extensions = new ArrayList();
}
|