📄 main.java
字号:
g2d.draw( new Rectangle2D.Double( -1, -1, x+3, y+3 ) ); } } public class SetPanel extends JPanel implements ItemListener, ChangeListener, ActionListener{ private JPanel jPanel_set1=new JPanel(); private JPanel jPanel_set2=new JPanel(); private JPanel temp0=new JPanel(new GridLayout(4,1)), temp1=new JPanel(new FlowLayout(FlowLayout.LEFT)), temp2=new JPanel(new FlowLayout(FlowLayout.LEFT)), temp3=new JPanel(new FlowLayout(FlowLayout.LEFT)), temp4=new JPanel(new FlowLayout(FlowLayout.LEFT)), temp5=new JPanel(new FlowLayout(FlowLayout.LEFT)), temp6=new JPanel(new FlowLayout(FlowLayout.LEFT)), temp7=new JPanel(new FlowLayout(FlowLayout.LEFT)), temp8=new JPanel(new GridLayout(3,1)); public JCheckBox jCheckBox = new JCheckBox(); private BufferedImage bufImg = new BufferedImage(50 ,50,BufferedImage.TYPE_3BYTE_BGR); private JLabel jlbImg=new JLabel(); float data[]={20}; JLabel pie[]=new JLabel[3]; public int number=5; JSpinner lineWidthSelect = new JSpinner(); JRadioButton style[] = new JRadioButton[ 5 ]; ButtonGroup styleGroup = new ButtonGroup() ,pieGroup = new ButtonGroup(); public SetPanel(){//产生面板// this.setLayout(null); this.add(jPanel_set1); jlbImg.setIcon(new ImageIcon(bufImg)); jPanel_set1.setLayout(new FlowLayout()); jPanel_set1.setBounds(new Rectangle(0, 0, 100, 160)); jPanel_set1.setBorder( new TitledBorder(null, "边框",TitledBorder.LEFT, TitledBorder.TOP) ); lineWidthSelect.setValue(new Integer(5)); for(i=0;i<=1;i++){ style[i] = new JRadioButton(); styleGroup.add(style[i]); style[i].addActionListener(this); } style[0].setSelected( true ); temp1.add(new JLabel("大小:")); temp1.add(lineWidthSelect); temp2.add(new JLabel("虛线:")); temp2.add(jCheckBox); temp3.add(new JLabel("圆角:")); temp3.add(style[0]); temp4.add(new JLabel("尖角:")); temp4.add(style[1]); temp0.add(temp1); temp0.add(temp2); temp0.add(temp3); temp0.add(temp4); jPanel_set1.add(temp0); lineWidthSelect.addChangeListener( this ); jCheckBox.addItemListener( this ); jPanel_set2.setBounds(new Rectangle(0, 170, 100, 130)); jPanel_set2.setBorder( new TitledBorder(null, "扇型设定",TitledBorder.LEFT, TitledBorder.TOP) ); for(i=2;i<=4;i++){ style[i] = new JRadioButton(); pieGroup.add(style[i]); style[i].addActionListener(this); } style[4].setSelected( true ); pie[0] = new JLabel("弦状:"); temp5.add(pie[0]); temp5.add(style[2]); pie[1] = new JLabel("开放:"); temp6.add(pie[1]); temp6.add(style[3]); pie[2] = new JLabel("派状:"); temp7.add(pie[2]); temp7.add(style[4]); temp8.add(temp5); temp8.add(temp6); temp8.add(temp7); temp8.setPreferredSize(new Dimension( 71 , 95 )); jPanel_set2.add(temp8); this.add(jPanel_set2); pie_remove_ctrl(); stroke = new BasicStroke(5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER); } public void pencil_add_ctrl(){ style[0].setSelected(true); style[1].setEnabled(false); jCheckBox.setSelected(false); jCheckBox.setEnabled(false); BasicStroke stroke2 = (BasicStroke) stroke; stroke = new BasicStroke(stroke2.getLineWidth(), BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER); } public void pencil_remove_ctrl(){ style[1].setEnabled(true); jCheckBox.setEnabled(true); } public void pie_add_ctrl(){ pie[0].setEnabled(true); pie[1].setEnabled(true); pie[2].setEnabled(true); style[2].setEnabled(true); style[3].setEnabled(true); style[4].setEnabled(true); } public void pie_remove_ctrl(){ pie[0].setEnabled(false); pie[1].setEnabled(false); pie[2].setEnabled(false); style[2].setEnabled(false); style[3].setEnabled(false); style[4].setEnabled(false); } public void actionPerformed( ActionEvent e ){ BasicStroke stroke2 = (BasicStroke) stroke; if ( e.getSource() == style[0] ) stroke = new BasicStroke( stroke2.getLineWidth(), BasicStroke.CAP_ROUND, stroke2.getLineJoin(), stroke2.getMiterLimit(), stroke2.getDashArray(), stroke2.getDashPhase() ); else if ( e.getSource() == style[1] ) stroke = new BasicStroke( stroke2.getLineWidth(), BasicStroke.CAP_BUTT, stroke2.getLineJoin(), stroke2.getMiterLimit(), stroke2.getDashArray(), stroke2.getDashPhase() ); else if ( e.getSource() == style[2] ) drawPanel.pie_shape=Arc2D.CHORD; else if ( e.getSource() == style[3] ) drawPanel.pie_shape=Arc2D.OPEN; else if ( e.getSource() == style[4] ) drawPanel.pie_shape=Arc2D.PIE; } public void stateChanged(ChangeEvent e){ number = Integer.parseInt(lineWidthSelect.getValue().toString()); if(number <= 0) { lineWidthSelect.setValue(new Integer(1)); number = 1; } BasicStroke stroke2 = (BasicStroke) stroke; stroke = new BasicStroke( number, stroke2.getEndCap(), stroke2.getLineJoin(), stroke2.getMiterLimit(), stroke2.getDashArray(), stroke2.getDashPhase() ); } public void itemStateChanged( ItemEvent e ){ BasicStroke stroke2 = (BasicStroke) stroke; if ( e.getSource() == jCheckBox ){ if ( e.getStateChange() == ItemEvent.SELECTED ) stroke = new BasicStroke( stroke2.getLineWidth(), stroke2.getEndCap(), stroke2.getLineJoin(), 10, data, 0 ); else stroke = new BasicStroke( stroke2.getLineWidth(), stroke2.getEndCap(), stroke2.getLineJoin()); } } public Dimension getPreferredSize(){ return new Dimension( 100, 300 ); } } public class Gradient extends JPanel{//间层预览 public Color G_color_left = new Color(255,255,255); public Color G_color_right = new Color(0,0,0); public Gradient(){ repaint(); } public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; g2d.setPaint( new GradientPaint( 0, 0, G_color_left, 100, 0, G_color_right, true ) ); g2d.fill( new Rectangle2D.Double( 0, 0, 100, 25 ) ); } public Dimension getPreferredSize(){ return new Dimension( 100, 25 ); } } public class ColorPanel extends JPanel implements MouseListener,ActionListener{//调色盘class private JPanel jPanel_color0[]=new JPanel[5]; private JPanel jPanel_color1[]=new JPanel[32]; private JPanel jPanel_color2[]=new JPanel[32]; private ImageIcon special_color[]= new ImageIcon[4]; private BufferedImage bufImg = new BufferedImage(12 ,12,BufferedImage.TYPE_3BYTE_BGR) ,bufImg2 = new BufferedImage(12 ,12,BufferedImage.TYPE_3BYTE_BGR); private JLabel jlbImg=new JLabel() ,jlbImg2=new JLabel(); private ImageIcon icon; private JDialog jDialog; private JButton ok, cancel,left,right; private Gradient center = new Gradient(); private int rgb[][]={ {0,255,128,192,128,255,128,255, 0, 0, 0, 0, 0, 0,128,255,128,255, 0, 0, 0,128, 0,128,128,255,128,255,255,255,255,255}, {0,255,128,192, 0, 0,128,255,128,255,128,255, 0, 0, 0, 0,128,255, 64,255,128,255, 64,128, 0, 0, 64,128,255,255,255,255}, {0,255,128,192, 0, 0, 0, 0, 0, 0,128,255,128,255,128,255, 64,128, 64,128,255,255,128,255,255,128, 0, 64,255,255,255,255} }; public ColorPanel(){//产生版面// addMouseListener( this ); jlbImg.setIcon(new ImageIcon(bufImg)); jlbImg2.setIcon(new ImageIcon(bufImg2)); special_color[0] = new ImageIcon( "img/icon1.gif" ); special_color[1] = new ImageIcon( "img/icon2.gif" ); special_color[2] = new ImageIcon( "img/icon3.gif" ); special_color[3] = new ImageIcon( "img/icon4.gif" ); this.setLayout(null); color_border=new Color(0,0,0); color_inside=null; for(i=0;i<jPanel_color0.length;i++){ jPanel_color0[i]=new JPanel(); if(i<=2){ jPanel_color0[i].setBorder(BorderFactory.createEtchedBorder(BevelBorder.RAISED)); jPanel_color0[i].setLayout(null); } else{ jPanel_color0[i].setBackground(new Color(rgb[0][i-3],rgb[1][i-3],rgb[2][i-3])); jPanel_color0[i].setLayout(new GridLayout(1,1)); jPanel_color0[i-2].add(jPanel_color0[i]); } } for(i=0;i<jPanel_color2.length;i++){ jPanel_color2[i]=new JPanel(); jPanel_color2[i].setLayout(new GridLayout(1,1)); jPanel_color2[i].setBounds(new Rectangle(2, 2, 12, 12)); jPanel_color2[i].setBackground(new Color(rgb[0][i],rgb[1][i],rgb[2][i])); if(i>=28) jPanel_color2[i].add(new JLabel(special_color[i-28])); } for(i=0;i<jPanel_color1.length;i++){ jPanel_color1[i]=new JPanel(); jPanel_color1[i].setLayout(null); jPanel_color1[i].add(jPanel_color2[i]); this.add(jPanel_color1[i]); if(i%2==0){jPanel_color1[i].setBounds(new Rectangle(32+i/2*16, 0, 16, 16));} else{jPanel_color1[i].setBounds(new Rectangle(32+i/2*16, 16, 16, 16));} jPanel_color1[i].setBorder(BorderFactory.createEtchedBorder(BevelBorder.RAISED)); } jPanel_color0[3].add(jlbImg); jPanel_color0[4].add(jlbImg2); Graphics2D g2d = bufImg2.createGraphics(); g2d.setPaint( Color.white ); g2d.fill( new Rectangle2D.Double( 0, 0, 12, 12 ) ); g2d.setPaint( Color.red ); g2d.draw( new Line2D.Double( 0, 0, 12, 12 ) ); g2d.draw( new Line2D.Double( 11, 0, 0, 11 ) ); repaint(); this.add(jPanel_color0[1]); this.add(jPanel_color0[2]); this.add(jPanel_color0[0]); jPanel_color0[0].setBounds(new Rectangle(0, 0, 32, 32)); jPanel_color0[1].setBounds(new Rectangle(4, 4, 16, 16)); jPanel_color0[2].setBounds(new Rectangle(12,12,16, 16)); jPanel_color0[3].setBounds(new Rectangle(2, 2, 12, 12)); jPanel_color0[4].setBounds(new Rectangle(2, 2, 12, 12)); jDialog = new JDialog(Painter.this, "请选择两种颜色作间层", true); jDialog.getContentPane().setLayout(new FlowLayout()); jDialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE ); jDialog.setSize(250, 110); JPanel temp = new JPanel(new GridLayout(2,1)); JPanel up = new JPanel(new FlowLayout()); JPanel down = new JPanel(new FlowLayout()); ok = new JButton("确定"); cancel = new JButton("取消"); left = new JButton(" "); right = new JButton(" "); center.setBorder(BorderFactory.createEtchedBorder(BevelBorder.RAISED)); up.add(left); up.add(center); up.add(right); down.add(ok); down.add(cancel); temp.add(up); temp.add(down); jDialog.getContentPane().add(temp); ok.addActionListener(this); cancel.addActionListener(this); left.addActionListener(this); right.addActionListener(this); } public void actionPerformed( ActionEvent e ){ if(e.getSource() == left){ center.G_color_left = JColorChooser.showDialog( Painter.this, "请选择边线颜色", center.G_color_left ); center.repaint(); } else if(e.getSource() == right){ center.G_color_right = JColorChooser.showDialog( Painter.this, "请选择边线颜色", center.G_color_right ); center.repaint(); } else{ jDialog.dispose(); } } public Dimension getPreferredSize(){ return new Dimension( 300, 32 ); } public void mouseClicked( MouseEvent e ){} public void mousePressed( MouseEvent e ){ Graphics2D g2d; if(e.getX()>=5 && e.getX()<=20 && e.getY()>=5 && e.getY()<=20){ g2d = bufImg.createGraphics(); color_border = JColorChooser.showDialog( Painter.this, "请选择边线颜色", (Color)color_border ); g2d.setPaint(color_border); g2d.fill( new Rectangle2D.Double( 0, 0, 12, 12 ) ); repaint(); } else if(e.getX()>=13 && e.getX()<=28 && e.getY()>=13 && e.getY()<=28){ g2d = bufImg2.createGraphics(); color_inside = JColorChooser.showDialog( Painter.this, "请选择填充颜色", (Color)color_inside ); g2d.setPaint(color_inside); g2d.fill( new Rectangle2D.Double( 0, 0, 12, 12 ) ); repaint(); } if(!(e.getX()>=32 && e.getX()<=288)) return; int choose=(e.getX()-32)/16*2+e.getY()/16; if(e.getButton()==1)//判断填充边框或是否填满内部 g2d = bufImg.createGraphics(); else g2d = bufImg2.createGraphics();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -