📄 lable.java
字号:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
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 java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.*;
import java.util.ArrayList;
import javax.swing.*;
public class lable extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* @param args
*/
private double px,py,px2,py2;
boolean drawn=false;
int status=0;
Graphics g3=getGraphics();
Graphics2D g2=(Graphics2D) g3;
public String command="";
private ArrayList<Rectangle2D>juxing;
private ArrayList<Ellipse2D>tuoyuan;
private ArrayList<Line2D>zhixian;
private ArrayList<Line2D>qianbi;
private Rectangle2D currentju;
private Ellipse2D currenttuo;
private Line2D currentzhi;
private Line2D currentqian;
private JColorChooser chose;
public static final int WIDTH = 700;
public static final int HEIGHT = 500;
public static final int width = 400;
public static final int height = 200;
public static final int VERTICAL_STRUT_SIZE = 10;
public static final int LINES = 15;
public static final int CHAR_PER_LINE = 13;
public static final int CHAR_LINE = 5;
private JTextArea theText;
private JTextArea inputText;
private String memo1 = "No Memo 1.";
private String memo2 = "No Memo 2.";
public static void main(String[] args) {
// TODO Auto-generated method stub
lable guiAdder = new lable( );
guiAdder.setVisible(true);
}
public lable(){
setTitle("sun 涂鸦");
addWindowListener(new WindowDestroyer( ));
setSize(WIDTH, HEIGHT);
Container contentPane = getContentPane( );
contentPane.setLayout(new BorderLayout( ));
JMenu memoMenu = new JMenu("文件");
JMenuItem m;
m = new JMenuItem("新建");
m.addActionListener(this);
memoMenu.add(m);
m = new JMenuItem("保存");
m.addActionListener(this);
memoMenu.add(m);
m = new JMenuItem("另存为");
m.addActionListener(this);
memoMenu.add(m);
m = new JMenuItem("Get Memo 2");
m.addActionListener(this);
memoMenu.add(m);
m = new JMenuItem("Clear");
m.addActionListener(this);
memoMenu.add(m);
m = new JMenuItem("Exit");
m.addActionListener(this);
memoMenu.add(m);
JMenuBar mBar = new JMenuBar();
mBar.add(memoMenu);
contentPane.add(mBar, BorderLayout.NORTH);
JPanel buttonPaneone = new JPanel( );
buttonPaneone.setBackground(Color.ORANGE);
// buttonPaneone.setLayout(new FlowLayout( ));
Box leftBox = Box.createVerticalBox( );
Component leftStrut = Box.createVerticalStrut(VERTICAL_STRUT_SIZE);
leftBox.add(leftStrut);
JPanel textPanel = new JPanel();
textPanel.setLayout(new BorderLayout( ));
textPanel.setBackground(Color.BLUE);
theText = new JTextArea(LINES, CHAR_PER_LINE);
theText.setText("Menu bar is at the bottom.");
theText.setBackground(Color.WHITE);
theText.setLineWrap(true);
JScrollPane scrolledText = new JScrollPane(theText);
scrolledText.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrolledText.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
textPanel.add(scrolledText,BorderLayout.CENTER);
// textPanel.add(theText);
//contentPane.add(textPanel, BorderLayout.CENTER);
//leftBox.add(textPanel);
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new BorderLayout( ));
inputPanel.setBackground(Color.BLUE);
inputText = new JTextArea(CHAR_LINE, CHAR_PER_LINE);
inputText.setText(" ");
inputText.setBackground(Color.WHITE);
// JMenu inputMenu = new JMenu("文件");
//JMenuItem in;
// JMenuBar inBar = new JMenuBar();
// inBar.add(inputMenu);
//inputPanel.add(inBar, BorderLayout.NORTH);
JToolBar tool=new JToolBar();
ImageIcon my =new ImageIcon("title.gif");
JButton open=new JButton(my);
my=new ImageIcon("hi.gif");
JButton mail=new JButton(my);
tool.add(open);
tool.add(mail);
inputPanel.add("North",tool);
inputText.setLineWrap(true);
JScrollPane InputText = new JScrollPane(inputText);
InputText.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
InputText.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
inputPanel.add(InputText,BorderLayout.CENTER);
leftBox.add(textPanel);
leftBox.add(inputPanel);
contentPane.add(leftBox, BorderLayout.WEST);
//涂鸦区
// JPanel buttonPanetwo = new JPanel( );
//buttonPanetwo.setBackground(Color.white);
// buttonPanetwo.setLayout(new FlowLayout( ));
Box centerBox = Box.createVerticalBox( );
Component centerStrut = Box.createVerticalStrut(VERTICAL_STRUT_SIZE);
centerBox.add(centerStrut);
final DrawPanel draw=new DrawPanel();
JToolBar Tool=new JToolBar();
//JPanel caidan=new JPanel();
JButton qianbi=new JButton("铅笔");
qianbi.addActionListener(this);
Tool.add(qianbi);
JButton zhixian=new JButton("直线");
zhixian.addActionListener(this);
Tool.add(zhixian);
JButton juxing=new JButton("矩形");
juxing.addActionListener(this);
Tool.add(juxing);
JButton tuoyuan=new JButton("椭圆");
tuoyuan.addActionListener(this);
Tool.add(tuoyuan);
JButton qingchu=new JButton("清除");
qingchu.addActionListener(this);
Tool.add(qingchu);
//draw.add(caidan,BorderLayout.NORTH);
centerBox.add(Tool);
centerBox.add(draw);
contentPane.add(centerBox, BorderLayout.CENTER);
JPanel buttonPanethree = new JPanel( );
buttonPanethree.setBackground(Color.YELLOW);
// buttonPanethree.setLayout(new FlowLayout( ));
Box rightBox = Box.createVerticalBox( );
Component rightStrut = Box.createVerticalStrut(VERTICAL_STRUT_SIZE);
rightBox.add(rightStrut);
rightBox.add(buttonPanethree );
JButton aButton = new JButton("笔触");
aButton.addActionListener(this);
rightBox.add(aButton);
JButton bButton = new JButton("颜色");
bButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Color board=JColorChooser.showDialog(draw, "自定义", draw.color);
draw.color=board;
draw.repaint();
}
});
rightBox.add(bButton);
JButton cButton = new JButton("图案");
cButton.addActionListener(this);
rightBox.add(cButton);
rightBox.add(buttonPanethree );
contentPane.add(rightBox, BorderLayout.EAST);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
command=e.getActionCommand();
if (e.getActionCommand( ).equals("笔触")){
pinting guiAdder = new pinting ( );
guiAdder.setVisible(true);
}
String actionCommand = e.getActionCommand();
if (actionCommand.equals("保存"))
memo1 = theText.getText();
else if (actionCommand.equals("新建"))
memo2 = theText.getText();
else if(command.equals("清除"))
{
repaint();
}
//else if(command.equals("颜色选择"))
// {
// color=chose.showDialog(this, "颜色选择", color);
// }
else if (actionCommand.equals("Clear"))
theText.setText("");
else if (actionCommand.equals("另存为"))
theText.setText(memo1);
else if (actionCommand.equals("Get Memo 2"))
theText.setText(memo2);
else if (actionCommand.equals("Exit"))
System.exit(0);
}
private class pinting extends JFrame
implements ActionListener{
private pinting(){
setTitle("笔触");
addWindowListener(new WindowDestroyer( ));
setSize(width , height);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}/*
private class pictuer extends JFrame
implements ActionListener{
public String command="";
private pictuer(){
setTitle("图案");
addWindowListener(new WindowDestroyer( ));
setSize(width , height);
Container contentPane=getContentPane();
JPanel caidan=new JPanel();
JButton qianbi=new JButton("铅笔");
qianbi.addActionListener(this);
caidan.add(qianbi);
JButton zhixian=new JButton("直线");
zhixian.addActionListener(this);
caidan.add(zhixian);
JButton juxing=new JButton("矩形");
juxing.addActionListener(this);
caidan.add(juxing);
JButton tuoyuan=new JButton("椭圆");
tuoyuan.addActionListener(this);
caidan.add(tuoyuan);
caidan.setLayout(new GridLayout(2,2));
contentPane.add(caidan);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
*/
public class DrawPanel extends JPanel implements MouseMotionListener,MouseListener
{
/**
*
*/
private Color color;
private static final long serialVersionUID = 1L;
public DrawPanel()
{
juxing =new ArrayList<Rectangle2D>();
currentju=null;
tuoyuan=new ArrayList<Ellipse2D>();
currenttuo=null;
zhixian=new ArrayList<Line2D>();
currentzhi=null;
qianbi=new ArrayList<Line2D>();
currentqian=null;
addMouseListener(this);
addMouseMotionListener(this);
}
//当调用REPAINT的时候 系统会自动调用这个方法进行绘图
//把所有需要画的工作都在这个里面完成 包括通过循环画出原来的 以及新画的
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(!command.equals("清除"))
{
Graphics2D g2=(Graphics2D)g;
for(Rectangle2D r:juxing)
{
g2.draw(r);
}
if(currentju!=null)
{
g2.draw(currentju);
}
for(Ellipse2D r:tuoyuan)
{
g2.draw(r);
}
if(currenttuo!=null)
{
g2.draw(currenttuo);}
for(Line2D r:zhixian)
{
g2.draw(r);
}
if(currentzhi!=null)
{
g2.draw(currentzhi);}
for(Line2D r:qianbi)
{
g2.draw(r);
}
}
else
{
juxing =new ArrayList<Rectangle2D>();
currentju=null;
tuoyuan=new ArrayList<Ellipse2D>();
currenttuo=null;
zhixian=new ArrayList<Line2D>();
currentzhi=null;
qianbi=new ArrayList<Line2D>();
currentqian=null;
}
}
//在MOUSEPRESSED里面做好图形 然后直接调用REPAINT 这样将直接把新画的图形和原有的图形一起画出来
//否则 新画的图形将不能够与原有的图形共存 即出现闪烁的情况
public void mousePressed(MouseEvent e)
{
px=e.getX();
py=e.getY();
}
public void mouseDragged(MouseEvent e)
{
if(command.equals("铅笔"))
{
Graphics g3=getGraphics();
Graphics2D g2=(Graphics2D) g3;
px2=e.getX();
py2=e.getY();
currentqian=new Line2D.Double(px,py,px2,py2);
g2.draw(currentqian);
px=px2;
py=py2;
qianbi.add(currentqian);
repaint();
}
else if (command.equals("直线"))
{
px2=e.getX();
py2=e.getY();
currentzhi=new Line2D.Double(px,py,px2,py2);
repaint();
}
else if (command.equals("矩形"))
{
px2=e.getX();
py2=e.getY();
currentju=new Rectangle2D.Double();
currentju.setFrameFromDiagonal(px,py,px2,py2);
repaint();
}
else if (command.equals("椭圆"))
{
px2=e.getX();
py2=e.getY();
currenttuo=new Ellipse2D.Double();
currenttuo.setFrameFromDiagonal(px,py,px2,py2);
repaint();
}
}
public void mouseMoved(MouseEvent e)
{}
public void mouseClicked(MouseEvent arg0)
{}
public void mouseEntered(MouseEvent arg0)
{}
public void mouseExited(MouseEvent arg0)
{}
//下面的方法是定义当松开鼠标时 图形应该定型 应当加入数组中去 同时在程序中应该留着图形 所以要再画一次
public void mouseReleased(MouseEvent e)
{
g3=getGraphics();
Graphics2D g2=(Graphics2D) g3;
g2.setColor(color);
if (command.equals("椭圆"))
{
tuoyuan.add(currenttuo);
g2.draw(currenttuo);
repaint();
}
else if (command.equals("矩形"))
{
juxing.add(currentju);
g2.draw(currentju);
repaint();
}
else if (command.equals("直线"))
{
zhixian.add(currentzhi);
g2.draw(currentzhi);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -