java图形编辑系统代码,Java图形界面编程
用JAVA编写一个图形应用程序,可以是一个简单的文本编辑器、计算器等等。 求完整代码
//百度文库找的,可费了我的财富值了,你可要把分给我呀。
成都创新互联服务项目包括成安网站建设、成安网站制作、成安网页制作以及成安网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,成安网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到成安省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
package lee;
/*文件名:Calculator.java
*说明:简易科学计算器
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends Frame implements ActionListener, WindowListener
{
private Container container;
private GridBagLayout layout;
private GridBagConstraints constraints;
private JTextField displayField; //计算结果显示区
private String lastCommand; //保存+,-,*,/,=命令0
private double result; //保存计算结果
private boolean start; //判断是否为数字的开始
private JMenuBar menubar;
private JMenuItem m_exit,m2_ejz,m2_bjz;
private Dialog dialog;
private Label label_dialog;
private JButton button_sqrt,button_plusminus,button_CE,button_cancel,button_1,button_2,
button_3,button_4,button_5,button_6,button_7,button_8,button_9,button_0,
button_plus,button_minus,button_multiply,button_divide,button_point,
button_equal,button_log,button_tan,button_cos,button_sin,button_exp;
public Calculator() //构造方法设置布局、为按钮注册事件监听器
{
super("Calculator");
this.setLocation(240,200);
this.setSize(350,300);
this.setResizable(true);
this.setLayout(new GridLayout(7,1));
this.addmyMenu(); //调用成员方法添加菜单
displayField=new JTextField(30);
this.add(displayField);
displayField.setEditable(true);
start=true;
result=0;
lastCommand = "=";
JPanel panel0=new JPanel();
panel0.setLayout(new GridLayout(1,4,4,4));
JPanel panel1=new JPanel();
panel1.setLayout(new GridLayout(1,5,4,4));
this.add(panel1);
button_sqrt=new JButton("sqrt");
button_plusminus=new JButton("+/-");
button_exp=new JButton("exp");
button_CE=new JButton("退格");
button_cancel=new JButton("C");
JPanel panel2=new JPanel();
panel2.setLayout(new GridLayout(1,5,4,4));
this.add(panel2);
button_7=new JButton("7");
button_8=new JButton("8");
button_9=new JButton("9");
button_log=new JButton("log");
button_divide=new JButton("/");
JPanel panel3=new JPanel();
panel3.setLayout(new GridLayout(1,5,4,4));
this.add(panel3);
button_4=new JButton("4");
button_5=new JButton("5");
button_6=new JButton("6");
button_tan=new JButton("tan");
button_multiply=new JButton("*");
JPanel panel4=new JPanel();
panel4.setLayout(new GridLayout(1,5,4,4));
this.add(panel4);
button_1=new JButton("1");
button_2=new JButton("2");
button_3=new JButton("3");
button_cos=new JButton("cos");
button_minus=new JButton("-");
JPanel panel5=new JPanel();
panel5.setLayout(new GridLayout(1,5,4,4));
this.add(panel5);
button_0=new JButton("0");
button_point=new JButton(".");
button_equal=new JButton("=");
button_sin=new JButton("sin");
button_plus=new JButton("+");
panel1.add(button_sqrt);
panel1.add(button_plusminus);
panel1.add(button_exp);
panel1.add(button_CE);
panel1.add(button_cancel);
panel2.add(button_7);
panel2.add(button_8);
panel2.add(button_9);
panel2.add(button_log);
panel2.add(button_divide);
panel3.add(button_4);
panel3.add(button_5);
panel3.add(button_6);
panel3.add(button_tan);
panel3.add(button_multiply);
panel4.add(button_1);
panel4.add(button_2);
panel4.add(button_3);
panel4.add(button_cos);
panel4.add(button_minus);
panel5.add(button_0);
panel5.add(button_point);
panel5.add(button_equal);
panel5.add(button_sin);
panel5.add(button_plus);
button_sqrt.addActionListener(this);
button_plusminus.addActionListener(this);
button_exp.addActionListener(this);
button_CE.addActionListener(this);
button_cancel.addActionListener(this);
button_7.addActionListener(this);
button_8.addActionListener(this);
button_9.addActionListener(this);
button_log.addActionListener(this);
button_divide.addActionListener(this);
button_4.addActionListener(this);
button_5.addActionListener(this);
button_6.addActionListener(this);
button_tan.addActionListener(this);
button_multiply.addActionListener(this);
button_1.addActionListener(this);
button_2.addActionListener(this);
button_3.addActionListener(this);
button_cos.addActionListener(this);
button_minus.addActionListener(this);
button_0.addActionListener(this);
button_point.addActionListener(this);
button_equal.addActionListener(this);
button_sin.addActionListener(this);
button_plus.addActionListener(this);
this.addWindowListener(new WinClose()); //注册窗口监听器
this.setVisible(true);
}
private void addmyMenu() //菜单的添加
{
JMenuBar menubar=new JMenuBar();
this.add(menubar);
JMenu m1=new JMenu("选项");
JMenu m2=new JMenu("进制转换");
JMenuItem m1_exit=new JMenuItem("退出");
m1_exit.addActionListener(this);
JMenuItem m2_ejz=new JMenuItem("二进制");
m2_ejz.addActionListener(this);
JMenuItem m2_bjz=new JMenuItem("八进制");
m2_bjz.addActionListener(this);
JMenu m3 = new JMenu(" 帮助");
JMenuItem m3_Help = new JMenuItem("用法");
m3_Help.addActionListener(this);
dialog = new Dialog(this,"提示",true); //模式窗口
dialog.setSize(240,80);
label_dialog = new Label("",Label.CENTER); //标签的字符串为空,居中对齐
dialog.add(label_dialog);
dialog.addWindowListener(this); //为对话框注册窗口事件监听器
m1.add(m1_exit);
menubar.add(m1);
m2.add(m2_ejz);
m2.add(m2_bjz);
menubar.add(m2);
m3.add(m3_Help);
menubar.add(m3); }
public void actionPerformed(ActionEvent e) //按钮的单击事件处理方法
{
if(e.getSource().equals(button_1)||e.getSource().equals(button_2)||
e.getSource().equals(button_3)||e.getSource().equals(button_4)||
e.getSource().equals(button_5)|| e.getSource().equals(button_6)||
e.getSource().equals(button_7)|| e.getSource().equals(button_8)||
e.getSource().equals(button_9) ||e.getSource().equals(button_0)||
e.getSource().equals(button_point)||e.getSource().equals(button_plusminus)||
e.getSource().equals(button_cancel)||e.getSource().equals(button_CE))
{ //非运算符的处理方法
String input=e.getActionCommand();
if (start)
{
displayField.setText("");
start=false;
if(input.equals("+/-"))
displayField.setText(displayField.getText()+"-");
}
if(!input.equals("+/-"))
{
String str=displayField.getText();
if(input.equals("退格")) //退格键的实现方法
{
if(str.length()0)
displayField.setText(str.substring(0,str.length()-1));
}
else if(input.equals("C")) //清零键的实现方法
{
displayField.setText("0");
start=true;
}
else
displayField.setText(displayField.getText()+input);
}
}
else if (e.getActionCommand()=="二进制") //二进制的转换
{
int n=Integer.parseInt(displayField.getText());
displayField.setText(Integer.toBinaryString(n));
}
else if (e.getActionCommand()=="八进制") //八进制的转换
{
int n=Integer.parseInt(displayField.getText());
displayField.setText(Integer.toOctalString(n));
}
else if (e.getActionCommand()=="退出") //选项中退出的处理方法
{
System.exit(0);
}
else if (e.getActionCommand()=="用法") //按下'帮助'菜单栏中用法的处理方法
{
label_dialog.setText("sqrt,exp等键是先输运算符再输数字\n");
dialog.setLocation(400,250);
dialog.setVisible(true);
}
else //各运算符的识别
{
String command=e.getActionCommand();
if(start)
{
lastCommand=command;
}
else
{
calculate(Double.parseDouble(displayField.getText()));
lastCommand=command;
start=true;
}
}
}
public void calculate(double x) //各运算符的具体运算方法
{
double d=0;
if (lastCommand.equals("+"))
result+= x;
else if (lastCommand.equals("-"))
result-=x;
else if (lastCommand.equals("*"))
result*=x;
else if (lastCommand.equals("/"))
result/=x;
else if (lastCommand.equals("="))
result=x;
else if (lastCommand.equals("sqrt"))
{
d=Math.sqrt(x);
result=d;
}
else if (lastCommand.equals("exp"))
{
d=Math.exp(x);
result=d;
}
else if (lastCommand.equals("log"))
{
d=Math.log(x);
result=d;
}
else if (lastCommand.equals("tan"))
{
d=Math.tan(x);
result=d;
}
else if (lastCommand.equals("cos"))
{
d=Math.cos(x);
result=d;
}
else if (lastCommand.equals("sin"))
{
d=Math.sin(x);
result=d;
}
displayField.setText(""+ result);
}
public void windowClosing(WindowEvent e)
{
if(e.getSource()==dialog)
dialog.setVisible(false); //隐藏对话框
else
System.exit(0);
}
public void windowOpened(WindowEvent e) { }
public void windowActivated(WindowEvent e) { }
public void windowDeactivated(WindowEvent e) { }
public void windowClosed(WindowEvent e) { }
public void windowIconified(WindowEvent e) { }
public void windowDeiconified(WindowEvent e) { }
public static void main(String args[])
{
Calculator calculator=new Calculator();
}
}
class WinClose implements WindowListener
{
public void windowClosing(WindowEvent e) //单击窗口关闭按钮时触发并执行实现窗口监听器接口中的方法
{
System.exit(0); //结束程序运行
}
public void windowOpened(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
}
Java编写图形用户界面程序
运行如图
参考代码如下
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RegDemo extends JFrame implements ActionListener{
JTextField jtf;
JPasswordField jpf;
public RegDemo() {
//组件的创建, 和布局安排
JPanel jpc = new JPanel();//默认流式布局
JPanel jp1 = new JPanel(new GridLayout(2, 2,5,10));//网格布局
jp1.setBorder(BorderFactory.createTitledBorder("用户注册"));
JLabel jl1 = new JLabel("用户名:");
jtf = new JTextField(10);
JLabel jl2 = new JLabel("密码:");
jpf = new JPasswordField(10);
jpf.setEchoChar('*');//用*号来隐藏密码的显示
jp1.add(jl1);jp1.add(jtf);
jp1.add(jl2);jp1.add(jpf);
jpc.add(jp1);
add(jpc);
JButton jb1 = new JButton("提交");
jb1.addActionListener(this);
jb1.setActionCommand("yes");
JButton jb2 = new JButton("取消");
jb2.addActionListener(this);
jb2.setActionCommand("no");
JPanel jp2 = new JPanel();
jp2.add(jb1);jp2.add(jb2);
add(jp2,BorderLayout.SOUTH);
setTitle("用户注册界面");
setSize(280, 280);
setLocationRelativeTo(null);//窗口居中
setDefaultCloseOperation(EXIT_ON_CLOSE);//
setVisible(true);
}
public static void main(String[] args) {
new RegDemo();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("yes")){
String name = jtf.getText().trim();
String pwd = new String(jpf.getPassword());
if(name.equals("")||pwd.equals("")){
JOptionPane.showMessageDialog(this, "你还没有输入用户名或者密码");
}else{
JOptionPane.showMessageDialog(this, "注册成功!用户名"+name+",密码"+pwd);
}
}else{
jtf.setText("");
jpf.setText("");
}
}
}
java图形界面代码解惑
一般来说.比较常使用的是 JTextArea文本框的无参数构造器进行构造
然后可以使用setText("文字")来设置文本,或者getText()方法来获得文本
JTextArea jta = new JTextArea();
jta.setText("你好啊");
String txt = jta.getText();
具体例子
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class ReadBook extends JFrame{
JTextArea jta;
JTextField jtf;
JButton jb;
public ReadBook(){
jta = new JTextArea();
jtf = new JTextField(30);
jtf.setText("文件路径如c:\\ab.txt");
jb = new JButton("显示文字");
JPanel jp = new JPanel();
jp.add(jtf);
jp.add(jb);
add(jta);
add(jp,BorderLayout.SOUTH);
setBounds(500, 100, 500, 380);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String path = jtf.getText();
File f= new File(path);
if(f.exists()!f.isDirectory()){//存在该文件且不是文件夹
try {
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new FileReader(f));
String temp;
while((temp=br.readLine())!=null){
sb.append(temp+"\n");
}
br.close();
jta.setText(sb.toString());//setText方法可以给jta添加文字
} catch (Exception e1) {
e1.printStackTrace();
}
}else{
jtf.setText("路径错误.重新填写");
}
}
});
}
public static void main(String[] args) {
new ReadBook();
}
}
效果
求一个简单的java代码:(图形界面)
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class vv extends JDialog {
private static final long serialVersionUID = 1L;
private JLabel l_Id = new JLabel("登陆账户", JLabel.CENTER);
private JLabel l_pw = new JLabel("登陆密码", JLabel.CENTER);
private JTextField t_Id = new JTextField(10);
private JPasswordField t_pw = new JPasswordField(10);
private JButton btnLogin;
private JButton btnClose;
public vv() {
super();
setResizable(false);
getContentPane().setBackground(new Color(225, 225, 225));
getContentPane().setLayout(null);
initialize();
}
protected void initialize() {
setTitle("系统登录");
l_Id.setBounds(48, 43, 53, 25);
t_Id.setBounds(110, 43, 150, 25);
l_pw.setBounds(48, 93, 53, 25);
t_pw.setBounds(110, 93, 150, 25);
getContentPane().add(l_Id);
getContentPane().add(l_pw);
getContentPane().add(t_Id);
getContentPane().add(t_pw);
btnLogin = new JButton();
btnLogin.setText("登 录");
btnLogin.setBounds(70, 142, 85, 28);
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addBtnLoginActionListener();
}
});
getContentPane().add(btnLogin);
btnClose = new JButton();
btnClose.setText("关 闭");
btnClose.setBounds(175, 142, 85, 28);
btnClose.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
System.exit(-1);
}
});
getContentPane().add(btnClose);
}
private void addBtnLoginActionListener() {
String user = t_Id.getText();
String password = new String(t_pw.getPassword());
if (user.equals("")) {
JOptionPane.showMessageDialog(this, "帐号不可为空", "Caution",
JOptionPane.WARNING_MESSAGE);
return;
}
if (password.equals("")) {
JOptionPane.showMessageDialog(this, "密码不可为空", "Caution",
JOptionPane.WARNING_MESSAGE);
return;
}
String sql = "select * FROM login WHERE id = '" + user + "' and pw = '"
+ password + "'";
boolean success = false;
// TODO:数据校验 success = executeQuery(sql);
if (success) {
// TODO: 如果数据校验成功 显示主界面 并关闭登录界面
JOptionPane.showMessageDialog(this, "成功登录", "提示",
JOptionPane.INFORMATION_MESSAGE);
this.dispose();
} else {
JOptionPane.showMessageDialog(this, "帐号或密码错误!", "警告",
JOptionPane.WARNING_MESSAGE);
t_pw.requestFocus(); // 密码框选中
}
}
public Dimension getPreferredSize() {
return new Dimension(320, 170);
}
public void show() {
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension screen = tk.getScreenSize();
Dimension d = getSize();
this.setLocation((screen.width - d.width) / 2,
(screen.height - d.height) / 2);
// 输入密码后回车相当于点击了登录按钮
getRootPane().setDefaultButton(btnLogin);
t_pw.requestFocus();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setSize(300, 220);
super.show();
}
public static void main(String[] args) {
vv loginFrame = new vv();
loginFrame.setVisible(true);
}
}
希望对你有帮助
用JAVA实现的图形编辑器
楼主给你一个我的,直接保存成pb.java编译运行,就是你要的画图功能 ,可以参考一下
____________________________________________________________________
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import java.awt.geom.*;
import java.io.*;
class Point implements Serializable
{
int x,y;
Color col;
int tool;
int boarder;
Point(int x, int y, Color col, int tool, int boarder)
{
this.x = x;
this.y = y;
this.col = col;
this.tool = tool;
this.boarder = boarder;
}
}
class paintboard extends Frame implements ActionListener,MouseMotionListener,MouseListener,ItemListener
{
int x = -1, y = -1;
int con = 1;//画笔大小
int Econ = 5;//橡皮大小
int toolFlag = 0;//toolFlag:工具标记
//toolFlag工具对应表:
//(0--画笔);(1--橡皮);(2--清除);
//(3--直线);(4--圆);(5--矩形);
Color c = new Color(0,0,0); //画笔颜色
BasicStroke size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);//画笔粗细
Point cutflag = new Point(-1, -1, c, 6, con);//截断标志
Vector paintInfo = null;//点信息向量组
int n = 1;
FileInputStream picIn = null;
FileOutputStream picOut = null;
ObjectInputStream VIn = null;
ObjectOutputStream VOut = null;
// *工具面板--画笔,直线,圆,矩形,多边形,橡皮,清除*/
Panel toolPanel;
Button eraser, drLine,drCircle,drRect;
Button clear ,pen;
Choice ColChoice,SizeChoice,EraserChoice;
Button colchooser;
Label 颜色,大小B,大小E;
//保存功能
Button openPic,savePic;
FileDialog openPicture,savePicture;
paintboard(String s)
{
super(s);
addMouseMotionListener(this);
addMouseListener(this);
paintInfo = new Vector();
/*各工具按钮及选择项*/
//颜色选择
ColChoice = new Choice();
ColChoice.add("black");
ColChoice.add("red");
ColChoice.add("blue");
ColChoice.add("green");
ColChoice.addItemListener(this);
//画笔大小选择
SizeChoice = new Choice();
SizeChoice.add("1");
SizeChoice.add("3");
SizeChoice.add("5");
SizeChoice.add("7");
SizeChoice.add("9");
SizeChoice.addItemListener(this);
//橡皮大小选择
EraserChoice = new Choice();
EraserChoice.add("5");
EraserChoice.add("9");
EraserChoice.add("13");
EraserChoice.add("17");
EraserChoice.addItemListener(this);
////////////////////////////////////////////////////
toolPanel = new Panel();
clear = new Button("清除");
eraser = new Button("橡皮");
pen = new Button("画笔");
drLine = new Button("画直线");
drCircle = new Button("画圆形");
drRect = new Button("画矩形");
openPic = new Button("打开图画");
savePic = new Button("保存图画");
colchooser = new Button("显示调色板");
//各组件事件监听
clear.addActionListener(this);
eraser.addActionListener(this);
pen.addActionListener(this);
drLine.addActionListener(this);
drCircle.addActionListener(this);
drRect.addActionListener(this);
openPic.addActionListener(this);
savePic.addActionListener(this);
colchooser.addActionListener(this);
颜色 = new Label("画笔颜色",Label.CENTER);
大小B = new Label("画笔大小",Label.CENTER);
大小E = new Label("橡皮大小",Label.CENTER);
//面板添加组件
toolPanel.add(openPic);
toolPanel.add(savePic);
toolPanel.add(pen);
toolPanel.add(drLine);
toolPanel.add(drCircle);
toolPanel.add(drRect);
toolPanel.add(颜色); toolPanel.add(ColChoice);
toolPanel.add(大小B); toolPanel.add(SizeChoice);
toolPanel.add(colchooser);
toolPanel.add(eraser);
toolPanel.add(大小E); toolPanel.add(EraserChoice);
toolPanel.add(clear);
//工具面板到APPLET面板
add(toolPanel,BorderLayout.NORTH);
setBounds(60,60,900,600); setVisible(true);
validate();
//dialog for save and load
openPicture = new FileDialog(this,"打开图画",FileDialog.LOAD);
openPicture.setVisible(false);
savePicture = new FileDialog(this,"保存图画",FileDialog.SAVE);
savePicture.setVisible(false);
openPicture.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ openPicture.setVisible(false); }
});
savePicture.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ savePicture.setVisible(false); }
});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ System.exit(0);}
});
}
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
Point p1,p2;
n = paintInfo.size();
if(toolFlag==2)
g.clearRect(0,0,getSize().width,getSize().height);//清除
for(int i=0; in ;i++){
p1 = (Point)paintInfo.elementAt(i);
p2 = (Point)paintInfo.elementAt(i+1);
size = new BasicStroke(p1.boarder,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
g2d.setColor(p1.col);
g2d.setStroke(size);
if(p1.tool==p2.tool)
{
switch(p1.tool)
{
case 0://画笔
Line2D line1 = new Line2D.Double(p1.x, p1.y, p2.x, p2.y);
g2d.draw(line1);
break;
case 1://橡皮
g.clearRect(p1.x, p1.y, p1.boarder, p1.boarder);
break;
case 3://画直线
Line2D line2 = new Line2D.Double(p1.x, p1.y, p2.x, p2.y);
g2d.draw(line2);
break;
case 4://画圆
Ellipse2D ellipse = new Ellipse2D.Double(p1.x, p1.y, Math.abs(p2.x-p1.x) , Math.abs(p2.y-p1.y));
g2d.draw(ellipse);
break;
case 5://画矩形
Rectangle2D rect = new Rectangle2D.Double(p1.x, p1.y, Math.abs(p2.x-p1.x) , Math.abs(p2.y-p1.y));
g2d.draw(rect);
break;
case 6://截断,跳过
i=i+1;
break;
default :
}//end switch
}//end if
}//end for
}
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==ColChoice)//预选颜色
{
String name = ColChoice.getSelectedItem();
if(name=="black")
{c = new Color(0,0,0); }
else if(name=="red")
{c = new Color(255,0,0);}
else if(name=="green")
{c = new Color(0,255,0);}
else if(name=="blue")
{c = new Color(0,0,255);}
}
else if(e.getSource()==SizeChoice)//画笔大小
{
String selected = SizeChoice.getSelectedItem();
if(selected=="1")
{
con = 1;
size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
}
else if(selected=="3")
{
con = 3;
size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
}
else if(selected=="5")
{con = 5;
size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
}
else if(selected=="7")
{con = 7;
size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
}
else if(selected=="9")
{con = 9;
size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
}
}
else if(e.getSource()==EraserChoice)//橡皮大小
{
String Esize = EraserChoice.getSelectedItem();
if(Esize=="5")
{ Econ = 5*2; }
else if(Esize=="9")
{ Econ = 9*2; }
else if(Esize=="13")
{ Econ = 13*2; }
else if(Esize=="17")
{ Econ = 17*3; }
}
}
public void mouseDragged(MouseEvent e)
{
Point p1 ;
switch(toolFlag){
case 0://画笔
x = (int)e.getX();
y = (int)e.getY();
p1 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p1);
repaint();
break;
case 1://橡皮
x = (int)e.getX();
y = (int)e.getY();
p1 = new Point(x, y, null, toolFlag, Econ);
paintInfo.addElement(p1);
repaint();
break;
default :
}
}
public void mouseMoved(MouseEvent e) {}
public void update(Graphics g)
{
paint(g);
}
public void mousePressed(MouseEvent e)
{
Point p2;
switch(toolFlag){
case 3://直线
x = (int)e.getX();
y = (int)e.getY();
p2 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p2);
break;
case 4: //圆
x = (int)e.getX();
y = (int)e.getY();
p2 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p2);
break;
case 5: //矩形
x = (int)e.getX();
y = (int)e.getY();
p2 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p2);
break;
default :
}
}
public void mouseReleased(MouseEvent e)
{
Point p3;
switch(toolFlag){
case 0://画笔
paintInfo.addElement(cutflag);
break;
case 1: //eraser
paintInfo.addElement(cutflag);
break;
case 3://直线
x = (int)e.getX();
y = (int)e.getY();
p3 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p3);
paintInfo.addElement(cutflag);
repaint();
break;
case 4: //圆
x = (int)e.getX();
y = (int)e.getY();
p3 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p3);
paintInfo.addElement(cutflag);
repaint();
break;
case 5: //矩形
x = (int)e.getX();
y = (int)e.getY();
p3 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p3);
paintInfo.addElement(cutflag);
repaint();
break;
default:
}
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==pen)//画笔
{toolFlag = 0;}
if(e.getSource()==eraser)//橡皮
{toolFlag = 1;}
if(e.getSource()==clear)//清除
{
toolFlag = 2;
paintInfo.removeAllElements();
repaint();
}
if(e.getSource()==drLine)//画线
{toolFlag = 3;}
if(e.getSource()==drCircle)//画圆
{toolFlag = 4;}
if(e.getSource()==drRect)//画矩形
{toolFlag = 5;}
if(e.getSource()==colchooser)//调色板
{
Color newColor = JColorChooser.showDialog(this,"调色板",c);
c = newColor;
}
if(e.getSource()==openPic)//打开图画
{
openPicture.setVisible(true);
if(openPicture.getFile()!=null)
{
int tempflag;
tempflag = toolFlag;
toolFlag = 2 ;
repaint();
try{
paintInfo.removeAllElements();
File filein = new File(openPicture.getDirectory(),openPicture.getFile());
picIn = new FileInputStream(filein);
VIn = new ObjectInputStream(picIn);
paintInfo = (Vector)VIn.readObject();
VIn.close();
repaint();
toolFlag = tempflag;
}
catch(ClassNotFoundException IOe2)
{
repaint();
toolFlag = tempflag;
System.out.println("can not read object");
}
catch(IOException IOe)
{
repaint();
toolFlag = tempflag;
System.out.println("can not read file");
}
}
}
if(e.getSource()==savePic)//保存图画
{
savePicture.setVisible(true);
try{
File fileout = new File(savePicture.getDirectory(),savePicture.getFile());
picOut = new FileOutputStream(fileout);
VOut = new ObjectOutputStream(picOut);
VOut.writeObject(paintInfo);
VOut.close();
}
catch(IOException IOe)
{
System.out.println("can not write object");
}
}
}
}//end paintboard
public class pb
{
public static void main(String args[])
{ new paintboard("画图程序"); }
}
网页标题:java图形编辑系统代码,Java图形界面编程
链接地址:http://azwzsj.com/article/dsgsgsi.html