import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Paint extends JFrame implements ActionListener {
private Draw d;
private String strDraw[]={"曲线","直线","矩形","椭圆","实心椭圆","颜色"};
private JButton btnDraw[]=new JButton[strDraw.length];
private boolean boo=false;//判断
private JDialog dlg=null;//报错信息,选择工具
Graphics g;
public Paint(String str) {
super(str);
Dimension dim=getToolkit().getScreenSize();
this.setBounds(dim.width/4,dim.height/4, dim.width/2,dim. height/2);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
Panel p=new Panel();
p.setLayout(new FlowLayout(FlowLayout.CENTER));
for(int i=0;i<strDraw.length;i++){
btnDraw[i]=new JButton(strDraw[i]);
p.add(btnDraw[i]);
btnDraw[i].addActionListener(this);
}
this.getContentPane().add(p,BorderLayout.NORTH);
d=new Draw();
this.getContentPane().add(d);
this.setVisible(true);
}
public static void main(String[] args) {
new Paint("畫板");
}
class Draw extends Canvas implements MouseListener, MouseMotionListener{
private Point start,end,lastEnd;
private String drawType=null;
private Color color=Color.black;
private MyPointLine p[]=null;//用于永久保存
private int numPoint=0;
private String drawCurve="curve",drawRanctangle="ranctangle",drawLine="line",drawEllipse="ellipse",drawEraser="eraser";
//注意如果要把所以图像放在一个数组中(需要设计一个类,来包含所以的图形,在该类中添加一个专用于type的成员变量)
//如果只是单纯的判断按钮的getAconTion的值,则会刷掉前面画的图,,,或者可以用不同的MyPointLine[] p句存
public Draw(){
p=new MyPointLine[1];
p[0]=new MyPointLine();
numPoint=0;
color=Color.red;
start = end =lastEnd= null;
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
public void paint(Graphics g) {
if(p==null&&p.length==0){
return;
}//判断是否为空
for (int i = 0; i < p.length; i++) {
try {
if (dlg!=null) {
dlg.dispose();
}
if(p[i].getStart()==null&&p[i].getEnd()==null){
g.drawLine(p[i].getStart().x, p[i].getStart().y, p[i].getEnd().x, p[i].getEnd().y);
}
g.setColor(p[i].getColor());
if(p[i]!=null&&p[i].getStart()!=null&&p[i].getEnd()!=null&&drawLine.equals(p[i].getDrawType())){
//画直线
g.drawLine(p[i].getStart().x, p[i].getStart().y, p[i].getEnd().x, p[i].getEnd().y);
}else if(p[i]!=null&&p[i].getStart()!=null&&p[i].getEnd()!=null&&drawCurve.equals(p[i].getDrawType())){
//画曲线
g.drawLine(p[i].getStart().x, p[i].getStart().y, p[i].getEnd().x, p[i].getEnd().y);
}else if(p[i]!=null&&p[i].getStart()!=null&&p[i].getEnd()!=null&&drawRanctangle.equals(p[i].getDrawType())){
//画矩形
g.drawRect(p[i].getStart().x, p[i].getStart().y, p[i].getEnd().x, p[i].getEnd().y);
}else if(p[i]!=null&&p[i].getStart()!=null&&p[i].getEnd()!=null&&drawEllipse.equals(p[i].getDrawType())){
g.drawOval(p[i].getStart().x, p[i].getStart().y, p[i].getEnd().x, p[i].getEnd().y);
}else if(p[i]!=null&&p[i].getStart()!=null&&p[i].getEnd()!=null&&drawEraser.equals(p[i].getDrawType())){
g.fillOval(p[i].getStart().x, p[i].getStart().y, p[i].getEnd().x, p[i].getEnd().y);
}else if(p[i].getDrawType().equals(null)){
JOptionPane.showMessageDialog(null, "请选择工具");
}
} catch (Exception e) {
//
}
}
}
public void addPoint(){
if(numPoint==p.length){
MyPointLine[] pointTemp=new MyPointLine[p.length +1];
for(int i=0;i<pointTemp.length-1;i++){
pointTemp[i]=p[i];
}
p=null;
p=pointTemp;
}
p[numPoint]=new MyPointLine();
p[numPoint].setColor(color);
p[numPoint].setDrawType(drawType);
}
@Override
public void mouseDragged(MouseEvent e) {
try {
if (drawType.equals(drawLine)) {
p[numPoint].setEnd(new Point(e.getX(), e.getY()));
repaint();
} else if (drawType.equals("curve")) {
p[numPoint].setStart(p[numPoint].getEnd());
p[numPoint].setEnd(new Point(e.getX(), e.getY()));
repaint();
numPoint++;
addPoint();
p[numPoint].setEnd(new Point(e.getX(), e.getY()));
} else if (drawType.equals("ranctangle")) {
p[numPoint].setEnd(new Point(e.getX(), e.getY()));
repaint();
} else if (drawType.equals(drawEllipse)||drawType.equals("eraser")) {
p[numPoint].setEnd(new Point(e.getX(), e.getY()));
repaint();
}
} catch (Exception e2) {
}
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
addPoint();
p[numPoint].setStart(new Point(e.getX(), e.getY()));
}
@Override
public void mouseReleased(MouseEvent e) {
p[numPoint].setEnd(new Point(e.getX(), e.getY()));
repaint();
numPoint++;
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equalsIgnoreCase("直线")){
d.drawType="line";
}
if(e.getActionCommand().equalsIgnoreCase("曲线")){
d.drawType="curve";
}
if(e.getActionCommand().equalsIgnoreCase("橡皮擦")){
d.drawType="eraser";
}
if(e.getActionCommand().equalsIgnoreCase("矩形")){
d.drawType="ranctangle";
}
if(e.getActionCommand().equalsIgnoreCase("椭圆")){
d.drawType="ellipse";
}
if(e.getActionCommand().equalsIgnoreCase("颜色")){
d.color=JColorChooser.showDialog(null, "请选择颜色", getBackground());
}
}
}
java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
public class MoneyJFrame extends JFrame implements CaretListener {
JTextField tfdMoney,tfdStr;
JDialog dlg = null;
public MoneyJFrame(String str) {
super(str);
this.setBounds(300, 240, 500, 140);
//this.setBackground(java.awt.Color.red);
this.getContentPane().setBackground(Color.WHITE);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(new FlowLayout(FlowLayout.RIGHT));
Font font = new Font("a",Font.BOLD,20);
JLabel lb = new JLabel("金额");
lb.setFont(font);
this.getContentPane().add(lb);
tfdMoney = new JTextField("12345.67",20);
tfdMoney.setFont(font);
tfdMoney.setHorizontalAlignment(JTextField.RIGHT);
this.getContentPane().add(tfdMoney);
JLabel lb2 = new JLabel("中文大写形式");
lb2.setFont(font);
this.getContentPane().add(lb2);
tfdStr = new JTextField(20);
tfdStr.setFont(font);
tfdStr.setHorizontalAlignment(JTextField.RIGHT);
tfdStr.setEditable(false);
this.getContentPane().add(tfdStr);
tfdMoney.addCaretListener(this);
this.setVisible(true);
}
@Override
public void caretUpdate(CaretEvent e) {
//System.out.println("kkk");
String str = tfdMoney.getText();
try {
double x = Double.parseDouble(str);
tfdStr.setText( RMBtoString(x) );
} catch (NumberFormatException e1) {
if(dlg!=null){
dlg.dispose();
}
dlg = new JDialog(this,true);//true为模式对话框。默认即不给布尔型参数则为无模式
dlg.setSize(300, 80);
dlg.add(new JLabel(str+"不能转换成浮点数,请重新输入!"));
int lx = this.getX();
int ly = this.getY();
dlg.setLocation(lx+20, ly+20);
dlg.setVisible(true);
}
}
private String RMBtoString(double x) {
String digit="零壹贰叁肆伍陆柒捌玖";
String yuan="亿千百拾万千百拾元角分";
String result="";
int y = (int)(x*100+0.5);//四舍五入
int i = yuan.length()-1;
while(y>0 && i>0){
String str = ""+digit.charAt((y%10))+yuan.charAt(i);
result = str + result;
y = y/10;
i--;
}
//System.out.println(result);
return result;
}
public static void main(String[] args) {
new MoneyJFrame("金额的中文大写形式");
}
}