芽可
|
分享:
▼
x0
|
[Java][求助] 請問在jScrollPane的圖片另存新檔的程式碼錯誤?
|
|
|
|
執行之後會跑出以下錯誤訊息: open3.java:110: cannot find symbol symbol : class ExampleFileFilter location: class open3 ExampleFileFilter filter = new ExampleFileFilter(); ^ open3.java:110: cannot find symbol symbol : class ExampleFileFilter location: class open3 ExampleFileFilter filter = new ExampleFileFilter(); ^ open3.java:114: cannot find symbol symbol : variable chooser location: class open3 chooser.setFileFilter(filter); ^ open3.java:115: parent is not public in java.awt.Component; cannot be accessed f rom outside package int returnVal = chooser.showOpenDialog(parent); ^ open3.java:115: cannot find symbol symbol : variable chooser location: class open3 int returnVal = chooser.showOpenDialog(parent); ^ open3.java:118: cannot find symbol symbol : variable chooser location: class open3 chooser.getSelectedFile().getName()); ^ open3.java:132: inner classes cannot have static declarations public static void main(String[] args) { ^ 7 errors 如果去掉public void saveImage1()這一段的話,則不會出現錯誤。 開啟檔案的方面很正常,但是在新增儲存功能的時候找了資料依然改不太出來>"< 儲存的部份搞不太懂,拜託幫我看一下問題出在哪QQ (PS:附件是有問題的JAVA檔案,因為不能上傳.java檔,請自行轉成java檔來執行QQ) import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.image.*; public class open3 extends JFrame implements ActionListener{ private Image m = null; private Image m2 = null; private String dir = null; JPanel contentPane; JScrollPane jScrollPane1 = new JScrollPane(); JScrollPane jScrollPane2 = new JScrollPane(); static FileDialog dialog; //宣告檔案對話方塊變數 JLabel jLabel1 = new JLabel(); JLabel jLabel2 = new JLabel(); MenuBar menubar1; //宣告功能表列變數 Menu mnuFile; //宣告功能表變數 MenuItem itmOpen1, itmExit, itmSave; //宣告功能項變數 java.awt.Image iconimg1; java.awt.Image iconimg2; //Construct the frame public open3() { enableEvents(AWTEvent.WINDOW_EVENT_MA .. 訪客只能看到部份內容,免費 加入會員 或由臉書 Google 可以看到全部內容
[ 此文章被芽可在2008-09-24 18:06重新編輯 ]
|
|
|
|
|
|
芽生可音
|
x0
[樓 主]
From:臺灣 | Posted:2008-09-24 10:48 |
|
|
overing
|
分享:
▲
▼
下面是引用芽可於2008-09-24 18:04發表的 :
請問如果不使用ExampleFileFilter這個CLASS的話,這段程式碼成立嗎? chooser要宣告成什麼@@?
.java檔不能上傳,所以我把程式碼放在.txt檔裡面上傳@@ 我想應該副檔名過濾的類別要換成 FileNameExtensionFilter 才對(比較好用) http://java.sun.com/javase/6/docs/api/javax/swi...eExtensionFilter.html建構式 FileNameExtensionFilter('說明文','附檔名1','附檔名2','附檔名3',...) EX 複製程式
JFileChooser fileChooser = new JFileChooser();
//個人比較習關用JFileChooser
//不過您使用的FileDialog能不能支援還要查一下@@
fileChooser.addChoosableFileFilter(
new FileNameExtensionFilter("C/C++ source code(*.c *.cc *.cpp *.h)", "c", "cc", "cpp", "h"));
fileChooser.addChoosableFileFilter(
new FileNameExtensionFilter("Java source code(*.java)", "java"));
所以以您的情況來講 save 那個方法應該改成...試試看 複製程式
public void saveImage1(){
FileDialog dlg = new FileDialog(this, "Choose Image", FileDialog.SAVE);//選擇檔案
//set current directory
if(dir != null){
dlg.setDirectory(dir);
}
FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & GIF Images", "jpg", "gif");
dlg.setFilenameFilter(filter); //這邊之後的chooser應該改成你宣告的FileDialog, 也就是dlg
int returnVal = dlg.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
dlg.getSelectedFile().getName());
}
}
還有就是開發視窗程式最好盡可能統一都用javax.swing底下的類別 比較不用顧慮太多低階的問題...也比較美觀點 不過相對的效能會稍稍的...降低些 補充一下~show那邊的parent您也沒宣告 通常這種對話框會以前一層的容器為父 所以parent建議可以改成this 指向open3的實體 剛剛又發現...FileDialog是沒有showOpenDialog這個方法的... 整篇下來問題蠻多的...於事就幫你值街都換成JFileChooser 複製程式
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
public class open3 extends JFrame implements ActionListener{
private Image m = null;
private Image m2 = null;
private File dir = null; //*
JPanel contentPane;
JScrollPane jScrollPane1 = new JScrollPane();
JScrollPane jScrollPane2 = new JScrollPane();
//static FileDialog dialog; //宣告檔案對話方塊變數
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
MenuBar menubar1; //宣告功能表列變數
Menu mnuFile; //宣告功能表變數
MenuItem itmOpen1, itmExit, itmSave; //宣告功能項變數
java.awt.Image iconimg1;
java.awt.Image iconimg2;
//Construct the frame
public open3() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(null);
this.setSize(new Dimension(700, 500)); //JFrame大小
this.setTitle("open");
jScrollPane1.setBounds(new Rectangle(30, 30, 300, 240)); //內框位置+大小
contentPane.add(jScrollPane1, null);
jScrollPane1.getViewport().add(jLabel1, null);
jScrollPane2.setBounds(new Rectangle(350, 30, 300, 240)); //內框位置+大小
contentPane.add(jScrollPane2, null);
jScrollPane2.getViewport().add(jLabel2, null);
menubar1 = new MenuBar(); //建立功能表列
mnuFile = new Menu("File"); //建立檔案功能表
menubar1.add(mnuFile); //加入檔案功能表
itmOpen1 = new MenuItem("開啟檔案"); //建立開啟舊檔功能項
itmSave = new MenuItem("Save"); //建立儲存檔案功能項
itmExit = new MenuItem("EXIT"); //建立結束功能項
mnuFile.add(itmOpen1); //加入開啟舊檔到檔案功能表
mnuFile.add(itmSave); //加入存檔到檔案功能表
mnuFile.add(itmExit); //加入結束到檔案功能表
//dialog = new FileDialog(this, "開啟舊檔"); //建立對話方塊
itmOpen1.addActionListener(this);
//dialog = new FileDialog(this, "另存新檔"); //建立對話方塊
itmSave.addActionListener(this);
itmExit.addActionListener(this);//加入結束功能項動作監聽
setMenuBar(menubar1); //設定視窗功能表列
}
//-----------------------------------------
public void actionPerformed(ActionEvent e) { //執行視窗Action事件方法
if(e.getSource() == itmOpen1){
//dialog.setVisible(true);//顯示對話方塊
loadImage1();
}
else if(e.getSource() == itmSave){
//dialog.setVisible(true);//顯示對話方塊
saveImage1();
}
else if(e.getSource() == itmExit) { //若為結束功能項事件
System.exit(0); //結束並關閉視窗
}
}
public void loadImage1(){
JFileChooser jfc = new JFileChooser();//選擇檔案
jfc.setDialogTitle("Choose Image");
//set current directory
if(dir != null){
jfc.setCurrentDirectory(dir);
}
FileNameExtensionFilter filter =
new FileNameExtensionFilter("JPG & GIF Images", "jpg", "gif");
jfc.addChoosableFileFilter(filter);
int returnVal = jfc.showOpenDialog(this);
//get image name and path
if(returnVal == JFileChooser.APPROVE_OPTION) {
String imgFile = jfc.getSelectedFile().getName();
dir = jfc.getCurrentDirectory();
//create image using filename
Toolkit tk = Toolkit.getDefaultToolkit();
m = tk.getImage(imgFile);
ImageIcon tempIcon1 = new ImageIcon(m);
iconimg1=tempIcon1.getImage();
jLabel1.setIcon(tempIcon1);
m2 = tk.getImage(imgFile);
ImageIcon tempIcon2 = new ImageIcon(m2);
iconimg2 = tempIcon2.getImage();
jLabel2.setIcon(tempIcon2);
}
}
public void saveImage1(){
JFileChooser jfc = new JFileChooser();//選擇檔案
jfc.setDialogTitle("Choose Image");
//set current directory
if(dir != null){
jfc.setCurrentDirectory(dir);
}
FileNameExtensionFilter filter =
new FileNameExtensionFilter("JPG & GIF Images", "jpg", "gif");
jfc.addChoosableFileFilter(filter);
int returnVal = jfc.showSaveDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
jfc.getSelectedFile().getName());
}
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
open3 _frame = new open3();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their layout
_frame.validate();
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = _frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
_frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
_frame.setVisible(true);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
[ 此文章被overing在2008-09-24 21:21重新編輯 ]
|
Zippy G2 600W Intel C2Q Q9550 Gigabyte GA-X48-DQ6 Gigabyte GV-NX96T512H-B G-skill pi 1100MHz 2Gx4 ACARD ANS-9010 32GB (Windows7 10sec boot!!) WD 150GB(WD1500ADFD)/640GB(6400AASK)
|
x0
[3 樓]
From:未知地址 | Posted:2008-09-24 20:21 |
|
|
overing
|
分享:
▲
▼
複製程式
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
public class Open3 extends JFrame implements ActionListener{
private Image m = null;
private Image m2 = null;
private File dir = null; //*
JPanel contentPane;
JScrollPane jScrollPane1 = new JScrollPane();
JScrollPane jScrollPane2 = new JScrollPane();
//static FileDialog dialog; //宣告檔案對話方塊變數
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
MenuBar menubar1; //宣告功能表列變數
Menu mnuFile; //宣告功能表變數
MenuItem itmOpen1, itmExit, itmSave; //宣告功能項變數
java.awt.Image iconimg1;
java.awt.Image iconimg2;
//Construct the frame
public Open3() {
jbInit();
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//Component initialization
private void jbInit() {
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(null);
this.setSize(new Dimension(700, 500)); //JFrame大小
this.setTitle("image view");
jScrollPane1.setBounds(new Rectangle(30, 30, 300, 240)); //內框位置+大小
contentPane.add(jScrollPane1, null);
jScrollPane1.getViewport().add(jLabel1, null);
jScrollPane2.setBounds(new Rectangle(350, 30, 300, 240)); //內框位置+大小
contentPane.add(jScrollPane2, null);
jScrollPane2.getViewport().add(jLabel2, null);
menubar1 = new MenuBar(); //建立功能表列
mnuFile = new Menu("檔案"); //建立檔案功能表
menubar1.add(mnuFile); //加入檔案功能表
itmOpen1 = new MenuItem("開啟檔案"); //建立開啟舊檔功能項
itmSave = new MenuItem("儲存檔案"); //建立儲存檔案功能項
itmExit = new MenuItem("結束"); //建立結束功能項
mnuFile.add(itmOpen1); //加入開啟舊檔到檔案功能表
mnuFile.add(itmSave); //加入存檔到檔案功能表
mnuFile.add(itmExit); //加入結束到檔案功能表
itmOpen1.addActionListener(this);
itmSave.addActionListener(this);
itmExit.addActionListener(this);//加入結束功能項動作監聽
setMenuBar(menubar1); //設定視窗功能表列
}
public void actionPerformed(ActionEvent e) { //執行視窗Action事件方法
if(e.getSource() == itmOpen1){
loadImage1();
}
else if(e.getSource() == itmSave){
saveImage1();
}
else if(e.getSource() == itmExit) { //若為結束功能項事件
System.exit(0); //結束並關閉視窗
}
}
public void loadImage1(){
JFileChooser jfc = new JFileChooser();//選擇檔案
jfc.setDialogTitle("Choose Image");
//set current directory
if(dir != null){
jfc.setCurrentDirectory(dir);
}
FileNameExtensionFilter filter =
new FileNameExtensionFilter("JPG & GIF Images", "jpg", "gif");
jfc.addChoosableFileFilter(filter);
int returnVal = jfc.showOpenDialog(this);
//get image name and path
if(returnVal == JFileChooser.APPROVE_OPTION) {
String imgFile = jfc.getCurrentDirectory() + "\\" + jfc.getSelectedFile().getName();
dir = jfc.getCurrentDirectory();
//create image using filename
Toolkit tk = Toolkit.getDefaultToolkit();
m = tk.getImage(imgFile);
ImageIcon tempIcon1 = new ImageIcon(imgFile);
iconimg1=tempIcon1.getImage();
jLabel1.setIcon(tempIcon1);
m2 = tk.getImage(imgFile);
ImageIcon tempIcon2 = new ImageIcon(imgFile);
iconimg2 = tempIcon2.getImage();
jLabel2.setIcon(tempIcon2);
jLabel1.updateUI();
jLabel2.updateUI();
}
}
public void saveImage1(){
JFileChooser jfc = new JFileChooser();//選擇檔案
jfc.setDialogTitle("Choose Image");
//set current directory
if(dir != null){
jfc.setCurrentDirectory(dir);
}
FileNameExtensionFilter filter =
new FileNameExtensionFilter("JPG & GIF Images", "jpg", "gif");
jfc.addChoosableFileFilter(filter);
int returnVal = jfc.showSaveDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
jfc.getSelectedFile().getName());
}
}
public static void main(String[] args) {
new Open3().setVisible(true);
}
}
|
Zippy G2 600W Intel C2Q Q9550 Gigabyte GA-X48-DQ6 Gigabyte GV-NX96T512H-B G-skill pi 1100MHz 2Gx4 ACARD ANS-9010 32GB (Windows7 10sec boot!!) WD 150GB(WD1500ADFD)/640GB(6400AASK)
|
x0
[4 樓]
From:未知地址 | Posted:2008-09-24 21:54 |
|
|
overing
|
分享:
▲
▼
初步搞定版... Java的內建套件真的是非常的齊全 不過因為你只有用JAP跟GIF兩種格式 所以有些判斷試寫得很糟糕 你先看看~有空再上註解 複製程式
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.*;
import javax.imageio.stream.ImageOutputStream;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
public class Open3 extends JFrame implements ActionListener{
private BufferedImage m = null; //編輯中的緩衝影像(原始圖樣用做對照)
private BufferedImage m2 = null; //編輯中的緩衝影像(未來可能為套用過效果的)
private File dir = null; //以開啟的檔案路徑
JPanel contentPane;
JScrollPane jScrollPane1 = new JScrollPane();
JScrollPane jScrollPane2 = new JScrollPane();
//static FileDialog dialog; //宣告檔案對話方塊變數
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
MenuBar menubar1; //宣告功能表列變數
Menu mnuFile; //宣告功能表變數
MenuItem itmOpen1, itmExit, itmSave; //宣告功能項變數
//Construct the frame
public Open3() {
jbInit();
this.setLocationRelativeTo(null); //視窗置中
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //如果點[X]就視同關閉程式
}
//Component initialization
private void jbInit() {
contentPane = (JPanel) this.getContentPane(); //取得Fram的內容層
contentPane.setLayout(null); //設定佈局為NULL
this.setSize(new Dimension(700, 500)); //JFrame大小
this.setTitle("image view"); //視窗標題
jScrollPane1.setBounds(new Rectangle(30, 30, 300, 240)); //內框位置+大小
contentPane.add(jScrollPane1, null);
jScrollPane1.getViewport().add(jLabel1, null);
jScrollPane2.setBounds(new Rectangle(350, 30, 300, 240)); //內框位置+大小
contentPane.add(jScrollPane2, null);
jScrollPane2.getViewport().add(jLabel2, null);
menubar1 = new MenuBar(); //建立功能表列
mnuFile = new Menu("檔案"); //建立檔案功能表
menubar1.add(mnuFile); //加入檔案功能表
itmOpen1 = new MenuItem("開啟檔案"); //建立開啟舊檔功能項
itmSave = new MenuItem("儲存檔案"); //建立儲存檔案功能項
itmExit = new MenuItem("結束"); //建立結束功能項
mnuFile.add(itmOpen1); //加入開啟舊檔到檔案功能表
mnuFile.add(itmSave); //加入存檔到檔案功能表
mnuFile.add(itmExit); //加入結束到檔案功能表
itmOpen1.addActionListener(this);
itmSave.addActionListener(this);
itmExit.addActionListener(this);//加入結束功能項動作監聽
setMenuBar(menubar1); //設定視窗功能表列
}
public void actionPerformed(ActionEvent e) { //執行視窗Action事件方法
if(e.getSource() == itmOpen1){
loadImage1();
}
else if(e.getSource() == itmSave){
saveImage1();
}
else if(e.getSource() == itmExit) { //若為結束功能項事件
System.exit(0); //結束並關閉視窗
}
}
public void loadImage1(){
JFileChooser jfc = new JFileChooser(); //檔案選擇對話框
jfc.setDialogTitle("開啟檔案"); //設定對話框標題
if(dir != null){
jfc.setCurrentDirectory(dir); //如果有路徑記錄就從路徑位置
}
jfc.addChoosableFileFilter(new FileNameExtensionFilter("JPG & GIF Images", "jpg", "gif")); //加入檔名過慮器
int returnVal = jfc.showOpenDialog(this); //傳回值
//get image name and path
if(returnVal == JFileChooser.APPROVE_OPTION) { //如果對話視窗是按了[OK]或[確定]之類的按鈕結束
try {
String imgFile = jfc.getCurrentDirectory() + "\\" + jfc.getSelectedFile().getName(); //檔案完整路徑
dir = jfc.getCurrentDirectory(); //儲存路徑記錄
this.m = ImageIO.read(jfc.getSelectedFile()); //讀入影像
ImageIcon tempIcon1 = new ImageIcon(imgFile);
jLabel1.setIcon(tempIcon1);
this.m2 = ImageIO.read(jfc.getSelectedFile()); //讀入影像
ImageIcon tempIcon2 = new ImageIcon(imgFile);
jLabel2.setIcon(tempIcon2);
jLabel1.updateUI(); //更新畫面
jLabel2.updateUI(); //更新畫面
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public void saveImage1(){
JFileChooser jfc = new JFileChooser(); //選擇檔案
jfc.setDialogTitle("儲存檔案"); //設定對話框標題
//set current directory
if(dir != null){
jfc.setCurrentDirectory(dir); //儲存路徑記錄
}
//存檔的時候要講名是那種檔案
jfc.addChoosableFileFilter(new FileNameExtensionFilter("jpeg file", "jpg")); //加入檔名過慮器
jfc.addChoosableFileFilter(new FileNameExtensionFilter("gif file", "gif")); //加入檔名過濾器
int returnVal = jfc.showSaveDialog(this); //儲存傳回值
if(returnVal == JFileChooser.APPROVE_OPTION) { //如果對話視窗是按了[OK]或[確定]之類的按鈕結束
String fileType = jfc.getFileFilter().getDescription(); //檔案類型
fileType = (fileType.equals("jpeg file") ? fileType = "jpg" : "gif"); //轉成3字元附檔名
Iterator writers = ImageIO.getImageWritersBySuffix(fileType); //依據檔案類型取得圖形寫入器
ImageWriter writer = (ImageWriter) writers.next();
String fullFileName = jfc.getCurrentDirectory() + "\\" + jfc.getSelectedFile().getName(); //完整檔名
if(!fullFileName.endsWith(fileType)) fullFileName = fullFileName + "." + fileType; //如果使用者沒有鍵入付檔名則自動補上
File f = new File(fullFileName); //包裝成檔案物件
try {
ImageOutputStream ios = ImageIO.createImageOutputStream(f); //建立圖形輸出串流
writer.setOutput(ios); //設定圖形輸出串流
writer.write(this.m2); //將圖形寫入
ios.close(); //關閉檔案串流
//System.out.println("You chose to open this file: " + jfc.getSelectedFile().getName());
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
new Open3().setVisible(true);
}
}
[ 此文章被overing在2008-09-25 12:29重新編輯 ]
|
Zippy G2 600W Intel C2Q Q9550 Gigabyte GA-X48-DQ6 Gigabyte GV-NX96T512H-B G-skill pi 1100MHz 2Gx4 ACARD ANS-9010 32GB (Windows7 10sec boot!!) WD 150GB(WD1500ADFD)/640GB(6400AASK)
|
x0
[7 樓]
From:未知地址 | Posted:2008-09-25 01:28 |
|
|
overing
|
分享:
▲
下面是引用芽可於2008-09-25 20:16發表的 : 真的可以了QQ 嗚嗚,我該怎麼感謝你QQ? 報答就不用了... 繼續玩吧!~XD 下面這個自己加了一些新功能~ 用NetBeans來設計GUI會比較方便喔!~ 像38~235行就都是NetBeans幫忙建立的(自己不用打) (一些英文註解是NettBeans加的~可以拿掉無訪) 玩玩看 複製程式
/*
* MainJFrame.java
*
* Created on 2008年9月25日, 下午 4:57
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
/**
*
* @author Overing Zero
*/
public class MainJFrame extends JFrame implements AdjustmentListener {
private BufferedImage originImage = null; //編輯中的緩衝影像(原始圖樣用做對照)
private BufferedImage m2 = null; //編輯中的緩衝影像(未來可能為套用過效果的)
private File dir = null; //以開啟的檔案路徑
/** Creates new form MainJFrame */
public MainJFrame() {
initComponents();
this.setLocationRelativeTo(null); //視窗置中
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
jLabel3 = new javax.swing.JLabel();
before_JScrollPane = new javax.swing.JScrollPane();
beforeImage_JLabel = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
after_JScrollPane = new javax.swing.JScrollPane();
afterImage_JLabel = new javax.swing.JLabel();
jPanel1 = new javax.swing.JPanel();
addBlack_JButton = new javax.swing.JButton();
addW_JButton = new javax.swing.JButton();
addRed_JButton = new javax.swing.JButton();
addGreen_JButton = new javax.swing.JButton();
addBlue_JButton = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
save_JMenuItem = new javax.swing.JMenuItem();
load_JMenuItem = new javax.swing.JMenuItem();
jSeparator1 = new javax.swing.JSeparator();
exit_JMenuItem = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenuItem2 = new javax.swing.JMenuItem();
jSeparator2 = new javax.swing.JSeparator();
jMenu4 = new javax.swing.JMenu();
jMenu3 = new javax.swing.JMenu();
about_JMenuItem = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Image Editer");
setMinimumSize(new java.awt.Dimension(400, 300));
getContentPane().setLayout(new java.awt.GridBagLayout());
jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel3.setText("Before");
jLabel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.weightx = 1.0;
getContentPane().add(jLabel3, gridBagConstraints);
before_JScrollPane.getHorizontalScrollBar().addAdjustmentListener(new AdjustmentListener(){
public void adjustmentValueChanged(AdjustmentEvent e) {
MainJFrame.this.after_JScrollPane.getHorizontalScrollBar().setValue(e.getValue());
}
});
before_JScrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener(){
public void adjustmentValueChanged(AdjustmentEvent e) {
MainJFrame.this.after_JScrollPane.getVerticalScrollBar().setValue(e.getValue());
}
});
before_JScrollPane.setViewportView(beforeImage_JLabel);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.gridheight = java.awt.GridBagConstraints.RELATIVE;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
getContentPane().add(before_JScrollPane, gridBagConstraints);
jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel4.setText("After");
jLabel4.setBorder(javax.swing.BorderFactory.createEtchedBorder());
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.weightx = 1.0;
getContentPane().add(jLabel4, gridBagConstraints);
after_JScrollPane.getHorizontalScrollBar().addAdjustmentListener(this);
after_JScrollPane.getVerticalScrollBar().addAdjustmentListener(this);
after_JScrollPane.setViewportView(afterImage_JLabel);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 1;
gridBagConstraints.gridheight = java.awt.GridBagConstraints.RELATIVE;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
getContentPane().add(after_JScrollPane, gridBagConstraints);
jPanel1.setLayout(new java.awt.GridLayout(1, 5, 2, 2));
addBlack_JButton.setText("黑");
addBlack_JButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
addBlack_JButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
toolButtonsActionPerformed(evt);
}
});
jPanel1.add(addBlack_JButton);
addW_JButton.setText("白");
addW_JButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
toolButtonsActionPerformed(evt);
}
});
jPanel1.add(addW_JButton);
addRed_JButton.setText("紅");
addRed_JButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
toolButtonsActionPerformed(evt);
}
});
jPanel1.add(addRed_JButton);
addGreen_JButton.setText("綠");
addGreen_JButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
toolButtonsActionPerformed(evt);
}
});
jPanel1.add(addGreen_JButton);
addBlue_JButton.setText("藍");
addBlue_JButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
toolButtonsActionPerformed(evt);
}
});
jPanel1.add(addBlue_JButton);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
getContentPane().add(jPanel1, gridBagConstraints);
jMenu1.setText("檔案");
save_JMenuItem.setText("開啟檔案");
save_JMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
open_JMenuItemActionPerformed(evt);
}
});
jMenu1.add(save_JMenuItem);
load_JMenuItem.setText("儲存檔案");
load_JMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
save_JMenuItemActionPerformed(evt);
}
});
jMenu1.add(load_JMenuItem);
jMenu1.add(jSeparator1);
exit_JMenuItem.setText("離開");
exit_JMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exit_JMenuItemActionPerformed(evt);
}
});
jMenu1.add(exit_JMenuItem);
jMenuBar1.add(jMenu1);
jMenu2.setText("編輯");
jMenuItem1.setText("Undo");
jMenu2.add(jMenuItem1);
jMenuItem2.setText("Redo");
jMenu2.add(jMenuItem2);
jMenu2.add(jSeparator2);
jMenu4.setText("ActionLog");
jMenu2.add(jMenu4);
jMenuBar1.add(jMenu2);
jMenu3.setText("關於");
about_JMenuItem.setText("關於本程式");
about_JMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
about_JMenuItemActionPerformed(evt);
}
});
jMenu3.add(about_JMenuItem);
jMenuBar1.add(jMenu3);
setJMenuBar(jMenuBar1);
pack();
}// </editor-fold>
private void open_JMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser jfc = new JFileChooser(); //檔案選擇對話框
jfc.setDialogTitle("開啟檔案"); //設定對話框標題
if(dir != null) jfc.setCurrentDirectory(dir); //如果有路徑記錄就從路徑位置
jfc.addChoosableFileFilter(new FileNameExtensionFilter("JPG & GIF Images", "jpg", "gif")); //加入檔名過慮器
int returnVal = jfc.showOpenDialog(this); //傳回值
//get image name and path
if(returnVal == JFileChooser.APPROVE_OPTION) { //如果對話視窗是按了[OK]或[確定]之類的按鈕結束
try {
String imgFile = jfc.getCurrentDirectory() + "\\" + jfc.getSelectedFile().getName(); //檔案完整路徑
dir = jfc.getCurrentDirectory(); //儲存路徑記錄
this.originImage = ImageIO.read(jfc.getSelectedFile()); //讀入影像
ImageIcon tempIcon1 = new ImageIcon(this.originImage);
this.beforeImage_JLabel.setIcon(tempIcon1);
this.m2 = ImageIO.read(jfc.getSelectedFile()); //讀入影像
ImageIcon tempIcon2 = new ImageIcon(this.m2);
this.afterImage_JLabel.setIcon(tempIcon2);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
private void save_JMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser jfc = new JFileChooser(); //選擇檔案
jfc.setDialogTitle("儲存檔案"); //設定對話框標題
if(dir != null) jfc.setCurrentDirectory(dir); //如果有路徑記錄就從路徑位置
//存檔的時候要講明那種檔案
jfc.addChoosableFileFilter(new FileNameExtensionFilter("jpeg file", "jpg")); //加入檔名過慮器
jfc.addChoosableFileFilter(new FileNameExtensionFilter("gif file", "gif")); //加入檔名過濾器
int returnVal = jfc.showSaveDialog(this); //儲存傳回值
if(returnVal == JFileChooser.APPROVE_OPTION) { //如果對話視窗是按了[OK]或[確定]之類的按鈕結束
String fileType = jfc.getFileFilter().getDescription(); //檔案類型
fileType = (fileType.equals("jpeg file") ? fileType = "jpg" : "gif"); //轉成3字元附檔名
String fullFileName = jfc.getCurrentDirectory() + "\\" + jfc.getSelectedFile().getName(); //完整檔名
if(!fullFileName.endsWith(fileType)) fullFileName = fullFileName + "." + fileType; //如果使用者沒有鍵入付檔名則自動補上
File f = new File(fullFileName); //包裝成檔案物件
try {
ImageIO.write(m2, fileType, f);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
private void exit_JMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
public void adjustmentValueChanged(AdjustmentEvent e) {
if(e.getSource() == this.before_JScrollPane.getHorizontalScrollBar())
this.after_JScrollPane.getHorizontalScrollBar().setValue(
this.before_JScrollPane.getHorizontalScrollBar().getValue());
if(e.getSource() == this.before_JScrollPane.getVerticalScrollBar())
this.after_JScrollPane.getVerticalScrollBar().setValue(
this.before_JScrollPane.getVerticalScrollBar().getValue());
}
private void toolButtonsActionPerformed(java.awt.event.ActionEvent evt) {
if(m2 == null) return;
Graphics g = this.m2.getGraphics();
if(evt.getSource() == this.addBlack_JButton)
g.setColor(new Color(0, 0, 0, 16));
if(evt.getSource() == this.addW_JButton)
g.setColor(new Color(255, 255, 255, 16));
if(evt.getSource() == this.addRed_JButton)
g.setColor(new Color(255, 0, 0, 16));
if(evt.getSource() == this.addGreen_JButton)
g.setColor(new Color(0, 255, 0, 16));
if(evt.getSource() == this.addBlue_JButton)
g.setColor(new Color(0, 0, 255, 16));
g.fillRect(0, 0,m2.getWidth(), m2.getHeight());
afterImage_JLabel.setIcon(new ImageIcon(this.m2));
}
private void about_JMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
String msg = "名稱: Image Editer\n" +
"版本: 0.9" +
"作者: Overing\n" +
"時間: 08/09/24~08/09/25\n" +
"臭蟲回報:\n" +
" zero_overing@yahoo.com.tw\n" +
" zero_overing@hotmail.com";
JOptionPane.showMessageDialog(this, msg, "關於本程式", JOptionPane.INFORMATION_MESSAGE);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JMenuItem about_JMenuItem;
private javax.swing.JButton addBlack_JButton;
private javax.swing.JButton addBlue_JButton;
private javax.swing.JButton addGreen_JButton;
private javax.swing.JButton addRed_JButton;
private javax.swing.JButton addW_JButton;
private javax.swing.JLabel afterImage_JLabel;
private javax.swing.JScrollPane after_JScrollPane;
private javax.swing.JLabel beforeImage_JLabel;
private javax.swing.JScrollPane before_JScrollPane;
private javax.swing.JMenuItem exit_JMenuItem;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenu jMenu4;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JPanel jPanel1;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JSeparator jSeparator2;
private javax.swing.JMenuItem load_JMenuItem;
private javax.swing.JMenuItem save_JMenuItem;
// End of variables declaration
}
[ 此文章被overing在2008-09-25 22:16重新編輯 ]
此文章被評分,最近評分記錄財富:100 (by 三仙) | 理由: ^^ 因為您的參與,讓程式設計更容易!! | |
|
|
|
Zippy G2 600W Intel C2Q Q9550 Gigabyte GA-X48-DQ6 Gigabyte GV-NX96T512H-B G-skill pi 1100MHz 2Gx4 ACARD ANS-9010 32GB (Windows7 10sec boot!!) WD 150GB(WD1500ADFD)/640GB(6400AASK)
|
x1
[9 樓]
From:未知地址 | Posted:2008-09-25 21:27 |
|
|
|