2011年5月29日 星期日

java - Android

從上一次上課就開始教我們使用Android
也下載了很久!!

以下大概是他的開機畫面





其他還有很多地方可以玩看看....請自行發掘!!!!!

這一次寫了一個換算匯率的程式(半成品)

先在main.xml 編寫你要的東西


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_height="wrap_content" android:layout_width="wrap_content">
   
    <TextView 
    android:text="@string/str2"  
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">
    </TextView>
    
     <EditText 
    android:id="@+id/editText1" 
    android:text="" 
    android:layout_height="wrap_content" android:layout_width="300dip">
    </EditText>
    
    <TextView 
    android:text="@string/str3"  
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">
    </TextView>
    
    <EditText 
    android:id="@+id/editText2" 
    android:text="" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content">
    </EditText>
    
    
    
    <Button 
    android:text="計算台幣兌換美金" 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">
    </Button>
    
    
    <TextView 
    android:text="@string/str_btn1" 
    android:id="@+id/textView3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">
    </TextView>
   
   </LinearLayout>




以上紅色部分的程式會顯示為以下圖片中的
文字方塊 //TextView 
按鈕 //Button 
輸入資料的格子 //EditText 



其中上面程式的
android:text="@string/str2"
android:text="@string/str3"  
android:text="@string/str_btn1" 


是由string.xml中的程式來的
以下是它的內容


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, EX203!</string>
  <string name="app_name">匯率換算</string>
 
  <string name="str1">匯率換算</string>
  <string name="str2">匯率</string>
  <string name="str3">臺幣</string>
  
  <string name="str_btn1">可兌換美金</string>


</resources>




而主要的程式還是java檔

package edu.fcu.d9726481;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Change extends Activity {
 private EditText ed1, ed2;
 private Button btn1;
 private TextView tv1;
 public static final String MY_PREFS = "mSharedPreferences01";
    /** Called when the activity is first created. */
    @Override
    
    
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ed1 = (EditText)Change.this.findViewById(R.id.editText1);  // 匯率
        ed2 = (EditText)Change.this.findViewById(R.id.editText2);  // 台幣
        btn1 = (Button)Change.this.findViewById(R.id.button1);
        tv1 = (TextView)Change.this.findViewById(R.id.textView3);
        
        ed1.setText("28.5");
        ed2.setText("10000");
        
        btn1.setOnClickListener(new Button.OnClickListener(){


public void onClick(View v) {
// TODO Auto-generated method stub
package edu.fcu.d9726481; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class Change extends Activity { private EditText ed1, ed2; private Button btn1; private TextView tv1; public static final String MY_PREFS = "mSharedPreferences01"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ed1 = (EditText)Change.this.findViewById(R.id.editText1); // 匯率 ed2 = (EditText)Change.this.findViewById(R.id.editText2); // 台幣 btn1 = (Button)Change.this.findViewById(R.id.button1); tv1 = (TextView)Change.this.findViewById(R.id.textView3); ed1.setText("28.5"); ed2.setText("10000"); btn1.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { // TODO Auto-generated method stub ed1.setTextColor(Color.RED); } } ); } }
}
        }
        );
        
        
        
    }
}



所以執行在Android的結果就是下面這張圖












作業就是從上面程式中改橘色那一行
可以按按鈕使它改變顏色!!!(也可以改變其他的)
















心得:


感覺它寫程式的樣子跟原本的java好像差不多
但又有一些不一樣


像我要修改顏色時
不能使用原本教的方法set.background(red);















2011年5月6日 星期五

java - 繼承

繼承主要是在程式中加上 extends JFrame

如下:

public class TimerDemo extends JFrame implements ActionListener

所以可以用前面的程式修改

如下:


import javax.swing.Timer;
import java.awt.event.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;


public class TimerDemo extends JFrame implements ActionListener {
  static JTextField tbx1=new JTextField(30 ); // 建立1文字方塊物件
  Timer t = new Timer(1000,this);

 /* TimerDemo() {
    t.start();
    }
*/
  public static void main(String args[]) {
    TimerDemo td = new TimerDemo();
    FlowLayout flow=new FlowLayout();
    td.setSize(500,500);

    JButton btn1=new JButton("現在時間"); // 建立按鈕物件 btn1
    btn1.addActionListener(td);
    JPanel p1 = new JPanel(flow);
    td.add(p1); // 在視窗td 內加入 panel 1
    p1.add(tbx1); // 在 panel 1內加入文字方塊
    p1.add(btn1); // 在 panel 1內加入按鈕
    td.setVisible(true);



     // 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) {

  String time =""+Calendar.getInstance().getTime();
    /*if (e.getSource() == t) {
      System.out.println
        ("Being ticked " + Calendar.getInstance().getTime());
      }*/
    tbx1.setText(time);
       }
}









接下來教了Applet



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(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();
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();
}
}


}















作業
把它按鈕丟掉
但會顯示其效果


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);
                                img = ip.createImage();
                                ip.noise(100);
                                ip.findEdges();
                             
                         
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();
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();
}
}


}