2016年2月25日 星期四

JAVA GUI MyEditor.java

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

public class MyEditor extends JFrame
{
JTextArea txt; //代表文字區域的物件
JFileChooser file = new JFileChooser(".");
public static void main(String[] args)
{
MyEditor f = new MyEditor();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(240,240);
f.setVisible(true);
}
public MyEditor()
{
txt = new JTextArea(80,80);
JScrollPane p = new JScrollPane(txt);
Container contentPane = getContentPane();
contentPane.add(buildMenu(),"North");
contentPane.add(p,"Center");
}
//建立功能表內容的方法
public JMenuBar buildMenu()
{
JMenuBar mbar= new JMenuBar(); //建立功能表欄
JMenu menu = new JMenu("檔案 (F)");
menu.setMnemonic(KeyEvent.VK_F);
mbar.add(menu);
//設定檔案功能表的項目
//開啟檔案
JMenuItem item = new JMenuItem("開啟(O)",KeyEvent.VK_O);
item.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
readfile();
}
});
menu.add(item); //將項目加到功能表中
//儲存檔案
item = new JMenuItem("儲存 (S)" , KeyEvent.VK_S);
item.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
writefile();
}
});
menu.add(item);
//結束程式
item = new JMenuItem("結束(X)",KeyEvent.VK_X);
item.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
menu.add(item); //將項目加到功能表中
return mbar;

}
public void readfile()
{
int state = file.showOpenDialog(this);
if(state == JFileChooser.APPROVE_OPTION)
{
File f = file.getSelectedFile();
try
{
//讀取檔案
txt.read(new FileReader(f),"");
}
catch(IOException e)
{
System.out.println(e);
}
setTitle(f.getName()); //將視窗標題設為檔案名稱
}
}
public void writefile()
{
int state =  file.showSaveDialog(this);
if(state == JFileChooser.APPROVE_OPTION)
{
File f = file.getSelectedFile();
//寫入檔案
try
{
txt.write(new FileWriter(f));

}
catch(IOException e)
{
System.out.println(e);
}

}
}
}

2016年2月24日 星期三

JAVA GUI TrigonoCalc.java

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

public class TrigonoCalc extends KeyAdapter implements ActionListener
{
JFrame myframe = new JFrame("計算三角函數");
JRadioButton deg = new JRadioButton("角度");
JRadioButton rad = new JRadioButton("徑度");
JTextField degree = new JTextField();
JTextField sintxt = new JTextField();
JTextField costxt = new JTextField();
JTextField tantxt = new JTextField();
JLabel sinlab = new JLabel(" SIN() ");
JLabel coslab = new JLabel(" COS() ");
JLabel tanlab = new JLabel(" TAN() ");
JButton go = new JButton("計算");
double convert = 180 / Math.PI; //一逕度等於(180/PI) 度

public static void main(String[] args)
{
TrigonoCalc tri = new TrigonoCalc();
tri.init();
}
public void init()
{
Container contentPane = myframe.getContentPane();
JPanel p = new JPanel();
//將兩個元件及JPanel 加入 JFrame
contentPane.add(degree,"North");
contentPane.add(p,"Center");
contentPane.add(go,"South");
  //將JPanel 設定為使用GridLayout(4列,2行)
p.setLayout(new GridLayout(4,2));
//將各元件加到JPanel中
p.add(deg);
p.add(rad);
p.add(sinlab);
p.add(sintxt);
p.add(coslab);
p.add(costxt);
p.add(tanlab);
p.add(tantxt);
//設定選單角度單位的快捷建
deg.setMnemonic(KeyEvent.VK_D);
rad.setMnemonic(KeyEvent.VK_R);

//將兩個單選紐設為一組
ButtonGroup group = new ButtonGroup();
group.add(deg);
group.add(rad);
deg.setSelected(true); //將deg設為預設選取的項目
go.addActionListener(this);
degree.addKeyListener(this);
//單選紐的選取事件之處理方法
deg.addItemListener(
new ItemListener(){
public void itemStateChanged(ItemEvent e){
if(e.getStateChange() == ItemEvent.SELECTED)
convert = 180 / Math.PI;
else
convert = 1;
}
}
);
myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myframe.setSize(240,240);
myframe.setVisible(true);

}
public void actionPerformed(ActionEvent e)
{
calc();
}
//再輸入區按 ENTER 鍵也進行計算
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_ENTER) calc();
}
public void calc()
{
try
{
//取得輸入區的字串,轉成浮點數後除以角度換算單位
double theta = Double.parseDouble(degree.getText())/convert;
//計算三角函數值,並將結果寫到各文字欄位中
sintxt.setText(Double.toString(Math.sin(theta)));
costxt.setText(Double.toString(Math.cos(theta)));
tantxt.setText(Double.toString(Math.tan(theta)));
}
catch(NumberFormatException e)
{
degree.setText(""); //發生例外時清除輸入區內容
}
}
}

JAVA GUI ShowImage.java

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

public class ShowImage extends JPanel
{
Image img = null; //代表影像的物件
public ShowImage(String filename)
{
img = getToolkit().getImage(filename); //載入指定的影像檔

}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Dimension d = getSize(); //取得面板大小

//建立座標轉換物件
AffineTransform at = new AffineTransform();

//依 Panel 的區域大小來調整顯示比例
double sc = Math.min(d.width/(double)img.getWidth(null),d.height/(double)img.getHeight(null));
at.scale(sc,sc);
g2.drawImage(img,at,this); //顯示影像

}
public static void main(String[] args)
{
try
{
ShowImage dimg = new ShowImage(args[0]);
JFrame f = new JFrame(args[0]);
f.getContentPane().add(dimg);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(640,480);
f.setVisible(true);
}
catch(Exception e)
{
System.out.println("用法: java ShowImage<影像檔名稱>");
System.exit(0);
}
}
}

2016年2月23日 星期二

JAVA GUI ColorShape.java

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

public class ColorShape extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
//根據 MyPanel 的寬與高調整圖案大小
float Width = getSize().width - 10;
float Height = getSize().height/3 - 10;
// 畫兩條紅色線
g2.setPaint(Color.red);
g2.draw(new Line2D.Float(5,5,5+Width,5+Height));
g2.draw(new Line2D.Float(5,5+Height,5+Width,5));
//畫自訂顏色的的舉形
g2.setPaint(new Color(192,128,64));
g2.fill(new Rectangle2D.Float(5,10+Height,Width,Height));
//畫建層橢圓
g2.setPaint(new GradientPaint(5,5,Color.blue,5+Width,5,Color.yellow));
g2.fill(new Ellipse2D.Float(5,20+2*Height,Width,Height));

}
public static void main(String[] args)
{
JFrame f = new JFrame("色彩應用");
ColorShape p = new ColorShape();
p.setBackground(Color.white); //將背景設為白色
f.getContentPane().add(p);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(640,480);
f.setVisible(true);
}
}

2016年2月22日 星期一

JAVA GUI ShowLines.java (JAVA8 無效)

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

public class ShowLines extends JPanel
{
public void painComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
//根據 MyPanel 的寬與高來調整線條長短
double Width =10; //getSize().width/6-20;
double Height =10; //getSize().height-30;
//畫 CAP_BUTT , JOIN_BEVEL線
g2.setStroke(new BasicStroke(15,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL));
g2.draw(new Line2D.Double(10,10+Height,10+Width,10));
g2.draw(new Line2D.Double(10+Width,10,10+2*Width,10+Height));

//畫 CAP_ROUND 、 JOIN_MITER線
g2.setStroke(new BasicStroke(15,BasicStroke.CAP_ROUND,BasicStroke.JOIN_MITER));
g2.draw(new Line2D.Double(30+2*Width,10+Height,30+3*Width,10));
g2.draw(new Line2D.Double(30+3*Width,10,30+4*Width,10+Height));

//畫CAP_SQUARE 、 JOIN_ROUND線
g2.setStroke(new BasicStroke(15,BasicStroke.CAP_SQUARE,BasicStroke.JOIN_ROUND));
g2.draw(new Line2D.Double(50+4*Width,10+Height,50+5*Width,10));
g2.draw(new Line2D.Double(50+5*Width,10,50+6*Width,10+Height));
}
public static void main(String[] args)
{
JFrame f = new JFrame("不同樣式的線斷");
f.getContentPane().add(new ShowLines());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(720,160);
f.setVisible(true);
}
}

2016年2月18日 星期四

JAVA GUI SimpleShape.java

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

public class SimpleShape extends JPanel
{
public void paintComponent(Graphics g){
//請上層容器重畫,已清除原有的內容
super.paintComponent(g);
//取得面板的寬與高
double Width = getSize().width - 10;
double Height = getSize().height / 4 - 10;
Graphics2D g2 = (Graphics2D) g;
g2.draw(new Line2D.Double(5,5,5+Width,5+Height)); //畫線
g2.draw(new Rectangle2D.Double(5,10+Height,Width,Height)); //畫矩形
g2.draw(new RoundRectangle2D.Double(5,15+2*Height,Width,Height,20,30)); //化園角矩型
g2.draw(new Ellipse2D.Double(5,20+ 3*Height,Width,Height)); //畫橢圓


}
public static void main(String[] args){
JFrame f = new JFrame("幾何圖形");
f.getContentPane().add(new SimpleShape());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(640,480);
f.setVisible(true);
}
}

2016年2月16日 星期二

JAVA GUI GridChangeColor.java

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

public class GridChangeColor implements ItemListener
{
JFrame f = new JFrame("變色龍");
JPanel p = new JPanel();
String[] cname = {"紅","橙","黃","綠"};
JRadioButton[] cb = new JRadioButton[4];

public static void main(String[] args)
{
GridChangeColor test = new GridChangeColor();
test.init();
}

public void init()
{
Container contentPane = f.getContentPane();
contentPane.add(p,"Center");
JPanel up = new JPanel();
contentPane.add(up,"North");
up.setLayout(new GridLayout(2,2));
ButtonGroup g = new ButtonGroup();
//用迴圈加入每個單選紐,設定其傾聽者
for(int i=0;i {
cb[i] = new JRadioButton(cname[i]);
up.add(cb[i]);
g.add(cb[i]);
cb[i].addItemListener(this);
}
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(320,240);
f.setVisible(true);
}
        //單選紐被選擇取及取消的事件處理方法
public void itemStateChanged(ItemEvent e)
{
if(e.getStateChange() == ItemEvent.SELECTED)
{
//取得產生事件的按鈕
JRadioButton s = (JRadioButton) e.getSource();
//將 p 的背景顏色換成對應的顏色
if(s == cb[0]) p.setBackground(Color.red);
else if(s == cb[1]) p.setBackground(Color.orange);
else if(s == cb[2]) p.setBackground(Color.yellow);
else p.setBackground(Color.green);
}
}

}