Nie jesteś zalogowany.
Jeśli nie posiadasz konta, zarejestruj je już teraz! Pozwoli Ci ono w pełni korzystać z naszego serwisu. Spamerom dziękujemy!
Prosimy o pomoc dla małej Julki — przekaż 1% podatku na Fundacji Dzieciom zdazyć z Pomocą.
Więcej informacji na dug.net.pl/pomagamy/.
Strony: 1
Witam Debianowców.
Mam problem z metodą saveFile w moim programie-ogolnie program służy do prostych operacji graficznych na wczytywanych plikach-zmienione pliki chciałbym zapisywać jednak mam coś źle we wcześniej wspomnianej metodzie. Wstawię osobno metody openFile i saveFile.
/* metody * To change this template, choose Tools | Templates * and open the template in the editor. */ package javaapplication8; import java.awt.BorderLayout; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.geom.AffineTransform; import java.awt.image.AffineTransformOp; import java.awt.image.BufferedImage; import java.awt.image.BufferedImageOp; import java.awt.image.ByteLookupTable; import java.awt.image.ConvolveOp; import java.awt.image.Kernel; import java.awt.image.LookupOp; import java.awt.image.RescaleOp; import java.awt.print.PageFormat; import java.awt.print.Printable; import java.awt.print.PrinterException; import java.awt.print.PrinterJob; import java.io.File; /*import java.io.IOException; import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.Set; import java.util.TreeSet; import javax.imageio.IIOImage; import javax.imageio.ImageIO; import javax.imageio.ImageReader; import javax.imageio.ImageWriter; import javax.imageio.stream.ImageOutputStream;*/ import java.io.FileNotFoundException; import java.io.IOException; import java.util.Iterator; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.IIOImage; import javax.imageio.ImageIO; //import javax.swing.ImageIcon; import javax.imageio.ImageWriteParam; import javax.imageio.ImageWriter; import javax.imageio.stream.FileImageOutputStream; import javax.imageio.stream.ImageOutputStream; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author E5500 */ class Ramka extends JFrame implements Printable{ public Ramka() { setTitle("Program graficzny"); setSize(800,1000); //panel-transformacje obrazu JPanel panel=new JPanel() { @Override public void paintComponent(Graphics g) { super.paintComponents(g); if (image!=null)g.drawImage(image, 0, 0, null);} }; add(panel, BorderLayout.CENTER); //menu programu JMenuBar menuBar=new JMenuBar(); setJMenuBar(menuBar); JMenu menu=new JMenu("Plik"); JMenu processingimage=new JMenu("Transformacje obrazu"); JMenu about=new JMenu("O programie"); menuBar.add(menu); menuBar.add(processingimage); menuBar.add(about); JMenuItem openitem=new JMenuItem("Otworz Plik"); JMenuItem exititem=new JMenuItem("Wyjscie"); JMenuItem saveitem=new JMenuItem("Zapisz"); JMenuItem printitem=new JMenuItem("Drukuj"); JMenuItem bluritem=new JMenuItem("Blur"); JMenuItem sharpenitem=new JMenuItem("Sharpen"); JMenuItem brightenitem=new JMenuItem("Bright"); JMenuItem edgeitem=new JMenuItem("Edge"); JMenuItem negativeitem=new JMenuItem("Negative"); JMenuItem rotateitem=new JMenuItem("Rotate"); JMenuItem aboutitem=new JMenuItem("O programie"); menu.add(openitem); menu.add(saveitem); menu.add(printitem); menu.add(exititem); processingimage.add(bluritem); processingimage.add(sharpenitem); processingimage.add(brightenitem); processingimage.add(edgeitem); processingimage.add(negativeitem); processingimage.add(rotateitem); about.add(aboutitem); /*ramka do wyswietlania pogldadu zdjec label=new JLabel(); add(label);*/ openitem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { openFile(); } }); saveitem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { saveFile(formatName); } }); printitem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { printFile(); } }); exititem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { System.exit(0); } }); bluritem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { float weight=1.0f/9.0f; float[] elements=new float[9]; for(int i=0;i<9;i++)elements[i]=weight; convolve(elements); } }); brightenitem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { float a=1.1f; float b= -20.0f; RescaleOp op=new RescaleOp(a, b, null); filter(op); } }); edgeitem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { float[] elements= {0.0f, -1,0f, 0.0f, -1.0f, 4.f, -1,0f, 0.0f, -1.0f, 0.0f }; convolve (elements);} }); sharpenitem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { float[] elements= { 0.0f, -1.0f, 0.0f, -1.0f, 5.f, -1.0f, 0.0f, -1.0f, 0.0f, }; convolve(elements); } }); negativeitem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { byte negative[]=new byte[256]; for(int i=0;i<256;i++)negative[i]=(byte)(255-i); ByteLookupTable table=new ByteLookupTable(0, negative); LookupOp op=new LookupOp(table, null); filter(op); } }); rotateitem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { if(image==null)return; AffineTransform transform=new AffineTransform().getRotateInstance(Math.toRadians(10), image.getWidth()/4, image.getHeight()/4); AffineTransformOp op=new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR); filter(op); } }); aboutitem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { JOptionPane.showMessageDialog(null, "Program stworzony na przedmiot " + "Warsztaty Programistyczne."); } }); } [b] //ZAPISZ PLIK public void saveFile(final String formatName) { JFileChooser chooser=new JFileChooser(); chooser.setCurrentDirectory(new File(".")); chooser.setFileFilter(new javax.swing.filechooser.FileFilter() { public boolean accept(File f) { String name= f.getName().toLowerCase(); return name.endsWith(".jpg") || f.isDirectory(); } public String getDescription(){ return "Zapisz plik jako jpg"; } } ); int r=chooser.showSaveDialog(Ramka.this); if(r!=JFileChooser.APPROVE_OPTION)return; try { //image=ImageIO.write(chooser.getSelectedFile()); image=ImageIO.write(chooser.getSelectedFile()); } catch (IOException e){ JOptionPane.showMessageDialog(this, e); } //repaint(); } [/b] //metoda otwierajaca pliki graficzne public void openFile() { JFileChooser chooser=new JFileChooser(); chooser.setCurrentDirectory(new File(".")); chooser.setFileFilter(new javax.swing.filechooser.FileFilter() { public boolean accept(File f) { String name= f.getName().toLowerCase(); return name.endsWith(".gif")||name.endsWith(".jpg")||name.endsWith(".jpeg") || f.isDirectory(); } public String getDescription(){ return "Pliki graficzne"; } } ); int r=chooser.showOpenDialog(Ramka.this); if(r!=JFileChooser.APPROVE_OPTION)return; try { image=ImageIO.read(chooser.getSelectedFile()); } catch (IOException e){ JOptionPane.showMessageDialog(this, e); } repaint(); //fileName=chooser.getSelectedFile().getPath(); //label.setIcon(new ImageIcon(fileName)); } public void printFile() { PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintable(this); boolean ok = job.printDialog(); if (ok) { try { job.print(); } catch (PrinterException ex) { } } } private void filter(BufferedImageOp op) { if(image==null)return; BufferedImage filteredImage=new BufferedImage(image.getWidth(), image.getHeight(), image.getType()); op.filter(image, filteredImage); image=filteredImage; repaint(); } //odrysowanie ponowne panela private void convolve(float[] elements) { Kernel kernel=new Kernel(3, 3, elements); ConvolveOp op=new ConvolveOp(kernel); filter(op); } private JLabel label; private String fileName; private BufferedImage image; private String formatName; public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { throw new UnsupportedOperationException("Not supported yet."); } }
[b]metoda openFile[/b]
public void openFile() { JFileChooser chooser=new JFileChooser(); chooser.setCurrentDirectory(new File(".")); chooser.setFileFilter(new javax.swing.filechooser.FileFilter() { public boolean accept(File f) { String name= f.getName().toLowerCase(); return name.endsWith(".gif")||name.endsWith(".jpg")||name.endsWith(".jpeg") || f.isDirectory(); } public String getDescription(){ return "Pliki graficzne"; } } ); int r=chooser.showOpenDialog(Ramka.this); if(r!=JFileChooser.APPROVE_OPTION)return; try { image=ImageIO.read(chooser.getSelectedFile()); } catch (IOException e){ JOptionPane.showMessageDialog(this, e); } repaint(); //fileName=chooser.getSelectedFile().getPath(); //label.setIcon(new ImageIcon(fileName)); }
Problematyczna metoda saveFile
public void saveFile(final String formatName) { JFileChooser chooser=new JFileChooser(); chooser.setCurrentDirectory(new File(".")); chooser.setFileFilter(new javax.swing.filechooser.FileFilter() { public boolean accept(File f) { String name= f.getName().toLowerCase(); return name.endsWith(".jpg") || f.isDirectory(); } public String getDescription(){ return "Zapisz plik jako jpg"; } } ); int r=chooser.showSaveDialog(Ramka.this); if(r!=JFileChooser.APPROVE_OPTION)return; try { //image=ImageIO.write(chooser.getSelectedFile()); image=ImageIO.write(chooser.getSelectedFile()); } catch (IOException e){ JOptionPane.showMessageDialog(this, e); }
W metodzie saveFile problem jest z liniką kodu:[b]image=ImageIO.write(chooser.getSelectedFile());[/b]
Na chłopski rozum tak chciałem zapisać wczytany wcześniej plik jednak [b]write[/b] tu nie pasuje- netbeans mówi "cannot find symbol"(chodzi o write).
Liczę na pomoc.
Pozdrawiam.
Ostatnio edytowany przez sonic81 (2009-09-04 11:32:38)
Offline
Jaki wyjątek dostajesz? Wklej stack trace.
image=ImageIO.write(chooser.getSelectedFile());
Skąd ci to przyszło do głowy? Nie pisz domyślając się, sprawdź jakie parametry przyjmuje metoda ImageIO.write().
Offline
[quote=kamikaze]Jaki wyjątek dostajesz? Wklej stack trace.
image=ImageIO.write(chooser.getSelectedFile());
Skąd ci to przyszło do głowy? Nie pisz domyślając się, sprawdź jakie parametry przyjmuje metoda ImageIO.write().[/quote]
Teraz metoda saveFile wygląda tak:
public void saveFile(File file) { JFileChooser chooser=new JFileChooser(); chooser.setCurrentDirectory(new File(".")); chooser.setFileFilter(new javax.swing.filechooser.FileFilter() { public boolean accept(File f) { String name= f.getName().toLowerCase(); return name.endsWith(".jpg") || f.isDirectory(); } public String getDescription(){ return "Zapisz plik jako"; } } ); int r=chooser.showSaveDialog(Ramka.this); if(r!=JFileChooser.SAVE_DIALOG)return; this.fileName = file.getName(); String suffix = fileName.substring(fileName.lastIndexOf('.') + 1); suffix = suffix.toLowerCase(); if (suffix.equals("jpg") ) { try { ImageIO.write(image, suffix, file); } catch (IOException e) { e.printStackTrace(); } } else { System.out.println("Blad:plik musi byc zapisany jako JPG"); } }
Teraz mam błąd w actionlistenerze
saveitem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { saveFile(File file); } });
Jak wywołać poprawnie metodę saveFile w listenerze w tym przypadku?
Ok. Teraz lisener wygląda tak i nie ma już błędów:
saveitem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { saveFile(null); } });
[b]Nie ma żadnych błędów, ale nie zapisuje w ogole pliku[/b]
Ostatnio edytowany przez sonic81 (2009-09-05 15:03:26)
Offline
Strony: 1
Time (s) | Query |
---|---|
0.00008 | SET CHARSET latin2 |
0.00005 | SET NAMES latin2 |
0.00085 | SELECT u.*, g.*, o.logged FROM punbb_users AS u INNER JOIN punbb_groups AS g ON u.group_id=g.g_id LEFT JOIN punbb_online AS o ON o.ident='13.59.2.242' WHERE u.id=1 |
0.00066 | UPDATE punbb_online SET logged=1732693045 WHERE ident='13.59.2.242' |
0.00046 | SELECT * FROM punbb_online WHERE logged<1732692745 |
0.00092 | SELECT topic_id FROM punbb_posts WHERE id=127254 |
0.00115 | SELECT id FROM punbb_posts WHERE topic_id=14925 ORDER BY posted |
0.00073 | SELECT t.subject, t.closed, t.num_replies, t.sticky, f.id AS forum_id, f.forum_name, f.moderators, fp.post_replies, 0 FROM punbb_topics AS t INNER JOIN punbb_forums AS f ON f.id=t.forum_id LEFT JOIN punbb_forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id=3) WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND t.id=14925 AND t.moved_to IS NULL |
0.00005 | SELECT search_for, replace_with FROM punbb_censoring |
0.00103 | SELECT u.email, u.title, u.url, u.location, u.use_avatar, u.signature, u.email_setting, u.num_posts, u.registered, u.admin_note, p.id, p.poster AS username, p.poster_id, p.poster_ip, p.poster_email, p.message, p.hide_smilies, p.posted, p.edited, p.edited_by, g.g_id, g.g_user_title, o.user_id AS is_online FROM punbb_posts AS p INNER JOIN punbb_users AS u ON u.id=p.poster_id INNER JOIN punbb_groups AS g ON g.g_id=u.group_id LEFT JOIN punbb_online AS o ON (o.user_id=u.id AND o.user_id!=1 AND o.idle=0) WHERE p.topic_id=14925 ORDER BY p.id LIMIT 0,25 |
0.00074 | UPDATE punbb_topics SET num_views=num_views+1 WHERE id=14925 |
Total query time: 0.00672 s |