当前位置:首页 > 代码 > 正文

java图片编辑器代码(java编程代码图片)[20240428更新]

admin 发布:2024-04-28 01:43 109


本篇文章给大家谈谈java图片编辑器代码,以及java编程代码图片对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

用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编写一个图形应用程序,可以是一个简单的文本编辑器、计算器等等。 求完整代码

//百度文库找的,可费了我的财富值了,你可要把分给我呀。

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 javax.swing.*;

import java.awt.event.*;

import javax.swing.event.*;

import java.util.*; //Date needed

import java.io.PrintWriter;

public class NotePad extends JFrame

{

JTextArea jta;

class newl implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

jta.setText("");

}

}

class openl implements ActionListener

{ public void actionPerformed(ActionEvent e)

{

JFileChooser jf=new JFileChooser();

jf.showOpenDialog(NotePad.this);

}

}

//保存文件的监听

class savel implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

JFileChooser jf = new JFileChooser();

jf.showSaveDialog(NotePad.this);

}

}

//打印的监听 ?

class printl implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

// PrintWriter p = new PrintWriter(NotePad.this);

}

}

//退出记事本的监听

class exitl implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

System.exit(0);//退出

}

}

//拷贝的监听

class copyl implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

jta.copy();

}

}

//粘贴的监听

class pastel implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

jta.paste();

}

}

//剪切的监听

class cutl implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

jta.cut();

}

}

//查找的监听

//添加日期的监听

class datel implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

Date d=new Date();

jta.append(d.toString());

}

}

//构造函数

public NotePad()

{

jta=new JTextArea("",24,40);

JScrollPane jsp=new JScrollPane(jta);

JMenuBar jmb=new JMenuBar();

JMenu mFile=new JMenu("File");

JMenu mEdit=new JMenu("Edit");

JMenuItem mNew=new JMenuItem("New",KeyEvent.VK_N);

mNew.addActionListener(new newl());

mFile.add(mNew);

JMenuItem mOpen=new JMenuItem("Open",KeyEvent.VK_O);

mOpen.addActionListener(new openl());

mFile.add(mOpen);

JMenuItem mSave=new JMenuItem("Save");

mSave.addActionListener(new savel());

mFile.add(mSave);

mFile.addSeparator(); //添加分割线

JMenuItem mPrint = new JMenuItem("Print");

mPrint.addActionListener(new printl());

mFile.add(mPrint);

mFile.addSeparator(); //添加分割线

JMenuItem mExit=new JMenuItem("Exit");

mExit.addActionListener(new exitl());

mFile.add(mExit);

mFile.setMnemonic(KeyEvent.VK_F);

//编辑菜单的子菜单的处理

JMenuItem jmi;

jmi=new JMenuItem("Copy");

jmi.addActionListener(new copyl());

mEdit.add(jmi);

jmi=new JMenuItem("Cut");

jmi.addActionListener(new cutl());

mEdit.add(jmi);

jmi=new JMenuItem("Paste");

jmi.addActionListener(new pastel());

mEdit.add(jmi);

mEdit.addSeparator(); //添加分割线

jmi=new JMenuItem("Find");

mEdit.add(jmi);

jmi=new JMenuItem("FindNext");

mEdit.add(jmi);

mEdit.addSeparator();

jmi=new JMenuItem("Select All");

mEdit.add(jmi);

jmi=new JMenuItem("Date/Time");

jmi.addActionListener(new datel());

mEdit.add(jmi);

jmb.add(mFile);

jmb.add(mEdit);

this.setJMenuBar(jmb);

this.getContentPane().add(jsp);

this.setSize(200,200);

this.setVisible(true);

}

//主函数,程序入口点

public static void main(String s[])

{

new NotePad();

}

}

我用Java写了一个图像编辑器,实现图像的模糊和锐化,但是每次转化后保存图片到桌面,保存的图片和显示的

ConvolveOp 这个。。不懂。

你是不是保存为jpg的格式的,换成png吧,或者其他库也行,开源库很多。

参考:编程中国风

参考网站: 

测试可用的源码:

求一个用Java编写的文本编辑器代码

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.io.*;

public class MyTextEditor extends JFrame implements ActionListener,ItemListener,MouseListener

{

private File file;

private JTextArea textarea;

private JRadioButtonMenuItem rbmi_red,rbmi_blue,rbmi_green;

private JMenuItem menuitem_copy,menuitem_cut,menuitem_paste,menuitem_seek;

private JMenuItem menuitem_song,menuitem_fang,menuitem_kai;//字体变量

private JMenuItem menuitem_normal,menuitem_bold,menuitem_italic;//字形变量

private JMenuItem menuitem_20,menuitem_30,menuitem_40;//字号变量

private JMenuItem menuitem_exit,menuitem_infor;

private JPopupMenu popupmenu;

private JMenuItem menuitem_red,menuitem_green,menuitem_blue;

private JDialog dialog,dialog1;

private JButton button_seek;

private JTextField textfield_seek;

private JLabel label_seek,label_infor;

String seek;

public MyTextEditor()

{

super("文本编辑器");

this.setSize(400,300);

this.setLocation(360,300);

this.setDefaultCloseOperation(HIDE_ON_CLOSE);

Container ss=this.getContentPane();

this.textarea = new JTextArea();

JScrollPane dd=new JScrollPane(textarea);

ss.add(dd);

textarea.addMouseListener(this);

this.addMenu();

this.setVisible(true);

this.Dialog();

this.Dialog1();

this.file = null;

}

public MyTextEditor(String filename)

{

this();

if (filename!=null)

{

this.file = new File(filename);

this.setTitle(filename);

this.textarea.setText(this.readFromFile());

}

}

public MyTextEditor(File file)

{

this();

if (file!=null)

{

this.file = file;

this.setTitle(this.file.getName());

this.textarea.setText(this.readFromFile());

}

}

public void Dialog() //建立对话框的方法

{

dialog=new JDialog(this,"查找",true);

dialog.setLayout(new FlowLayout());

dialog.setSize(200,90);

label_seek=new JLabel("查找内容");

dialog.add(label_seek);

textfield_seek=new JTextField(10);

dialog.add(textfield_seek);

button_seek=new JButton("查找");

dialog.add(button_seek);

button_seek.addActionListener(this);

}

public void Dialog1()

{

dialog1=new JDialog(this,"专利",true);

dialog1.setLayout(new FlowLayout());

dialog1.setSize(200,100);

label_infor=new JLabel("刘同虎制作");

dialog1.add(label_infor);

}

public void addMenu()

{

JMenuBar menubar = new JMenuBar();

this.setJMenuBar(menubar);

JMenu menu_file = new JMenu("文件"); //文件菜单

menubar.add(menu_file);

JMenuItem menuitem_open = new JMenuItem("打开");

menu_file.add(menuitem_open);

menuitem_open.addActionListener(this);

JMenuItem menuitem_save = new JMenuItem("保存");

menu_file.add(menuitem_save);

menuitem_save.addActionListener(this);

JMenuItem menuitem_saveas = new JMenuItem("另存为");

menu_file.add(menuitem_saveas);

menuitem_saveas.addActionListener(this);

menuitem_exit=new JMenuItem("退出" );

menu_file.add(menuitem_exit);

menuitem_exit.addActionListener(this);

menuitem_infor=new JMenuItem("信息");

menu_file.add(menuitem_infor);

menuitem_infor.addActionListener(this);

JMenu menu_editor=new JMenu("编辑");//编辑菜单

menubar.add(menu_editor);

menuitem_seek=new JMenuItem("查找");

menu_editor.add(menuitem_seek);

menuitem_seek.addActionListener(this);

menuitem_copy=new JMenuItem("复制");

menuitem_copy.addActionListener(this);

menu_editor.add(menuitem_copy);

menuitem_cut=new JMenuItem("剪切");

menu_editor.add(menuitem_cut);

menuitem_cut.addActionListener(this);

menuitem_paste=new JMenuItem("粘贴");

menu_editor.add(menuitem_paste);

menuitem_paste.addActionListener(this);

JMenuItem menu_color=new JMenu("颜色");//颜色菜单

menu_editor.add(menu_color);

ButtonGroup buttongroup=new ButtonGroup();

rbmi_red=new JRadioButtonMenuItem("红",true);

buttongroup.add(rbmi_red);

menu_color.add(rbmi_red);

rbmi_red.addItemListener(this);

rbmi_blue=new JRadioButtonMenuItem("蓝",true);

buttongroup.add(rbmi_blue);

menu_color.add(rbmi_blue);

rbmi_blue.addItemListener(this);

rbmi_green=new JRadioButtonMenuItem("绿",true);

buttongroup.add(rbmi_green);

menu_color.add(rbmi_green);

rbmi_green.addItemListener(this);

JMenu menu_font=new JMenu("设置字体");//设置字体菜单

menubar.add(menu_font);

menuitem_song=new JMenuItem("宋体");

menu_font.add(menuitem_song);

menuitem_song.addActionListener(this);

menuitem_fang=new JMenuItem("仿宋");

menu_font.add(menuitem_fang);

menuitem_fang.addActionListener(this);

menuitem_kai=new JMenuItem("楷体");

menu_font.add(menuitem_kai);

menuitem_kai.addActionListener(this);

JMenu menu_style=new JMenu("设置字形");//设置字形菜单

menubar.add(menu_style);

menuitem_bold=new JMenuItem("粗体");

menu_style.add(menuitem_bold);

menuitem_bold.addActionListener(this);

menuitem_italic=new JMenuItem("斜体");

menu_style.add(menuitem_italic);

menuitem_italic.addActionListener(this);

JMenu menu_size=new JMenu("设置字号"); //设置字号菜单

menubar.add(menu_size);

menuitem_20=new JMenuItem("20");

menu_size.add(menuitem_20);

menuitem_20.addActionListener(this);

menuitem_30=new JMenuItem("30");

menu_size.add(menuitem_30);

menuitem_30.addActionListener(this);

menuitem_40=new JMenuItem("40");

menu_size.add(menuitem_40);

menuitem_40.addActionListener(this);

popupmenu=new JPopupMenu(); //快捷菜单

JMenuItem menuitem_red=new JMenuItem("红色");

popupmenu.add(menuitem_red);

menuitem_red.addActionListener(this);

JMenuItem menuitem_green=new JMenuItem("绿色");

popupmenu.add(menuitem_green);

menuitem_green.addActionListener(this);

menuitem_blue=new JMenuItem("蓝色");

popupmenu.add(menuitem_blue);

menuitem_blue.addActionListener(this);

textarea.add(popupmenu); //向文本区内添加快捷菜单

}

public void writeToFile(String lines) //写文件

{

try

{

FileWriter fout = new FileWriter(this.file);

fout.write(lines+"\r\n");

fout.close();

}

catch (IOException ioex)

{

return;

}

}

public String readFromFile() //读文件

{

try

{

FileReader fin = new FileReader(this.file);

BufferedReader bin = new BufferedReader(fin);

String aline="", lines="";

do

{

aline = bin.readLine();

if (aline!=null)

lines += aline + "\r\n";

} while (aline!=null);

bin.close();

fin.close();

return lines;

}

catch (IOException ioex)

{

return null;

}

}

关于java图片编辑器代码和java编程代码图片的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://www.ahzz.com.cn/post/731.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载