2011-06-10

2011/06/10

顯示在觸控螢幕做了什麼動作
( http://coderzheaven.com/2011/03/29/using-gestures-in-androida-simple-example )


package pack.GestureSampleThree;
   
    import android.app.Activity;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.view.GestureDetector;
    import android.view.MotionEvent;
    import android.view.GestureDetector.OnGestureListener;
    import android.widget.LinearLayout;
    import android.widget.TextView;
   
    public class GestureSampleThreeExample extends Activity implements OnGestureListener {
         private LinearLayout main;
            private TextView viewA;
   
            private GestureDetector gestureScanner;
   
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
   
                gestureScanner = new GestureDetector(this);
   
                main = new LinearLayout(this);
                main.setBackgroundColor(Color.GRAY);
                main.setLayoutParams(new LinearLayout.LayoutParams(320,480));
   
                viewA = new TextView(this);
                viewA.setBackgroundColor(Color.YELLOW);
                viewA.setTextColor(Color.BLACK);
                viewA.setTextSize(16);
                viewA.setLayoutParams(new LinearLayout.LayoutParams(320,80));
                main.addView(viewA);
   
                setContentView(main);
            }
   
            @Override
            public boolean onTouchEvent(MotionEvent me) {
                return gestureScanner.onTouchEvent(me);
            }
   
            public boolean onDown(MotionEvent e) {
                viewA.setText("-" + "DOWN" + "-");
                return true;
            }
   
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            viewA.setText("-" + "FLING" + "-");
                return true;
            }
   
            public void onLongPress(MotionEvent e) {
                viewA.setText("-" + "LONG PRESS" + "-");
            }
   
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                viewA.setText("-" + "SCROLL" + "-");
                return true;
            }
   
            public void onShowPress(MotionEvent e) {
                viewA.setText("-" + "SHOW PRESS" + "-");
            }
   
            public boolean onSingleTapUp(MotionEvent e) {
                viewA.setText("-" + "SINGLE TAP UP" + "-");
                return true;
            }
        }


=========================================================================

計算BMI



Bm.java


package Bm.i;



import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.text.NumberFormat;

public class Bm extends Activity {
    /** Called when the activity is first created. */

    private EditText et1, et2;
    private Button bt1;
    private TextView tv1, tv2, tv3, tv4;
    public static final String MY_PREFS = "mSharedPreferences01";

  
  
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
      
      
      

        et1 = (EditText)Bm.this.findViewById(R.id.et1);
        et2 = (EditText)Bm.this.findViewById(R.id.et2);
        bt1 = (Button)Bm.this.findViewById(R.id.bt1);
        tv3 = (TextView)Bm.this.findViewById(R.id.tv3);
        tv4 = (TextView)Bm.this.findViewById(R.id.tv4);

     
        bt1.setOnClickListener(new Button.OnClickListener()
        {
     
          public void onClick(View arg0)
          {
              NumberFormat nf = NumberFormat.getInstance();
              nf.setMaximumFractionDigits( 2 );
            
              EditText et1 = (EditText)findViewById(R.id.et1);
              EditText et2 = (EditText)findViewById(R.id.et2);
              double h = Double.parseDouble(et1.getText().toString());
              double w = Double.parseDouble(et2.getText().toString());
              double BMI = w / ((h/100) * (h/100));
              tv3.setText("BMI:"+nf.format(BMI));
              if(BMI<18.5){
                  tv4.setText("體重過輕");
              }
              else if(18.5<=BMI & BMI<24){
                  tv4.setText("體重適中");
              }
              else if(24<=BMI){
                  tv4.setText("體重過重");
              }
             
          }
        });
     

  
      
      
    }
}




main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
   
    <TextView
    android:text="Height"
    android:id="@+id/tv1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ></TextView>
  
    <EditText
    android:id="@+id/et1"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:inputType="numberDecimal"
    >
        <requestFocus></requestFocus>
    </EditText>
  
  
    <TextView
    android:text="Weight"
    android:id="@+id/tv2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ></TextView>
  
    <EditText
    android:id="@+id/et2"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:inputType="numberDecimal"
    ></EditText>
  
    <Button
    android:text="Count"
    android:id="@+id/bt1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ></Button>
  
    <TextView
    android:text=" "
    android:id="@+id/tv3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ></TextView>
   
    <TextView
    android:text=" "
    android:id="@+id/tv4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ></TextView>
  
</LinearLayout>

2011-05-27

2011/05/27

Android

strings.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>




main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <TextView
    android:text="@string/str2"
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    </TextView>
      
    <EditText
     android:layout_height="wrap_content"
     android:id="@+id/editText1"
     android:text=""
     android:layout_width="match_parent">
     </EditText>
    
    <TextView
    android:text="@string/str3"
    android:layout_height="wrap_content"
    android:id="@+id/textView2"
    android:layout_width="wrap_content">
    </TextView>
   
    <EditText
    android:layout_height="wrap_content"
    android:id="@+id/editText2"
    android:text=""
    android:layout_width="match_parent">
    </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>



Try3.java

package Try.T;

import java.text.NumberFormat;

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

public class Try3 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)Try3.this.findViewById(R.id.editText1);  // 匯率
      ed2 = (EditText)Try3.this.findViewById(R.id.editText2);  // 台幣
      btn1 = (Button)Try3.this.findViewById(R.id.button1);
      tv1 = (TextView)Try3.this.findViewById(R.id.textView3);
       
     
      ed1.setText("33.5");
      ed2.setText("10000");
     
     
     
      btn1.setOnClickListener(new Button.OnClickListener()
      {
     
        public void onClick(View arg0)
        {
          btn1.setWidth(70);
           
           
            // 按鈕事件,處理數學換算的語法
          //NumberFormat nf = NumberFormat.getInstance();
          //nf.setMaximumFractionDigits( 2 );
         
          //double d = Double.parseDouble(ed2.getText().toString()) / Double.parseDouble(ed1.getText().toString());
         
          //tv1.setText( Try3.this.getResources().getString(R.string.str3) + ":" + nf.format(d) );
       
        }
      });
     
     
     
    }
}

2011-05-06

2011/05/06

import javax.swing.Timer;
import java.awt.event.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
public class TimerDemoI extends JFrame implements ActionListener {
  Timer t = new Timer(1000,this);
static  JTextField tf1=new JTextField();

  TimerDemoI() {
    t.start();
    }

  public static void main(String args[]) {
    TimerDemoI td = new TimerDemoI();
GridLayout grid33=new GridLayout(3,3);
td.setLayout(grid33);
td.setSize(450,450);
JPanel p1 = new JPanel(grid33);
p1.add(tf1);
td.add(p1);
td.setVisible(true);
    java.awt.Frame dummy = new java.awt.Frame();


//dummy.setSize(300,225);
//dummy.add(tf1);
//    dummy.setVisible(true);
    }

  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == t) {
Date da=Calendar.getInstance().getTime();
tf1.setText(da.toString());

 //      tf1.setText("Being ticked " + Calendar.getInstance().getTime());
//                       ↑上禮拜的方式  在Cal...前面加上字串(即"XX")  可不用再轉date string這些
//                      原先前面的/007去掉就不再會有聲音
      }
    }
}

==================================================================

一開就反白

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();
ip.invert();
img = ip.createImage();  //從下面綠色抓上來
repaint();
    }


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

}

2011-04-29

2011/04/29

import javax.swing.Timer;
import java.awt.event.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
public class TimerDemo implements ActionListener {
  Timer t = new Timer(1000,this);
static  JLabel tf1=new JLabel();

  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.setSize(300,225);
dummy.add(tf1);
    dummy.setVisible(true);
    }

  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == t) {

       tf1.setText("\007Being ticked " + Calendar.getInstance().getTime());
      System.out.println
        ("\007Being ticked " + Calendar.getInstance().getTime());
      }
    }
}






=================================================

import java.util.Timer;
import java.util.TimerTask;


public class ToDo  {
  Timer timer;

  public ToDo ( int seconds )   {
    timer = new Timer (  ) ;
    timer.schedule ( new ToDoTask (  ) , seconds*1000 ) ;

  }


  class ToDoTask extends TimerTask  {
    public void run (  )   {

System.out.println ( "OK, It's time to do something!" ) ;
timer.cancel (  ) ; //Terminate the thread
    }


  }


  public static void main ( String args [  ]  )   {
    System.out.println ( "Schedule something to do in 5 seconds." ) ;
    new ToDo ( 5 ) ;
    System.out.println ( "Waiting." ) ;
 TimerDemo t1=new TimerDemo();
  }
}

2011-04-22

2011/04/22

 random不重複



// AWT, JButton類別

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

public class swingtest1random extends JFrame  implements ActionListener
{


static JButton bt1=new JButton("RUN"); 
static JTextField tf1=new JTextField("1");
static JLabel lb[]=new JLabel[25];
static JButton bt[]=new JButton[25];
String tmp;
String numbers[]  = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22","23","24"};

public static void main(String args[])
{
swingtest1random fm=new swingtest1random();
GridLayout gl= new GridLayout(5,5);
JPanel p1 = new JPanel(gl);
JPanel p2 = new JPanel(gl);
JPanel p3 = new JPanel(gl);
String numbers[]  = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22","23","24"};
JButton buttons[]=new JButton[25];
fm.setLayout(gl);
fm.setSize(300,225);
fm.add(p1);
fm.add(p2);
fm.add(p3);

for (int i = 0; i < 25; i++)
{
bt[i] = new JButton(numbers[i]);
p1.add(bt[i], gl);
}
fm.setVisible(true);


p2.add(tf1, gl);
p2.add(bt1, gl);


for (int i = 0; i < numbers.length; i++)
{
lb[i] = new JLabel();
p3.add(lb[i], gl);
}
fm.setVisible(true);


bt1.addActionListener(fm);


}

public void actionPerformed(ActionEvent e)
{
//String s1;
//s1=tf1.getText();
//int i1;
//i1 = Integer.parseInt(s1);


for (int i = 0; i < 25; i++)
{
int j=24-i;
int r1=(int) (Math.random()*(j+1));
tmp=numbers[j];
numbers[j]=numbers[r1];
numbers[r1]=tmp;
}

for (int i = 0; i < 25; i++)
{
System.out.println(numbers[i]);
bt[i].setLabel(numbers[i]);
}
/*
for (int i = 0; i < 9; i++)
{

int r1=(int) (Math.random()*9);
//int r2=(int) (Math.random()*9);
System.out.println(r1);
String s1=Integer.toString(r1);
bt[i].setText(s1);
//lb[r1].setText(s1);

}
*/

}


}



==================================================================

輸入100內數字從其中挑25個數
// AWT, JButton類別

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

public class swingtest1random extends JFrame  implements ActionListener
{


static JButton bt1=new JButton("RUN"); 
static JTextField tf1=new JTextField("1");
static JLabel lb[]=new JLabel[25];
static JButton bt[]=new JButton[25];
String tmp;


public static void main(String args[])
{
swingtest1random fm=new swingtest1random();
GridLayout gl= new GridLayout(5,5);
JPanel p1 = new JPanel(gl);
JPanel p2 = new JPanel(gl);
JPanel p3 = new JPanel(gl);
String numbers[]  = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22","23","24"};
JButton buttons[]=new JButton[25];
fm.setLayout(gl);
fm.setSize(300,225);
fm.add(p1);
fm.add(p2);
fm.add(p3);

for (int i = 0; i < 25; i++)
{
bt[i] = new JButton(numbers[i]);
p1.add(bt[i], gl);
}
fm.setVisible(true);


p2.add(tf1, gl);
p2.add(bt1, gl);


for (int i = 0; i < numbers.length; i++)
{
lb[i] = new JLabel();
p3.add(lb[i], gl);
}
fm.setVisible(true);


bt1.addActionListener(fm);


}

public void actionPerformed(ActionEvent e)
{
//String s1;
//s1=tf1.getText();
//int i1;
//i1 = Integer.parseInt(s1);

String numbers[]=new String[101];
for (int i = 0; i <101; i++)
{
numbers[i]=String.valueOf(i);
}
for (int i = 0; i < 25; i++)
{

int j=24-i;
String m1=tf1.getText();
int m2=Integer.parseInt(m1);
int r1=(int) 1+(int)(Math.random()*m2);
tmp=numbers[j];
numbers[j]=numbers[r1];
numbers[r1]=tmp;
}

for (int i = 0; i < 25; i++)
{
System.out.println(numbers[i]);
bt[i].setLabel(numbers[i]);
}
/*
for (int i = 0; i < 9; i++)
{

int r1=(int) (Math.random()*9);
//int r2=(int) (Math.random()*9);
System.out.println(r1);
String s1=Integer.toString(r1);
bt[i].setText(s1);
//lb[r1].setText(s1);

}
*/

}


}

2011-04-01

2011/04/01

取代



註解掉很多

  // AWT, JButton類別

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

public class swingtest extends JFrame  implements ActionListener
{


static JButton bt1=new JButton("RUN");   // 建立按鈕物件
static JTextField tf1=new JTextField("1");

/*
static JTextField tf1=new JTextField("1");
static JTextField tf2=new JTextField("2");
static JTextField tf3=new JTextField("3");
static JTextField tf4=new JTextField("4");
static JTextField tf5=new JTextField("5");
static JTextField tf6=new JTextField("6");
static JTextField tf7=new JTextField("7");
*/

public static void main(String args[])
{
swingtest fm=new swingtest();
//BorderLayout bd=new BorderLayout();
//fm.setLayout(bd);
GridLayout gl= new GridLayout(3,3);
JPanel p1 = new JPanel(gl);
JPanel p2 = new JPanel(gl);
String numbers[]  = {"0", "1", "2", "3", "4", "5", "6", "7", "8"};
JButton buttons[]=new JButton[10];
fm.setLayout(gl);
fm.setSize(300,225);
fm.add(p1);
fm.add(p2);


p2.add(tf1, gl);
p2.add(bt1, gl);

for (int i = 0; i < numbers.length; i++)
{
buttons[i] = new JButton(numbers[i]);
p1.add(buttons[i], gl);
}
fm.setVisible(true);

bt1.addActionListener(fm);

/*
fm.add(bt1);   // 在視窗內加入按鈕
fm.add(tf1);
fm.add(tf2);
fm.add(tf3);
fm.add(tf4);
fm.add(tf5);
fm.add(tf6);
fm.add(tf7);
*/

}

public void actionPerformed(ActionEvent e)
{
String s1;
s1=tf1.getText();
System.out.println(s1);
}



/*
public void actionPerformed(ActionEvent e)
{
int r1,r2,r3,r4,r5,r6,r7;
r1=(int) (Math.random()*49)+1;
r2=(int) (Math.random()*49)+1;
r3=(int) (Math.random()*49)+1;
r4=(int) (Math.random()*49)+1;
r5=(int) (Math.random()*49)+1;
r6=(int) (Math.random()*49)+1;
r7=(int) (Math.random()*49)+1;
String sv1 = Integer.toString(r1);
String sv2 = Integer.toString(r2);
String sv3 = Integer.toString(r3);
String sv4 = Integer.toString(r4);
String sv5 = Integer.toString(r5);
String sv6 = Integer.toString(r6);
String sv7 = Integer.toString(r7);
tf1.setText(sv1);
tf2.setText(sv2);
tf3.setText(sv3);
tf4.setText(sv4);
tf5.setText(sv5);
tf6.setText(sv6);
tf7.setText(sv7);
}
*/

}


 執行圖