📄 main.java
字号:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JColorChooser;
import javax.swing.JOptionPane;
import java.awt.geom.*;
import com.sun.image.codec.jpeg.*;
import java.awt.image.*;
import java.io.*;
import java.util.*;
class Window extends Frame implements ActionListener
{String path1=new String("1");
MenuBar menubar;
Menu menu;
MyCanvas can;
Graphics g;
MenuItem a,b,c;
FileDialog filed,files;
Image image;
class panell extends Panel implements ItemListener
{Checkbox box1,box2,box3,box4,box5;
Choice choice;
Color backcolor;
int typebox=0;
int typebox1=0;
String name=new String("1");
CheckboxGroup sex1;
panell()
{
sex1=new CheckboxGroup();
box4=new Checkbox("填充",false,sex1);
box5=new Checkbox("不填充",true,sex1);
choice=new Choice();
choice.add("1");
choice.add("2");
choice.add("3");
choice.add("4");
choice.add("5");
add(box4);
add(box5);
add(choice);
setBackground(Color.pink);
backcolor=getBackground();
box4.addItemListener(this);
box5.addItemListener(this);
choice.addItemListener(this);
}
public void itemStateChanged(ItemEvent e)//获取选择的颜色和填充模式
{
if(e.getSource()==box4)
{ typebox1=4;
}
else if(e.getSource()==box5)
{ typebox1=5;
}
else if(e.getSource()==choice)
{ name=choice.getSelectedItem();
}
}
}
class panel extends Panel implements ActionListener//画板
{
Button button1,button2,button3,button4,button5,button6,button7,button8;
Color backcolor;
int typebutton=0;
panel()
{
button1=new Button("直线");
button2=new Button("矩形");
button3=new Button("圆角矩形");
button4=new Button("椭圆");
button5=new Button("圆");
button6=new Button("圆弧");
button7=new Button("颜色板");
button8=new Button("画笔");
add(button1);
add(button2);
add(button3);
add(button4);
add(button5);
add(button6);
add(button7);
add(button8);
setBackground(Color.pink);
backcolor=getBackground();
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
}
public void actionPerformed(ActionEvent e)//判断鼠标点击那个按钮
{
if(e.getSource()==button1)
{typebutton=1;
}
else if(e.getSource()==button2)
{typebutton=2;
}
else if(e.getSource()==button3)
{typebutton=3;
}
else if(e.getSource()==button4)
{typebutton=4;
}
else if(e.getSource()==button5)
{typebutton=5;
}
else if(e.getSource()==button6)
{typebutton=6;
}
else if(e.getSource()==button7)
{typebutton=7;
}
else if(e.getSource()==button8)
{typebutton=8;
}
}
}
class MyCanvas extends Canvas//画布
implements MouseListener
{panel pan;
panell panl;
BufferedImage image;
int typebutton=1;
int x2=-1,y2=-1;
int x=-1,y=-1;
int x1=-1,y1=-1;
int typebox=0;
int k=0;
int d=0;
String path=new String("1");
Toolkit tool;
Image image1;
Graphics2D g2D;
Vector v=new Vector();
Point p;
Color color;
MyCanvas()
{setSize(200,200);
tool=getToolkit();
image=new BufferedImage(600,600,BufferedImage.TYPE_INT_RGB);
g2D=image.createGraphics();
image1=tool.getImage(path);
g2D.setColor(Color.white);
g2D.fillRect(0,0,600,600);
pan=new panel();
panl=new panell();
p=new Point(0,0);
typebutton=1;
g2D.setColor(Color.black);
addMouseListener(this);
addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{if(pan.typebutton==8)
{d=pan.typebutton;
x2=(int)e.getX();y2=(int)e.getY();
Point p=new Point(x2,y2);
v.addElement(p);
if(x!=-1&&y!=-1)
{
for(int i=0;i<v.size()-1;i++)
{
Point p1=(Point)v.elementAt(i);
Point p2=(Point)v.elementAt(i+1);
g2D.drawLine(p1.x,p1.y,p2.x,p2.y);
}
repaint();
}}
if(pan.typebutton==1&&k==1)//直线
{d=pan.typebutton;
if(x1!=-1)
g2D.drawLine(x,y,x1,y1);
k=0;
}
else if(pan.typebutton==2&&k==1)//矩形
{ d=pan.typebutton;
int i,j;
i=x1-x;
j=y1-y;
if(i>=0&&j>=0){
if(panl.typebox1==4)
{g2D.fillRect(x,y,i,j);
}
else g2D.drawRect(x,y,i,j);}
else{
if(panl.typebox1==4)
{g2D.fillRect(x1,y1,Math.abs(i),Math.abs(j));
}
else g2D.drawRect(x1,y1,Math.abs(i),Math.abs(j));}
k=0;
}
else if(pan.typebutton==3&&k==1)//圆角矩形
{d=pan.typebutton;
int i,j;
i=x1-x;
j=y1-y;
if(i>=0&&j>=0){
if(panl.typebox1==4)
{g2D.fillRoundRect(x,y,i,j,20,20);
}
else g2D.drawRoundRect(x,y,i,j,20,20);}
else{
if(panl.typebox1==4)
{g2D.fillRoundRect(x1,y1,Math.abs(i),Math.abs(j),20,20);
}
else g2D.drawRoundRect(x1,y1,Math.abs(i),Math.abs(j),20,20);}
k=0;
}
else if(pan.typebutton==4&&k==1)//椭圆
{d=pan.typebutton;
int i,j;
i=x1-x;
j=y1-y;
if(i>=0&&j>=0){
if(panl.typebox1==4)
{g2D.fillOval(x,y,i,j);
}
else g2D.drawOval(x,y,i,j); }
else{
if(panl.typebox1==4)
{g2D.fillOval(x1,y1,Math.abs(i),Math.abs(j));
}
else g2D.drawOval(x1,y1,Math.abs(i),Math.abs(j)); }
k=0;
}
else if(pan.typebutton==5&&k==1)//圆
{d=pan.typebutton;
int i;
i=x1-x;
if(i>=0){
if(panl.typebox1==4)
{g2D.fillOval(x,y,i,i);
}
else g2D.drawOval(x,y,i,i);}
else{
if(panl.typebox1==4)
{g2D.fillOval(x1,y1,Math.abs(i),Math.abs(i));
}
else g2D.drawOval(x1,y1,Math.abs(i),Math.abs(i));}
k=0;
}
else if(pan.typebutton==6&&k==1)//圆弧
{d=pan.typebutton;
int i;
i=x1-x;
if(i>=0)
{g2D.drawArc(x,y,i,i,60,60);
}
else
{g2D.drawArc(x1,y1,Math.abs(i),Math.abs(i),60,60);
}
k=0;
}
}
}
);
addMouseMotionListener(new MouseMotionAdapter()
{
public void mousePressed(MouseEvent e)
{
x=e.getX();
y=e.getY();
k=1;
}
}
);
addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseReleased(MouseEvent e){
x1=e.getX();
y1=e.getY();
v.removeAllElements();
repaint();
}
});
}
public void paint(Graphics g)
{
BasicStroke b1=new BasicStroke
(2,BasicStroke.CAP_SQUARE,BasicStroke.JOIN_MITER);
BasicStroke b2=new BasicStroke
(4,BasicStroke.CAP_SQUARE,BasicStroke.JOIN_MITER);
BasicStroke b3=new BasicStroke
(6,BasicStroke.CAP_SQUARE,BasicStroke.JOIN_MITER);
BasicStroke b4=new BasicStroke
(8,BasicStroke.CAP_SQUARE,BasicStroke.JOIN_MITER);
g.drawImage(image,0,0,this);
if(path!="1")
{g.drawImage(image1,0,0,this);
}
if(panl.name=="1"){}
else if(panl.name=="2")
{g2D.setStroke(b1);}
else if(panl.name=="3")
{g2D.setStroke(b2);}
else if(panl.name=="4")
{g2D.setStroke(b3);}
else if(panl.name=="5")
{g2D.setStroke(b4);}
else if(pan.typebutton==7)//颜色板
{
color=JColorChooser.showDialog(pan,"颜色板",pan.button8.getBackground());
g2D.setColor(color);
pan.typebutton=d;
}
else if(pan.typebutton==8)//画笔
{
}
}
public Image getImage()
{return image;
}
public void mousePressed(MouseEvent e)
{
x=e.getX();
y=e.getY();
k=1;
}
public void mouseReleased(MouseEvent e){
x1=e.getX();
y1=e.getY();
v.removeAllElements();
repaint();
}
public void mouseEntered(MouseEvent e){repaint();}
public void mouseExited(MouseEvent e)
{
repaint();
}
public void mouseClicked(MouseEvent e){}
public void update(Graphics g)
{if(typebutton!=0)
{paint(g);
}
else
{super.update(g);
}
}
}
Window()
{
can=new MyCanvas();
menubar=new MenuBar();
menu=new Menu("文件");
a=new MenuItem("打开");
b=new MenuItem("保存");
c=new MenuItem("退出");
filed=new FileDialog(this,"打开文件对话框",FileDialog.LOAD);
filed.setVisible(false);
files=new FileDialog(this,"保存文件对话框",FileDialog.SAVE);
files.setVisible(false);
menu.add(a);
menu.add(b);
menu.add(c);
menubar.add(menu);
setMenuBar(menubar);
setBounds(200,100,600,600);
add(can.panl,BorderLayout.SOUTH);
add(can.pan,BorderLayout.NORTH);
setVisible(true);
add(can,BorderLayout.CENTER);
validate();
a.addActionListener(this);
b.addActionListener(this);
c.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()==a)
{filed.setVisible(true);
path1=filed.getDirectory();
String name=filed.getFile();
int w=can.getWidth();
int h=can.getHeight();
g=can.g2D;
image=Toolkit.getDefaultToolkit().getImage(path1+"/"+name);
g.drawImage(image,0,0,w,h,can);
}
else if(e.getSource()==b)
{files.setVisible(true);
path1=files.getDirectory();
String name=filed.getFile();
try{JPEGImageEncoder encoder=
JPEGCodec.createJPEGEncoder(new FileOutputStream(path1+"a.jpg"));
encoder.encode(can.image);
}
catch(Exception ee){}
}
else if(e.getSource()==c)
{System.exit(0);}
}
}
public class Main {
public Main() { }
public static void main(String[] args) {
Window f=new Window();
f.addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);}
});
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -