📄 paintbrush.txt
字号:
g2d.draw( new Line2D.Double( 0, 0, 12, 12 ) );
g2d.draw( new Line2D.Double( 11, 0, 0, 11 ) );
repaint();
if(e.getButton()==1)
color_border=null;
else
color_inside=null;
}
else if(choose==29){//填充间层
jDialog.show();
g2d.setPaint( new GradientPaint( 0, 0, center.G_color_left, 12, 12, center.G_color_right, true ) );
g2d.fill( new Rectangle2D.Double( 0, 0, 12, 12 ) );
repaint();
if(e.getButton()==1)
color_border=new GradientPaint( 0, 0, center.G_color_left, 12, 12, center.G_color_right, true );
else
color_inside=new GradientPaint( 0, 0, center.G_color_left, 12, 12, center.G_color_right, true );
}
else if(choose==30){//填充图案
FileDialog fileDialog = new FileDialog( new Frame() , "选择一个文件", FileDialog.LOAD );//利用FileDialog抓取文件名
fileDialog.show();//出视窗
if(fileDialog.getFile()==null) return;//按取消的处理
g2d.drawImage(special_color[2].getImage(), 0, 0,this);//把调色盘左边变为图片
icon = new ImageIcon(fileDialog.getDirectory()+fileDialog.getFile());//利用FileDialog传进来的文件名读取图片
BufferedImage bufferedImage = new BufferedImage(icon.getIconWidth(),icon.getIconHeight(),BufferedImage.TYPE_3BYTE_BGR);//创一张新的BufferedImage,为了要读取娶进来的图片长宽,以免有空白
bufferedImage.createGraphics().drawImage(icon.getImage(),0,0,this);//把icon画到BufferedImage上
repaint();//重绘画板
if(e.getButton()==1)//判断边线颜色或内部是否填满色
color_border=new TexturePaint(bufferedImage, new Rectangle( icon.getIconWidth(), icon.getIconHeight() ) );//把这张張BufferedImage设成TexturePaint来填满
else
color_inside=new TexturePaint(bufferedImage, new Rectangle( icon.getIconWidth(), icon.getIconHeight() ) );
}
else if(choose==31){//填充文字
String text=JOptionPane.showInputDialog("请输入文字","文字");//熟入文字
if(text==null) return;//按取消时的处理
Color FontColor=new Color(0,0,0);//改字的颜色
FontColor = JColorChooser.showDialog( Painter.this, "请选择一个颜色当文字颜色", FontColor );//请使用者选择颜色
g2d.drawImage(special_color[3].getImage(), 0, 0,this);//把调色盘左方换成『字』
BufferedImage bufferedImage = new BufferedImage(draw_panel_width,draw_panel_height,BufferedImage.TYPE_3BYTE_BGR);//创一张新的BufferedImage
Graphics2D g2d_bufferedImage = bufferedImage.createGraphics();//把Graphics拿出來画
FontRenderContext frc = g2d_bufferedImage.getFontRenderContext();//读Graphics中的Font
Font f = new Font("新细明体",Font.BOLD,10);//新Font
TextLayout tl = new TextLayout(text, f, frc);//创新的TextLayout,并利用f(Font)跟画到frc(FontRenderContext)
int sw = (int) (tl.getBounds().getWidth()+tl.getCharacterCount());//计算TextLayout的长
int sh = (int) (tl.getBounds().getHeight()+3);//计算TextLayout的高
bufferedImage = new BufferedImage(sw,sh,BufferedImage.TYPE_3BYTE_BGR);//再创一张新的BufferedImage,这里利用相同指标指向不同记忆体
g2d_bufferedImage = bufferedImage.createGraphics();//拿出Graphics來画,前一張BufferedImage只是为了计算文字长度与高度,这样才能完整填满
g2d_bufferedImage.setPaint(Color.WHITE);//设定颜色为白色
g2d_bufferedImage.fill(new Rectangle(0,0,sw,sh));//画一个填满矩形白色矩型
g2d_bufferedImage.setPaint(FontColor);//设定颜色为之前选择文字颜色
g2d_bufferedImage.drawString(text,0,10);//画一個String于BufferedImage上
repaint();//更新画面
if(e.getButton()==1)//判断边线颜色或内部是否填满色
color_border=new TexturePaint(bufferedImage, new Rectangle(sw,sh) );//把这张BufferedImage设成TexturePaint來填滿来填满
else
color_inside=new TexturePaint(bufferedImage, new Rectangle(sw,sh) );
}
else{//填充一般色
g2d.setPaint(new Color(rgb[0][choose],rgb[1][choose],rgb[2][choose]));
g2d.fill( new Rectangle2D.Double( 0, 0, 12, 12 ) );
repaint();
if(e.getButton()==1)
color_border=new Color(rgb[0][choose],rgb[1][choose],rgb[2][choose]);
else
color_inside=new Color(rgb[0][choose],rgb[1][choose],rgb[2][choose]);
}
}
public void mouseReleased( MouseEvent e ){}
public void mouseEntered( MouseEvent e ){}
public void mouseExited( MouseEvent e ){}
}
public class DrawPanel extends JPanel implements MouseListener, MouseMotionListener, ItemListener, ActionListener, ChangeListener{//中央畫布
public BufferedImage bufImg;//记录最新画面,並在此上作画
private BufferedImage bufImg_data[];//记录所有画出图面,索引值越大越新,最大为最新
private BufferedImage bufImg_cut;
private ImageIcon img;
private JLabel jlbImg;
private int x1=-1,y1=-1,x2,y2,count,redo_lim,press,temp_x1,temp_y1,temp_x2,temp_y2,temp_x3,temp_y3,step,step_chk,step_arc,step_chk_arc,chk,first,click,cut;
private Arc2D.Double arc2D = new Arc2D.Double();//扇型
private Line2D.Double line2D = new Line2D.Double();//直线
private Ellipse2D.Double ellipse2D = new Ellipse2D.Double();//椭圆
private Rectangle2D.Double rectangle2D = new Rectangle2D.Double();//矩型
private CubicCurve2D.Double cubicCurve2D = new CubicCurve2D.Double();//贝氏曲线
private RoundRectangle2D.Double roundRectangle2D = new RoundRectangle2D.Double();//圆角矩型
private Polygon polygon;//多边型
private float data[]={5};
private Rectangle2D.Double rectangle2D_select = new Rectangle2D.Double();//矩型
private Ellipse2D.Double ellipse2D_pan = new Ellipse2D.Double();
private BasicStroke basicStroke_pen = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER);
private BasicStroke basicStroke_select = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER,10, data, 0);
private double center_point_x;
private double center_point_y;
private double start;
private double end;
public String filename;
private JTextField textField_font = new JTextField("Fixedsys",16), textField_word = new JTextField("JAVA",16);
private int size=100;
private JSpinner fontsize = new JSpinner();
private JDialog jDialog;
private JCheckBox bold, italic;
private JButton ok, cancel;
public int pie_shape=Arc2D.PIE;
private int valBold = Font.BOLD;
private int valItalic = Font.ITALIC;
private int select_x,select_y,select_w,select_h;
public void resize(){//改变大小
bufImg = new BufferedImage(draw_panel_width, draw_panel_height,BufferedImage.TYPE_3BYTE_BGR);
jlbImg = new JLabel(new ImageIcon(bufImg));//在JLabel上放置bufImg,用来绘图
this.removeAll();
this.add(jlbImg);
jlbImg.setBounds(new Rectangle(0, 0, draw_panel_width, draw_panel_height));
//画出原本图形//
Graphics2D g2d_bufImg = (Graphics2D) bufImg.getGraphics();
g2d_bufImg.setPaint(Color.white);
g2d_bufImg.fill(new Rectangle2D.Double(0,0,draw_panel_width,draw_panel_height));
g2d_bufImg.drawImage(bufImg_data[count],0,0,this);
//记录可重做最大次数,并让重做不可按//
redo_lim=count++;
jMenuItem[1][1].setEnabled(false);
//新增一张BufferedImage形态至bufImg_data[count],并将bufImg绘制至bufImg_data[count]//
bufImg_data[count] = new BufferedImage(draw_panel_width, draw_panel_height, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g2d_bufImg_data = (Graphics2D) bufImg_data[count].getGraphics();
g2d_bufImg_data.drawImage(bufImg,0,0,this);
//判断坐标为新起点//
press=0;
//让复原MenuItem可以点选//
if(count>0)
jMenuItem[1][0].setEnabled(true);
}
public DrawPanel() {
bufImg_data = new BufferedImage[1000];
bufImg = new BufferedImage(draw_panel_width, draw_panel_height,BufferedImage.TYPE_3BYTE_BGR);
jlbImg = new JLabel(new ImageIcon(bufImg));//在JLabel上放置bufImg,用來绘图
this.setLayout(null);
this.add(jlbImg);
jlbImg.setBounds(new Rectangle(0, 0, draw_panel_width, draw_panel_height));
jMenuItem[1][0].setEnabled(false);
jMenuItem[1][1].setEnabled(false);
jMenuItem[1][2].setEnabled(false);
jMenuItem[1][3].setEnabled(false);
jMenuItem[1][4].setEnabled(false);
//画出空白//
Graphics2D g2d_bufImg = (Graphics2D) bufImg.getGraphics();
g2d_bufImg.setPaint(Color.WHITE);
g2d_bufImg.fill(new Rectangle2D.Double(0,0,draw_panel_width,draw_panel_height));
bufImg_data[count] = new BufferedImage(draw_panel_width, draw_panel_height, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g2d_bufImg_data = (Graphics2D) bufImg_data[count].getGraphics();
g2d_bufImg_data.drawImage(bufImg,0,0,this);
//Font//
jDialog = new JDialog(Painter.this, "请选择文字、字型、大小与属性", true);
fontsize.setValue(new Integer(100));
bold = new JCheckBox( "粗体" ,true);
italic = new JCheckBox( "斜体" ,true);
ok = new JButton("确定");
cancel = new JButton("取消");
JPanel temp_0 = new JPanel(new GridLayout(5,1));
JPanel temp_1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel temp_2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel temp_3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel temp_4 = new JPanel(new FlowLayout());
JPanel temp_5 = new JPanel(new FlowLayout(FlowLayout.LEFT));
Container jDialog_c = jDialog.getContentPane();
jDialog_c.setLayout(new FlowLayout());
jDialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE );
jDialog.setSize(250, 200);
temp_5.add(new JLabel("文字:"));
temp_5.add(textField_word);
temp_1.add(new JLabel("字体:"));
temp_1.add(textField_font);
temp_2.add(new JLabel("大小:"));
temp_2.add(fontsize);
temp_3.add(new JLabel("属性:"));
temp_3.add(bold);
temp_3.add(italic);
temp_4.add(ok);
temp_4.add(cancel);
temp_0.add(temp_5);
temp_0.add(temp_1);
temp_0.add(temp_2);
temp_0.add(temp_3);
temp_0.add(temp_4);
jDialog_c.add(temp_0);
bold.addItemListener( this );
italic.addItemListener( this );
fontsize.addChangeListener( this );
ok.addActionListener(this);
cancel.addActionListener(this);
temp_0.setPreferredSize(new Dimension( 180 , 150 ));
repaint();
addMouseListener(this);
addMouseMotionListener(this);
}
public void stateChanged(ChangeEvent e){
size = Integer.parseInt(fontsize.getValue().toString());
if(size <= 0) {
fontsize.setValue(new Integer(1));
size = 1;
}
}
public void actionPerformed( ActionEvent e ){
jDialog.dispose();
}
public void itemStateChanged( ItemEvent e ){
if ( e.getSource() == bold )
if ( e.getStateChange() == ItemEvent.SELECTED )
valBold = Font.BOLD;
else
valBold = Font.PLAIN;
if ( e.getSource() == italic )
if ( e.getStateChange() == ItemEvent.SELECTED )
valItalic = Font.ITALIC;
else
valItalic = Font.PLAIN;
}
public Dimension getPreferredSize(){
return new Dimension( draw_panel_width, draw_panel_height );
}
public void openfile(String filename){//开放旧文件
Graphics2D g2d_bufImg = (Graphics2D) bufImg.getGraphics();
ImageIcon icon = new ImageIcon(filename);
g2d_bufImg.drawImage(icon.getImage(),0,0,this);
count++;
bufImg_data[count] = new BufferedImage(draw_panel_width, draw_panel_height, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g2d_bufImg_data = (Graphics2D) bufImg_data[count].getGraphics();
g2d_bufImg_data.drawImage(bufImg,0,0,this);
repaint();
}
public void undo(){//复原
count--;
draw_panel_width=bufImg_data[count].getWidth();
draw_panel_height=bufImg_data[count].getHeight();
drawPanel.setSize(draw_panel_width,draw_panel_height);
bufImg = new BufferedImage(draw_panel_width, draw_panel_height,BufferedImage.TYPE_3BYTE_BGR);
jlbImg = new JLabel(new ImageIcon(bufImg));//在JLabel上放置bufImg,用來绘图
this.removeAll();
this.add(jlbImg);
jlbImg.setBounds(new Rectangle(0, 0, draw_panel_width, draw_panel_height));
Graphics2D g2d_bufImg = (Graphics2D) bufImg.getGraphics();
g2d_bufImg.setPaint(Color.white);
g2d_bufImg.fill(new Rectangle2D.Double(0,0,draw_panel_width,draw_panel_height));
g2d_bufImg.drawImage(bufImg_data[count],0,0,this);
underDrawPanel.ctrl_area.setLocation(draw_panel_width+3,draw_panel_height+3);
underDrawPanel.ctrl_area2.setLocation(draw_panel_width+3,draw_panel_height/2+3);
underDrawPanel.ctrl_area3.setLocation(draw_panel_width/2+3,draw_panel_height+3);
underDrawPanel.x=draw_panel_width;
underDrawPanel.y=draw_panel_height;
if(count<=0)
jMenuItem[1][0].setEnabled(false);
jMenuItem[1][1].setEnabled(true);
cut=3;
repaint();
}
public void redo(){//重做
count++;
draw_panel_width=bufImg_data[count].getWidth();
draw_panel_height=bufImg_data[count].getHeight();
drawPanel.setSize(draw_panel_width,draw_panel_height);
bufImg = new BufferedImage(draw_panel_width, draw_panel_height,BufferedImage.TYPE_3BYTE_BGR);
jlbImg = new JLabel(new ImageIcon(bufImg));//在JLabel上放置bufImg,用來绘图
this.removeAll();
this.add(jlbImg);
jlbImg.setBounds(new Rectangle(0, 0, draw_panel_width, draw_panel_height));
Graphics2D g2d_bufImg = (Graphics2D) bufImg.getGraphics();
g2d_bufImg.setPaint(Color.white);
g2d_bufImg.fill(new Rectangle2D.Double(0,0,draw_panel_width,draw_panel_height));
g2d_bufImg.drawImage(bufImg_data[count],0,0,this);
underDrawPanel.ctrl_area.setLocation(draw_panel_width+3,draw_panel_height+3);
underDrawPanel.ctrl_area2.setLocation(draw_panel_width+3,draw_panel_height/2+3);
underDrawPanel.ctrl_area3.setLocation(draw_panel_width/2+3,draw_panel_height+3);
underDrawPanel.x=draw_panel_width;
underDrawPanel.y=draw_panel_height;
if(redo_lim<count)
jMenuItem[1][1].setEnabled(false);
jMenuItem[1][0].setEnabled(true);
cut=3;
repaint();
}
public void cut(){
bufImg_cut = new BufferedImage((int)rectangle2D_select.getWidth(), (int)rectangle2D_select.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
BufferedImage copy = bufImg.getSubimage((int)rectangle2D_select.getX(),(int)rectangle2D_select.getY(),(int)rectangle2D_select.getWidth(),(int)rectangle2D_select.getHeight());
Graphics2D g2d_bufImg_cut = (Graphics2D) bufImg_cut.createGraphics();
g2d_bufImg_cut.drawImage(copy,0,0,this);
Graphics2D g2d_bufImg = (Graphics2D) bufImg.getGraphics();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -