2011年5月6日 星期五

2011/05/06上課內容

上次的作業答案:
原本程式碼為
import javax.swing.Timer;
import java.awt.event.*;
import java.util.*;
public class TimerDemo implements ActionListener {
  Timer t = new Timer(1000,this);
  TimerDemo() {
    t.start();
    }
  public static void main(String args[]) {
    TimerDemo td = new TimerDemo();
    // create a dummy frame to keep the JVM running
    //  (for demonstation purpose)
    java.awt.Frame dummy = new java.awt.Frame();
    dummy.setVisible(true);
    }
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == t) {
      System.out.println
        ("\007Being ticked " + Calendar.getInstance().getTime());
      }
    }
}
修改過後為:
import javax.swing.Timer;
import java.awt.event.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
public class TimerDemo extends JFrame implements ActionListener {
  Timer t = new Timer(1000,this);
  static JTextField tbx1=new JTextField(20); 
  TimerDemo() {
    t.start();
    }
  public static void main(String args[]) {
    TimerDemo td = new TimerDemo();
    FlowLayout flow=new FlowLayout();
    td.setLayout(flow);
    td.setSize(450,150);
    JPanel p1 = new JPanel(flow);
    td.add(p1);
    p1.add(tbx1);
    td.setVisible(true);
    }
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == t) {
      tbx1.setText("" + Calendar.getInstance().getTime());
      }
    }
}
之後下載ip-demo2.zip的壓縮檔案以及negun3276.tif
此為別人設計的具修改圖片功能的套件
作業:將裡面的IPDemo.java其中一個功能(例如雜訊)不用靠按鍵就顯示出來,
   並把所有的按鍵去除
IPDemo.java原程式碼:
import ij.*;
import java.applet.*;
import java.awt.*;
import ij.process.*;
/**Simple applet that demonstrates how to use ImageJ's ImageProcessor class.*/
public class IPDemo extends Applet {
 String name;
 Image img;
 ImageProcessor ip = null;
 public void init() {
  setLayout(new BorderLayout());
  Panel p = new Panel();
  p.setLayout(new GridLayout(5, 3));
  p.add(new Button("Reset"));
  p.add(new Button("Flip"));
  p.add(new Button("Invert"));
  p.add(new Button("Lighten"));
  p.add(new Button("Darken"));
  p.add(new Button("Rotate"));
  p.add(new Button("Zoom In"));
  p.add(new Button("Zoom Out"));
  p.add(new Button("Threshsold"));
  p.add(new Button("Smooth"));
  p.add(new Button("Sharpen"));
  p.add(new Button("Find Edges"));
  p.add(new Button("Macro 1"));
  p.add(new Button("Macro 2"));
  p.add(new Button("Add Noise"));
  add("South", p);
  name = getParameter("img");
  img = getImage(getDocumentBase(), name);
  MediaTracker tracker = new MediaTracker(this);
  tracker.addImage(img, 0);
  try {tracker.waitForID(0);}
  catch (InterruptedException e){}
  if (name.endsWith("jpg"))
   ip = new ColorProcessor(img);
  else
   ip = new ByteProcessor(img);
  ip.snapshot();
 }

 public void update(Graphics g) {
  paint(g);
 }
 public void paint(Graphics g) {
  g.drawImage(img, 0, 0, this);
 }
 public boolean action(Event e, Object arg) {
  if (e.target instanceof Button) {
   String label = (String)arg;
   if (label.equals("Reset"))
    ip.reset();
   else if (label.equals("Flip"))
    ip.flipVertical();
   else if (label.equals("Invert"))
    ip.invert();
   else if (label.equals("Lighten"))
    ip.multiply(0.9);
   else if (label.equals("Darken"))
    ip.multiply(1.1);
   else if (label.equals("Rotate"))
    ip.rotate(30);
   else if (label.equals("Zoom In"))
    ip.scale(1.2, 1.2);
   else if (label.equals("Zoom Out"))
    ip.scale(0.8, 0.8);
   else if (label.equals("Threshsold"))
    ip.autoThreshold();
   else if (label.equals("Smooth"))
    ip.smooth();
   else if (label.equals("Sharpen"))
    ip.sharpen();
   else if (label.equals("Find Edges"))
    ip.findEdges();
   else if (label.equals("Macro 1"))
    macro1();
   else if (label.equals("Macro 2"))
    macro2();
   else if (label.equals("Add Noise"))
    ip.noise(20);
   img = ip.createImage();
   repaint();
   return true;
  }
  return false;
 }

 void updateAndDraw() {
  img.flush();
  img = ip.createImage();
  getGraphics().drawImage(img, 0, 0, this);
 }
 void macro1() {
  for (int i=10; i<=360; i+=10) {
   ip.reset();
   ip.rotate(i);
   updateAndDraw();
  }
 }

 void macro2() {
  double scale = 1, m = 1.2;
  for (int i=0; i<20; i++) {
   ip.reset();
   scale *= m;
   ip.scale(scale, scale);
   updateAndDraw();
  }
  for (int i=0; i <20; i++) {
   ip.reset();
   scale /= m;
   ip.scale(scale, scale);
   updateAndDraw();
  }
 }

}
修改後程式碼:(為了讓雜訊更為明顯,將20改成100)
import ij.*;
import java.applet.*;
import java.awt.*;
import ij.process.*;
/**Simple applet that demonstrates how to use ImageJ's ImageProcessor class.*/
public class IPDemo extends Applet {
 String name;
 Image img;
 ImageProcessor ip = null;
       
 public void init() {
  setLayout(new BorderLayout());
  Panel p = new Panel();
 /* p.setLayout(new GridLayout(5, 3));
  p.add(new Button("Reset"));
  p.add(new Button("Flip"));
  p.add(new Button("Invert"));
  p.add(new Button("Lighten"));
  p.add(new Button("Darken"));
  p.add(new Button("Rotate"));
  p.add(new Button("Zoom In"));
  p.add(new Button("Zoom Out"));
  p.add(new Button("Threshsold"));
  p.add(new Button("Smooth"));
  p.add(new Button("Sharpen"));
  p.add(new Button("Find Edges"));
  p.add(new Button("Macro 1"));
  p.add(new Button("Macro 2"));
 */
  add("South", p);
  name = getParameter("img");
  img = getImage(getDocumentBase(), name);
  MediaTracker tracker = new MediaTracker(this);
  tracker.addImage(img, 0);
  try {tracker.waitForID(0);}
  catch (InterruptedException e){}
  if (name.endsWith("jpg"))
   ip = new ColorProcessor(img);
  else
   ip = new ByteProcessor(img);
                        ip.noise(100);
   img = ip.createImage();
   repaint();
  ip.snapshot();
               
            
 }

 public void update(Graphics g) {
  paint(g);
 }
 public void paint(Graphics g) {
  g.drawImage(img, 0, 0, this);
 }
 public boolean action(Event e, Object arg) {
  if (e.target instanceof Button) {
   String label = (String)arg;
                          
                        if (label.equals("Reset"))
    ip.reset();
   else if (label.equals("Flip"))
    ip.flipVertical();
   else if (label.equals("Invert"))
    ip.invert();
   else if (label.equals("Lighten"))
    ip.multiply(1.1);
   else if (label.equals("Darken"))
    ip.multiply(0.9);
   else if (label.equals("Rotate"))
    ip.rotate(30);
   else if (label.equals("Zoom In"))
    ip.scale(1.2, 1.2);
   else if (label.equals("Zoom Out"))
    ip.scale(0.8, 0.8);
   else if (label.equals("Threshsold"))
    ip.autoThreshold();
   else if (label.equals("Smooth"))
    ip.smooth();
   else if (label.equals("Sharpen"))
    ip.sharpen();
   else if (label.equals("Find Edges"))
    ip.findEdges();
   else if (label.equals("Macro 1"))
    macro1();
   else if (label.equals("Macro 2"))
    macro2();
   
   img = ip.createImage();
   repaint();
   return true;
  }
  return false;
 }

 void updateAndDraw() {
       img.flush();
  img = ip.createImage();
  getGraphics().drawImage(img, 0, 0, this);
 }
 void macro1() {
  for (int i=10; i<=360; i+=10) {
   ip.reset();
   ip.rotate(i);
   updateAndDraw();
  }
 }

 void macro2() {
  double scale = 1, m = 1.2;
  for (int i=0; i<20; i++) {
   ip.reset();
   scale *= m;
   ip.scale(scale, scale);
   updateAndDraw();
  }
  for (int i=0; i <20; i++) {
   ip.reset();
   scale /= m;
   ip.scale(scale, scale);
   updateAndDraw();
  }
 }
}

2011年4月29日 星期五

2011/04/29上課內容

這次上課首先教導何謂「封裝」,首先開啟一範例程式如下:
並將程式碼分成以下兩個檔
程式碼裡面有main才會執行,以下是執行結果
本次作業:將以下範例程式的顯示時間由顯示在命令提示字元改成顯示在文字方塊上

2011年4月22日 星期五

2011/04/22上課內容

開啟一範例程式如下:
// with event
//Swing, JButton類別 有ActionListener
//Swing, JButton類別
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingTestEvent extends JFrame implements ActionListener
{
//static JFrame myfrm=new JFrame("JButton class"); // Java Class JFrame
//static AwtTestEvent myfrm=new AwtTestEvent("JFrame 1 "); // Java Class JFrame
static JTextField tbx1=new JTextField(2); // 建立1文字方塊物件
static JButton buttons[]=new JButton[10];
static JLabel  labels[]=new JLabel [10];
public static void main(String args[])
{
SwingTestEvent myfrm=new SwingTestEvent();
String numbers[]  = {"0", "1", "2", "3", "4", "5", "6", "7", "8"};
FlowLayout flow=new FlowLayout();
GridLayout grid12= new GridLayout(1,2);
GridLayout grid33= new GridLayout(3,3);
myfrm.setLayout(grid12);
myfrm.setSize(450,450);
JPanel p1 = new JPanel(grid33); //實作  panel 1
for (int i = 0; i < numbers.length; i++)
{
buttons[i] = new JButton(numbers[i]); // create buttons
p1.add(buttons[i], grid33); // 在 panel 1內加入按鈕陣列
}
myfrm.add(p1); // 在視窗myfrm 內加入 panel 1
JPanel p3 = new JPanel(grid33); //實作  panel 3
for (int i = 0; i < numbers.length; i++)
{
labels[i] = new JLabel(); // create labels
p3.add(labels[i], grid33); // 在 panel 1內加入按鈕陣列
}
myfrm.add(p3); // 在視窗myfrm 內加入 panel 3
JPanel p2 = new JPanel(flow); //實作  panel 2
JButton btn1=new JButton("JButton 1"); // 建立按鈕物件 btn1
btn1.addActionListener(myfrm);
p2.add(tbx1); // 在 panel 2內加入文字方塊
p2.add(btn1); // 在 panel 2內加入按鈕
myfrm.add(p2); // 在視窗myfrm 內加入 panel 2

myfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String stringValue;
stringValue=tbx1.getText();
int intValue = Integer.parseInt(stringValue);
System.out.println(intValue);
buttons[intValue].setBackground(Color.blue);
labels[intValue].setText(stringValue);
}
}
--------------------------------------------------------
此程式碼的執行結果為
在文字方塊輸入一數字
並按下旁邊的按鈕
會在左邊九個按鈕中
將所對應數字的按鈕變成藍色

接下來將程式碼改成如下圖:
此執行結果為
按按鈕時在文字方塊產生一亂數並且在對應的按鈕變成藍色
此為反覆按完的結果

接下來將程式碼改成如下圖:
執行結果為按按鈕之後以亂數在第一個按鈕變更標籤
接下來按按鈕之後以亂數將3乘3按鈕變更標籤並且按鈕的數字不重複
作業:將3乘3改成5乘5 程式碼:
// with event
//Swing, JButton類別 有ActionListener
//Swing, JButton類別
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingTestEvent extends JFrame implements ActionListener
{
//static JFrame myfrm=new JFrame("JButton class"); // Java Class JFrame
//static AwtTestEvent myfrm=new AwtTestEvent("JFrame 1 "); // Java Class JFrame
static JTextField tbx1=new JTextField(2); // 建立1文字方塊物件
static JButton buttons[]=new JButton[25];
static JLabel  labels[]=new JLabel [25];
public static void main(String args[])
{
SwingTestEvent myfrm=new SwingTestEvent();
String numbers[]  = {"1", "2", "3", "4", "5", "6", "7", "8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25"};
FlowLayout flow=new FlowLayout();
GridLayout grid12= new GridLayout(1,2);
GridLayout grid33= new GridLayout(5,5);
myfrm.setLayout(grid12);
myfrm.setSize(700,700);
JPanel p1 = new JPanel(grid33); //實作  panel 1
for (int i = 0; i < numbers.length; i++)
{
buttons[i] = new JButton(numbers[i]); // create buttons
p1.add(buttons[i], grid33); // 在 panel 1內加入按鈕陣列
}
myfrm.add(p1); // 在視窗myfrm 內加入 panel 1
/*
JPanel p3 = new JPanel(grid33); //實作  panel 3
for (int i = 0; i < numbers.length; i++)
{
labels[i] = new JLabel(); // create labels
p3.add(labels[i], grid33); // 在 panel 1內加入按鈕陣列
}
myfrm.add(p3); // 在視窗myfrm 內加入 panel 3
*/
JPanel p2 = new JPanel(flow); //實作  panel 2
JButton btn1=new JButton("JButton 1"); // 建立按鈕物件 btn1
btn1.addActionListener(myfrm);
p2.add(tbx1); // 在 panel 2內加入文字方塊
p2.add(btn1); // 在 panel 2內加入按鈕
myfrm.add(p2); // 在視窗myfrm 內加入 panel 2

myfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String numbers[]  = {"1", "2", "3", "4", "5", "6", "7", "8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25"};
String stringValue,tmp;
int j,rndno;
for (int i = 0; i < 25; i++)
{
j=24-i;
rndno=(int)(Math.random()*(j+1));
//System.out.println(rndno);
tmp=numbers[j];
numbers[j]=numbers[rndno];
numbers[rndno]=tmp;
}
for (int i = 0; i < 25; i++)
{
System.out.println(numbers[i]);
buttons[i].setLabel(numbers[i]);
}
//tbx1.setText(numbers[0]);
//buttons[rndno].setBackground(Color.blue);
//labels[rndno].setText(numbers[0]);
//buttons[intValue].setBackground(Color.blue);
//labels[intValue].setText(stringValue);
}
}

2011年3月25日 星期五

2011/03/25上課內容

http://tccnchsu.blogspot.com/  程式設計工藝大師

本次上課介紹使用AWT按鈕的範例


建立五個按鈕物件,並在視窗內加入按鈕
執行結果是五個按鈕疊在一起
因為還沒有排列按鈕的位置

首先,利用AWT資料夾裡面的BorderLayout的排序法
此為編排的結果

接下來建立文字方塊物件

建立兩個按鈕,並在中間建立文字方塊物件
替Button 1加入監聽事件
按Button 1會顯示49號碼取一的隨機變數

接下來要讓顯示再命令提示字元的數字改成顯示在文字方塊物件上
但是顯示的方式只能用字串才能執行
所以要將整數轉為字串

在此程式只有按Button 1有動作
顯示結果如下

作業:
建立一個按鈕以及七個文字方塊物件,再按下按鈕後,七個文字方塊能顯示出七個不同的
1~49隨機數字(大樂透)

程式碼:
import java.awt.*;
import java.awt.event.*;
public class AwtTest extends Frame implements ActionListener
{
static Frame myfrm=new Frame("Button class");
static Button btn=new Button("Button");
static TextField tf1=new TextField("TextField");
static TextField tf2=new TextField("TextField");
static TextField tf3=new TextField("TextField");
static TextField tf4=new TextField("TextField");
static TextField tf5=new TextField("TextField");
static TextField tf6=new TextField("TextField");
static TextField tf7=new TextField("TextField");
public static void main(String args[])
{
AwtTest myfrm=new AwtTest();
FlowLayout flow=new FlowLayout();
myfrm.setLayout(flow);
myfrm.setSize(200,150);
myfrm.add(btn,flow);
myfrm.add(tf1,flow);
myfrm.add(tf2,flow);
myfrm.add(tf3,flow);
myfrm.add(tf4,flow);
myfrm.add(tf5,flow);
myfrm.add(tf6,flow);
myfrm.add(tf7,flow);
btn.addActionListener(myfrm);
myfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
int a,b,c,d,f,g,h;
a=(int)(Math.random()*49)+1;
b=(int)(Math.random()*49)+1;
c=(int)(Math.random()*49)+1;
d=(int)(Math.random()*49)+1;
f=(int)(Math.random()*49)+1;
g=(int)(Math.random()*49)+1;
h=(int)(Math.random()*49)+1;
String one= Integer.toString(a);
String two= Integer.toString(b);
String three= Integer.toString(c);
String four= Integer.toString(d);
String five= Integer.toString(f);
String six= Integer.toString(g);
String seven= Integer.toString(h);
tf1.setText(one);
tf2.setText(two);
tf3.setText(three);
tf4.setText(four);
tf5.setText(five);
tf6.setText(six);
tf7.setText(seven);
System.out.println();
}
}

執行結果:

2011年3月18日 星期五

2011/03/18 上課內容

先開啟一範例程式並執行
之後按按鈕能夠顯示出wait
增加兩個按鈕,執行結果閃一下
下載此檔
將檔案執行
先讓套件改成SWING的
執行要有兩最後那行才不會出錯
新增兩個按鈕
執行結果
新增按鈕動作
執行結果
作業:按按鈕顯示出亂數的七個號碼

2011年3月11日 星期五

2011/03/11上課內容

今天的課程主要是教我們新增Button的幾種方法
首先可以用VB來新增Button
開新專案
點選Windows Form應用程式,然後按確定
接下來就可以開始新增Button
如果沒有VB的話,可以利用Office的任一軟體來用,在此舉EXCEL為例
點選檢視,巨集→錄製巨集
按確定, 之後隨意在工作表上打一個數字
然後按巨集→停止錄製
之後按巨集→檢視巨集→編輯
輸入以下字串然後按執行(F5)
出現以下結果
以下是在JAVA建立Button的方法
以及相關部落格
作業:完成一個CheckBox
要將以下的檔案執行出來

程式碼:
執行結果: