📄 paintbrush.txt
字号:
g2d_bufImg.setPaint(Color.WHITE);
g2d_bufImg.fill(new Rectangle2D.Double((int)rectangle2D_select.getX(),(int)rectangle2D_select.getY(),(int)rectangle2D_select.getWidth(),(int)rectangle2D_select.getHeight()));
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);
jMenuItem[1][2].setEnabled(false);
jMenuItem[1][3].setEnabled(false);
jMenuItem[1][4].setEnabled(true);
cut=3;
repaint();
}
public void copy(){
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);
jMenuItem[1][4].setEnabled(true);
cut=1;
repaint();
}
public void paste(){
cut=2;
repaint();
}
public void mousePressed(MouseEvent e) {
x1=e.getX();
y1=e.getY();
if(first==0){
polygon = new Polygon();
polygon.addPoint(x1, y1);
first=1;
}
//判断坐标为新起点//
press=1;
chk=0;
if(cut!=2) cut=0;
}
public void mouseReleased(MouseEvent e) {
x2=e.getX();
y2=e.getY();
if(step_chk==0)//控制贝氏曲线用
step=1;
else if(step_chk==1)
step=2;
if(step_chk_arc==0)//控制扇型用
chk=step_arc=1;
else if(step_chk_arc==1)
chk=step_arc=2;
if(drawMethod==6 && click!=1){
polygon.addPoint(x2, y2);
repaint();
}
if(drawMethod==10){
if(cut!=2) cut=1;
select_x=(int)rectangle2D_select.getX();
select_y=(int)rectangle2D_select.getY();
select_w=(int)rectangle2D_select.getWidth();
select_h=(int)rectangle2D_select.getHeight();
jMenuItem[1][2].setEnabled(true);
jMenuItem[1][3].setEnabled(true);
}
if((step_chk==2 && step==2) || (step_chk_arc==2 && step_arc==2) || drawMethod==0 || drawMethod==1 || drawMethod==2 || drawMethod==3 || drawMethod==7 || drawMethod==8 || drawMethod==9 || cut==2){//當不是畫貝氏曲線或是已經完成貝氏曲線時畫
toDraw();
}
}
public void clear(){
cut=select_x=select_y=select_w=select_h=step_chk_arc=step_arc=first=step_chk=step=0;
x1=x2=y1=y2=-1;
}
public void toDraw(){
if(x1<0 || y1<0) return;//防止误按
chk=3;
draw(x1,y1,x2,y2);
//画出图形至bufImg//
Graphics2D g2d_bufImg = (Graphics2D) bufImg.getGraphics();
if(cut!=2){
if(color_inside!=null && drawMethod!=8){
g2d_bufImg.setPaint(color_inside);
g2d_bufImg.fill(shape);
}
if(color_border!=null && drawMethod!=8){
g2d_bufImg.setPaint(color_border);
g2d_bufImg.setStroke(stroke);
g2d_bufImg.draw(shape);
}
}
else{
g2d_bufImg.drawImage(bufImg_cut,x2,y2,this);
}
repaint();
clear();
//记录可重做最大次数,並让重做不可按//
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 void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){
if(click==1){//双击时
toDraw();
}
click=1;
}
public void mouseDragged(MouseEvent e){
x2=e.getX();
y2=e.getY();
if(drawMethod==7 || drawMethod==8){
draw(x1,y1,x2,y2);
x1=e.getX();
y1=e.getY();
}
if(drawMethod!=9)
repaint();
}
public void mouseMoved(MouseEvent e) {
show_x=x2=e.getX();
show_y=y2=e.getY();
jLabel[0].setText(show_x+","+show_y);
click=0;
if(drawMethod==7 || drawMethod==8 || cut==2)
repaint();
}
public void draw(int input_x1,int input_y1,int input_x2,int input_y2){
if(drawMethod==0){//直线时,让shape为Line2D
shape=line2D;
line2D.setLine(input_x1,input_y1,input_x2,input_y2);
}
else if(drawMethod==1){//矩型時,让shape为Rectangle2D
shape=rectangle2D;
rectangle2D.setRect(Math.min(input_x1,input_x2),Math.min(input_y1,input_y2),Math.abs(input_x1-input_x2),Math.abs(input_y1-input_y2));
}
else if(drawMethod==2){//椭圆
shape=ellipse2D;
ellipse2D.setFrame(Math.min(input_x1,input_x2),Math.min(input_y1,input_y2),Math.abs(input_x1-input_x2),Math.abs(input_y1-input_y2));
}
else if(drawMethod==3){//圆角矩型
shape=roundRectangle2D;
roundRectangle2D.setRoundRect(Math.min(input_x1,input_x2),Math.min(input_y1,input_y2),Math.abs(input_x1-input_x2),Math.abs(input_y1-input_y2),10.0f,10.0f);
}
else if(drawMethod==4){//贝氏曲線
shape=cubicCurve2D;
if(step==0){
cubicCurve2D.setCurve(input_x1,input_y1,input_x1,input_y1,input_x2,input_y2,input_x2,input_y2);
temp_x1=input_x1;
temp_y1=input_y1;
temp_x2=input_x2;
temp_y2=input_y2;
step_chk=0;
}
else if(step==1){
cubicCurve2D.setCurve(temp_x1,temp_y1,input_x2,input_y2,input_x2,input_y2,temp_x2,temp_y2);
temp_x3=input_x2;
temp_y3=input_y2;
step_chk=1;
}
else if(step==2){
cubicCurve2D.setCurve(temp_x1,temp_y1,temp_x3,temp_y3,input_x2,input_y2,temp_x2,temp_y2);
step_chk=2;
}
}
else if(drawMethod==5){//扇型,chk用來防止意外的repaint//
if(step_arc==0 || chk==1){//步驟控制
shape=ellipse2D;
ellipse2D.setFrame(Math.min(input_x1,input_x2),Math.min(input_y1,input_y2),Math.abs(input_x1-input_x2),Math.abs(input_y1-input_y2));
temp_x1=input_x1;
temp_y1=input_y1;
temp_x2=input_x2;
temp_y2=input_y2;
step_chk_arc=0;
}
else if(step_arc==1 || chk==2){//步骤控制
shape=arc2D;
center_point_x = Math.min(temp_x1,temp_x2)+Math.abs(temp_x1-temp_x2)/2;
center_point_y = Math.min(temp_y1,temp_y2)+Math.abs(temp_y1-temp_y2)/2;
double a = Math.pow(Math.pow(input_x2-center_point_x,2)+Math.pow(input_y2-center_point_y,2),0.5);
double b = input_x2-center_point_x;
if(input_y2>center_point_y)
start=360+Math.acos(b/a)/Math.PI*-180;
else
start=Math.acos(b/a)/Math.PI*180;
arc2D.setArc(Math.min(temp_x1,temp_x2),Math.min(temp_y1,temp_y2),Math.abs(temp_x1-temp_x2),Math.abs(temp_y1-temp_y2),start,0,pie_shape);
step_chk_arc=1;
}
else if(step_arc==2 || chk==3){//步骤控制
shape=arc2D;
double a = Math.pow(Math.pow(input_x2-center_point_x,2)+Math.pow(input_y2-center_point_y,2),0.5);
double b = input_x2-center_point_x;
if(input_y2>center_point_y)
end=360+Math.acos(b/a)/Math.PI*-180-start;
else
end=Math.acos(b/a)/Math.PI*180-start;
if(end<0){end=360-Math.abs(end);}
arc2D.setArc(Math.min(temp_x1,temp_x2),Math.min(temp_y1,temp_y2),Math.abs(temp_x1-temp_x2),Math.abs(temp_y1-temp_y2),start,end,pie_shape);
step_chk_arc=2;
}
}
else if(drawMethod==6){//多边型
shape=polygon;
}
else if(drawMethod==7 || drawMethod==8){//任意线&橡皮擦
Graphics2D g2d_bufImg = (Graphics2D) bufImg.getGraphics();
shape=line2D;
line2D.setLine(input_x1,input_y1,input_x2,input_y2);
if(drawMethod==7)
g2d_bufImg.setPaint(color_border);
else
g2d_bufImg.setPaint(Color.white);
g2d_bufImg.setStroke(stroke);
g2d_bufImg.draw(shape);
}
else if(drawMethod==9){//文字
Graphics2D g2d_bufImg = (Graphics2D) bufImg.getGraphics();
FontRenderContext frc = g2d_bufImg.getFontRenderContext();
jDialog.show();
Font f = new Font(textField_font.getText(),valBold + valItalic,size);
TextLayout tl = new TextLayout(textField_word.getText(), f, frc);
double sw = tl.getBounds().getWidth();
double sh = tl.getBounds().getHeight();
AffineTransform Tx = AffineTransform.getScaleInstance(1, 1);
Tx.translate(input_x2,input_y2+sh);
shape = tl.getOutline(Tx);
}
else if(drawMethod==10){//选取工具
shape=rectangle2D;
rectangle2D.setRect(Math.min(input_x1,input_x2),Math.min(input_y1,input_y2),Math.abs(input_x1-input_x2),Math.abs(input_y1-input_y2));
}
if(color_border instanceof GradientPaint){//使用渐层填色读取拖拉坐标
color_border = new GradientPaint( input_x1,input_y1, (Color)((GradientPaint)color_border).getColor1(), input_x2,input_y2, (Color)((GradientPaint)color_border).getColor2(), true );
}
if(color_inside instanceof GradientPaint){
color_inside = new GradientPaint( input_x1,input_y1, (Color)((GradientPaint)color_inside).getColor1(), input_x2,input_y2, (Color)((GradientPaint)color_inside).getColor2(), true );
}
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
super.paint(g2d);//重回底层JPanel以及上面所有元件
if(press==1 && drawMethod!=10 && !(x1<0 || y1<0)) {//绘图在最上面的JLabel上,并判断是不是起点才画
draw(x1,y1,x2,y2);
if(drawMethod==8) return;
if(color_inside!=null){
g2d.setPaint(color_inside);
g2d.fill(shape);
}
if(color_border!=null){
g2d.setPaint(color_border);
g2d.setStroke(stroke);
g2d.draw(shape);
}
}
if(drawMethod==10 && cut==0){//选取选取控制、判断是否选取、剪下、或贴上上
g2d.setPaint(Color.black);
g2d.setStroke(basicStroke_select);
rectangle2D_select.setRect(Math.min(x1,x2),Math.min(y1,y2),Math.abs(x1-x2),Math.abs(y1-y2));
g2d.draw(rectangle2D_select);
}
if(cut==1){
g2d.setPaint(Color.black);
g2d.setStroke(basicStroke_select);
rectangle2D_select.setRect(select_x,select_y,select_w,select_h);
g2d.draw(rectangle2D_select);
}
if(cut==2){
g2d.drawImage(bufImg_cut,x2,y2,this);
}
//跟随游标的圆形//
if(drawMethod==7 || drawMethod==8){
g2d.setPaint(Color.black);
g2d.setStroke(basicStroke_pen);
ellipse2D_pan.setFrame(x2-setPanel.number/2,y2-setPanel.number/2,setPanel.number,setPanel.number);
g2d.draw(ellipse2D_pan);
}
}
}
public static void main( String args[] ){
try{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());} // 此类跟踪当前的外观及其默认设置
catch(Exception e){e.printStackTrace();} //将此 throwable 及其追踪输出至标准错误流
Painter app = new Painter();
app.setVisible(true);
app.setExtendedState(Frame.MAXIMIZED_BOTH);//面板最大化
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -