📄 g.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.Vector;
import java.util.*;
import javax.swing.JColorChooser;
import javax.swing.*;
class MyPanel extends Panel implements ActionListener,ItemListener,MouseMotionListener,MouseListener
{
int x1,y1;
int x2,y2;
int x,y;
Vector lines = new Vector();
Vector colors = new Vector();
Vector width=new Vector( );
Button button;
JButton eraser;
Choice choice1;
Choice choice2;
JToolBar bar;
Color newColor=new Color(0,0,0);
int linewidth;
int mode;
boolean flag;
Label label;
MyPanel()
{setLayout(null);
button=new Button("选择颜色");
button.setBackground(Color.red);
button.setBounds(20,20,60,30);
add(button);
button.addActionListener(this);
choice1=new Choice( );
String index[]={"1","2","3","4","5","6","7","8","9","10"};
for(int j=0;j<=9;j++)
choice1.add(index[j]);
choice1.setBounds(100,20,60,30);
choice1.addItemListener(this);
add(choice1);
choice2=new Choice( );
choice2.add("曲线");
choice2.add("直线");
choice2.setBounds(180,20,60,30);
choice2.addItemListener(this);
add(choice2);
addMouseMotionListener(this);
addMouseListener(this);
flag=false;
Icon 橡皮擦=new ImageIcon("b10.gif");
eraser=new JButton(橡皮擦);
eraser.addActionListener(this);
bar=new JToolBar( );
bar.add(eraser);
bar.setBounds(650,20,120,90);
bar.setRollover(true);
add(bar);
eraser.setToolTipText("橡皮擦");
label=new Label("你点我啊!--->");
label.setBounds(562,20,100,30);
add(label);
}
public void paint(Graphics g)
{if(!flag)
{ Graphics2D g_2=(Graphics2D)g;
int np = lines.size();
for (int i=0; i < np; i++)
{String s=(String)width.elementAt(i);
int k=Integer.parseInt(s);
BasicStroke bs=new BasicStroke(k,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
g_2.setStroke(bs);
g.setColor((Color)colors.elementAt(i));
Line2D line=(Line2D)lines.elementAt(i);
g_2.draw(line);
}//end of for
/*if (mode==1)
{
if (x2 != -1)
{
BasicStroke bs=new BasicStroke(linewidth,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
g_2.setStroke(bs);
g.setColor(newColor);
Line2D linee=new Line2D.Double(x1,y1,x2,y2);
g_2.draw(linee);
}
}*/
}
else
{
g.clearRect(x,y,10,10);
repaint( );
int np = lines.size();
for (int i=0; i < np; i++)
{Line2D line=(Line2D)lines.elementAt(i);
if(line.intersects((double)x,(double)y, 10.0,10.0))
{colors.remove(i);
width.remove(i);
lines.remove(i);
}
}
}
}
public void update(Graphics g)
{
paint(g);
}
public void mouseDragged(MouseEvent e) {
e.consume();
x=e.getX( );
y=e.getY( );
switch (mode) {
case 1:
x2 = e.getX();
y2 = e.getY();
break;
case 0:
default:
if(!flag)
{
colors.addElement(newColor);
String s=new String(""+linewidth);
width.addElement(s);
lines.addElement(new Line2D.Double(x1,y1,e.getX(),e.getY( )));
x1 = e.getX();
y1 = e.getY();
repaint( );
break;
}
}
repaint();
}
public void mouseMoved(MouseEvent e){ }
public void mousePressed(MouseEvent e) {
e.consume();
switch (mode) {
case 1:
x1 = e.getX();
y1 = e.getY();
break;
case 0:
default:
if(!flag)
{
x1 = e.getX();
y1 = e.getY();
break;
}
}
}
public void mouseReleased(MouseEvent e) {
e.consume();
switch (mode) {
case 1:
if(!flag)
{
colors.addElement(newColor);
String s=new String(""+linewidth);
width.addElement(s);
lines.addElement(new Line2D.Double(x1,y1,e.getX(),e.getY( )));
x2 = -1;
break;
}
case 0:
default:
break;
}
repaint();
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void actionPerformed(ActionEvent e)
{if(e.getSource( )==button)
newColor=JColorChooser.showDialog(this,"调色板",Color.black);
if(e.getSource( )==eraser)
{
flag=!flag;
if(flag==true)
label.setText("再点击可继续绘图!");
if(flag==false)
label.setText("你点我啊!");
}
}
public void itemStateChanged(ItemEvent e)
{if(e.getSource( )==choice1)
linewidth=choice1.getSelectedIndex( )+1;
if(e.getSource( )==choice2)
mode=choice2.getSelectedIndex( );
}
}
class MyFrame extends Frame
{
MyPanel panel;
MyFrame(String s)
{super(s);
panel=new MyPanel( );
add(panel);
setBounds(0,0,800,500);
setVisible(true);
addWindowListener(new WindowAdapter( )
{public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
}
public class g
{public static void main(String args[])
{new MyFrame("简易绘图板");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -