📄 j_painter.java
字号:
/////////////
//created by liaohongshen
//used for painting
///////////////////
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import javax.imageio.ImageIO;
import java.io.IOException;
//////////////////////
public class J_Painter extends JFrame
{
public J_Panel m_panel = new J_Panel();
public J_ToolPanel m_toolPanel = new J_ToolPanel();//添加两个面板用于作图和添加ToolBar
JButton []m_left_Button={new JButton("缩放")};
public J_LeftPanel m_leftPanel=new J_LeftPanel();
public static JFrame m_fatherFrame;
public static BufferedImage m_buffImage;
static Container con;
JMenuBar m_menuBar = new JMenuBar();
//构造图像画在m_panel上
PixelGrabber m_pixelGrabber; //用于抓取图像的象素值
JTextField m_textFieldX;
JTextField m_textFieldY; //用于向用户反馈当前的坐标值
JPopupMenu m_popupMenu = new JPopupMenu();
Cursor m_cursor; //自定义光标
int m_Width = 1024, m_Height = 768;
int m_currentpointx, m_currentpointy;
int m_clickedx, m_clickedy; //用户粘贴确定点
int rec_pixel[]; //记录用户选取的象素
int cordinate[] = new int[4];
public boolean b_freeWriting = true, b_rectangle = false,b_fill = false,
b_isExisteRecChoose = false, b_ellipse = false, b_line = false,
b_Typed=false,b_rectanglechoose,b_circle;//判别用户当前选取的工具
float m_linewidth; //线条粗细
Color m_backcolor = Color.white, m_fillcolor = Color.white,
m_linecolor = Color.black; //判别颜色选取
public J_Painter()//构造添加菜单和面板以及初始化图像
{
super("ImageDraw-LHS");
con = getContentPane();
con.setLayout(new BorderLayout());
con.add(m_toolPanel,BorderLayout.NORTH);
m_toolPanel.setBackground(new Color(57, 169, 42));
m_toolPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
con.add(new JScrollPane(m_leftPanel),BorderLayout.WEST);
m_leftPanel.setBackground(new Color(200,243,245));
con.add(new JScrollPane(m_panel),BorderLayout.CENTER);
setJMenuBar(m_menuBar);
addMenu(); //添加菜单
Image img = Toolkit.getDefaultToolkit().getImage("tupian/T00.gif");
m_cursor = Toolkit.getDefaultToolkit().createCustomCursor(img,
new Point(13, 13), "liao"); //自定义光标
m_buffImage = new BufferedImage(m_Width, m_Height,
BufferedImage.TYPE_INT_BGR);
Graphics2D g21d = m_buffImage.createGraphics();
g21d.setColor(m_backcolor);
// g21d.setBackground(Color.blue);
g21d.fillRect(0, 0, m_Width, m_Height);//初始化图像
float[] ss = { 4, 4, 4 };
g21d.setStroke(new BasicStroke(1, 0, 1, 1, ss, 3));
g21d.setColor(Color.red);
g21d.drawRect(188, 144, 370, 120);
g21d.setFont(new Font("宋体", Font.BOLD, 20));
g21d.drawString("欢迎你的使用,谢谢你对本软件的支持", 200, 212);//欢迎界面
repaint();
setSize(820, 700);
setVisible(true);
}// end of J_Painter
public void addMenu()//构造菜单
{
JMenu m_menu[] = new JMenu[5];
JMenuItem m_menuItem[] = new JMenuItem[23];
String strMenu[] = { new String("File"), new String("Edit"),
new String("Tool"), new String("Style"), new String("Help") };
m_menuBar.setBackground(new Color(240, 202, 241));
m_menuBar.setPreferredSize(new Dimension(600, 37));
String strMenuItem[] = { new String("New"), new String("Open"),
new String("Save"), new String("Exit"),
new String("Cut"), new String("Copy"), new String("Paste"),
new String("FreeWriting"), new String("Line"),
new String("Rectangle"), new String("Ellipse"),
new String("Fill"), new String("FillColor"),
new String("LineColor"), new String("LineWidth"),
new String("SetbackColor"), new String("ColorReplace"),
new String("About"), new String("Introduction") };
for (int i = 0; i <= 4; i++) {
m_menu[i] = new JMenu(strMenu[i]);
m_menuBar.add(m_menu[i]);
}
for (int j = 0; j < 4; j++) {
m_menuItem[j] = new JMenuItem(strMenuItem[j]);
m_menu[0].add(m_menuItem[j]);
}
for (int j = 4; j < 7; j++) {
m_menuItem[j] = new JMenuItem(strMenuItem[j]);
m_menu[1].add(m_menuItem[j]);
}
for (int j = 7; j < 11; j++) {
m_menuItem[j] = new JMenuItem(strMenuItem[j]);
m_menu[2].add(m_menuItem[j]);
}
for (int j = 11; j < 17; j++) {
m_menuItem[j] = new JMenuItem(strMenuItem[j]);
m_menu[3].add(m_menuItem[j]);
}
for (int j = 17; j <= 18; j++) {
m_menuItem[j] = new JMenuItem(strMenuItem[j]);
m_menu[4].add(m_menuItem[j]);
}
m_menuItem[22]=new JMenuItem("Circle");
m_menu[2].add(m_menuItem[22]);
m_menuItem[19] = new JMenuItem("Arc");
m_menu[2].add(m_menuItem[19]);
m_menuItem[20] = new JMenuItem("RectClipChoose");
m_menu[1].add(m_menuItem[20]);
m_menuItem[21] = new JMenuItem("ClearRectClip");
m_menu[1].add(m_menuItem[21]);
// 设置记忆符
m_menu[0].setMnemonic('F');
m_menu[1].setMnemonic('E');
m_menu[2].setMnemonic('T');
m_menu[3].setMnemonic('S');
m_menu[4].setMnemonic('H');
m_menuItem[0].setMnemonic('N');
m_menuItem[1].setMnemonic('O');
m_menuItem[2].setMnemonic('S');
m_menuItem[4].setMnemonic('V');
m_menuItem[5].setMnemonic('C');
m_menuItem[6].setMnemonic('P');
m_menuItem[7].setMnemonic('W');
m_menuItem[8].setMnemonic('L');
m_menuItem[9].setMnemonic('R');
m_menuItem[10].setMnemonic('P');
m_menuItem[17].setMnemonic('A');
m_menuItem[18].setMnemonic('I');
m_menuItem[2].addActionListener(new J_dealSave());
m_menuItem[7].addActionListener(new J_dealTool());
m_menuItem[8].addActionListener(new J_dealTool());
m_menuItem[9].addActionListener(new J_dealTool());
m_menuItem[10].addActionListener(new J_dealTool());
m_menuItem[19].addActionListener(new J_dealTool());
m_menuItem[20].addActionListener(new J_dealTool());
m_menuItem[22].addActionListener(new J_dealTool());
m_menuItem[11].addActionListener(new J_dealStyle());
m_menuItem[12].addActionListener(new J_dealStyle());
m_menuItem[13].addActionListener(new J_dealStyle());
m_menuItem[14].addActionListener(new J_dealStyle());
m_menuItem[15].addActionListener(new J_dealStyle());
m_menuItem[16].addActionListener(new J_dealStyle());
m_menuItem[22].addActionListener(new J_dealStyle());
m_menuItem[17].addActionListener(new J_dealHelp());
m_menuItem[18].addActionListener(new J_dealHelp());
m_menuItem[4].addActionListener(new J_dealEditCut());
m_menuItem[6].addActionListener(new J_dealEditPaste());
m_menuItem[21].addActionListener(new J_dealEditClear());
m_menuItem[16].addActionListener(new J_dealColorChange());
m_menuItem[0].addActionListener(new J_dealNew());
m_menuItem[1].addActionListener(new J_dealOpen());
m_menuItem[3].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ee)
{
System.exit(0);
}
});// exit
for (int ii = 0; ii <= 4; ii++)
m_menu[ii].setForeground(new Color(100, 0, 0));//菜单前景色
// 设置右键
String strpop[] = { new String("FreeWriting"), new String("Line"),
new String("Rectangle"), new String("Ellipse"),
new String("B_Fill"), new String("FillColor"),
new String("LineColor") };
JMenuItem m_menuPopupItem[] = new JMenuItem[7];
for (int ii = 0; ii < 7; ii++) {
m_menuPopupItem[ii] = new JMenuItem(strpop[ii]);
m_popupMenu.add(m_menuPopupItem[ii]);
}
m_menuPopupItem[6].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ee) {
m_linecolor = JColorChooser.showDialog(m_fatherFrame,
"LineColor Dialog", m_linecolor);
}
});
m_menuPopupItem[5].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ee) {
m_fillcolor = JColorChooser.showDialog(m_fatherFrame,
"FillColor Dialog", m_fillcolor);
}
});
m_menuPopupItem[0].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ee) {
makeallfalse();
b_freeWriting = true;
}
});
m_menuPopupItem[1].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ee) {
makeallfalse();
b_line = true;
}
});
m_menuPopupItem[2].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ee) {
makeallfalse();
b_rectangle = true;
}
});
m_menuPopupItem[3].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ee) {
makeallfalse();
b_ellipse = true;
}
});
m_menuPopupItem[4].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ee) {
b_fill = !b_fill;
}
});//设置右键并且作出响应
}// end of addMenu
class J_dealNew implements ActionListener//新建图像
{
public void actionPerformed(ActionEvent ee)
{
final JDialog m_newDiag = new JDialog(m_fatherFrame,
"NewDialog-图片大小", false);
m_newDiag.setDefaultLookAndFeelDecorated(true);
final JTextField m_textHeight = new JTextField("680", 7);
final JTextField m_textWidth = new JTextField("800", 7);
JLabel m_set = new JLabel("设置图片大小", null, JLabel.LEFT);
JLabel m_widthLab = new JLabel("Width", null, JLabel.LEFT);
;
JLabel m_heightLab = new JLabel("Height", null, JLabel.LEFT);
JPanel m_panelnew1 = new JPanel();
JPanel m_panelnew2 = new JPanel();
JPanel m_panelnew3 = new JPanel();
JPanel m_panelnew4 = new JPanel();
Container m_newDiagCon = m_newDiag.getContentPane();
m_newDiagCon
.setLayout(new BoxLayout(m_newDiagCon, BoxLayout.Y_AXIS));
m_newDiagCon.add(m_panelnew1);
m_newDiagCon.add(m_panelnew2);
m_newDiagCon.add(m_panelnew3);
m_newDiagCon.add(m_panelnew4);
m_panelnew4.setLayout(new FlowLayout(FlowLayout.RIGHT));
m_panelnew1.add(m_set);
m_panelnew2.add(m_widthLab);
m_panelnew2.add(m_textWidth);
m_panelnew3.add(m_heightLab);
m_panelnew3.add(m_textHeight);
JButton m_newButton = new JButton("确定");
m_panelnew4.add(m_newButton);
m_newDiag.setSize(200, 230);
m_newDiag.setLocation(152, 130);//初始化新建对话框
m_newButton.addActionListener(new ActionListener() //响应确定
{
public void actionPerformed(ActionEvent eve)
{
try {
m_Width = Integer.valueOf(m_textWidth.getText());
m_Height = Integer.valueOf(m_textWidth.getText());
if (m_Width < 0)
m_Width = 0;
if (m_Height < 0)
m_Height = 0;
m_newDiag.setVisible(false);
m_buffImage = new BufferedImage(m_Width, m_Height,
BufferedImage.TYPE_INT_BGR);
Graphics2D g2d = m_buffImage.createGraphics();
g2d.setColor(m_backcolor);
g2d.fillRect(0, 0, m_Width, m_Height);
m_leftPanel.b_WT=false;
repaint();
}
catch (Exception rr)
{
}
}
});//重新新建图像
m_newDiag.setVisible(true);
m_newDiag.setResizable(true);
}
}// end of dealnew
class J_dealOpen implements ActionListener //打开图像
{
public void actionPerformed(ActionEvent ee)
{
JFileChooser choose = new JFileChooser();
if (choose.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
{
String name = choose.getSelectedFile().getPath();//获得图像路径
File m_inputFile = new File(name);
try {
m_buffImage = ImageIO.read(m_inputFile);//读取图像
}
catch (IOException en)
{
System.out.print(en);
}
// Image img=Toolkit.getDefaultToolkit().getImage("26.jpg");
repaint();
}
}
}
public static void main(String args[])
{
JFrame.setDefaultLookAndFeelDecorated(true);// 设置窗口风格java
J_Painter app = new J_Painter();
m_fatherFrame = app;
app.setIconImage(Toolkit.getDefaultToolkit().getImage(
"tupian/C3.gif")); //设置程序图标
//app.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
app.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
app.addWindowListener(new J_SaveAndClose());
}
static class J_SaveAndClose extends WindowAdapter
{
String m_filename = new String("");//路径名
String m_formatname = new String("");//保存格式名
public void windowClosing(WindowEvent e)
{
JOptionPane fileNameDialog=new JOptionPane();
int a=fileNameDialog.showConfirmDialog(con,"您要保存图像吗?");
if(a==0)
{
String name[] = { new String("jpg"), new String("bmp"),
new String("png") };
final JDialog m_diag = new JDialog(m_fatherFrame, "Save dialog ",
true);
m_diag.setDefaultLookAndFeelDecorated(true);
JLabel m_lab = new JLabel("输入保存路径名", null, JLabel.LEFT);
JLabel m_lab1 = new JLabel("选择文件保存格式", null, JLabel.LEFT);
final JTextField m_text = new JTextField("", 24);
final JComboBox m_combobox = new JComboBox(name);
JButton m_button = new JButton("确定");
m_lab.setToolTipText("输入文件保存的路径名和文件名,如果只输入文件名,将被保存在本地路径中");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -