package Rong;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class DrawCurve implements MouseMotionListener, ActionListener {
static int ox, oy;
static int flag = 0;
static JFrame f;
public static void main(String arg[]) {
f = new JFrame("鼠标画曲线");
Container p = f.getContentPane();
p.setLayout(new FlowLayout());
JButton red = new JButton("红色");
JButton blue = new JButton("蓝色");
JButton green = new JButton("绿色");
red.addActionListener(new DrawCurve());
blue.addActionListener(new DrawCurve());
green.addActionListener(new DrawCurve());
p.addMouseMotionListener(new DrawCurve());
p.add(red);
p.add(blue);
p.add(green);
f.setSize(400, 400);
f.setVisible(true);
Dimension dim=Toolkit.getDefaultToolkit().getScreenSize();
f.setLocation((dim.width-f.getWidth())/2,(dim.height-f.getHeight())/2);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("红色")) {
flag=1;
}
else if (e.getActionCommand().equals("蓝色")) {
flag=2;
}
else if (e.getActionCommand().equals("绿色")){
flag=3;
}
}
public void mouseDragged(MouseEvent e) {
Container c = (Container) e.getSource();
Graphics g = c.getGraphics();
if (ox >= 0) {
if(flag==0){
g.setColor(Color.black);
g.drawLine(ox, oy, e.getX(), e.getY());
}
else if(flag == 1) {
g.setColor(Color.red);
g.drawLine(ox, oy, e.getX(), e.getY());
}
else if(flag==2){
g.setColor(Color.blue);
g.drawLine(ox, oy, e.getX(), e.getY());
}
else if(flag==3){
g.setColor(Color.green);
g.drawLine(ox, oy, e.getX(), e.getY());
}
}
ox = e.getX();
oy = e.getY();
}
public void mouseMoved(MouseEvent e) {
ox = -1;
oy = -1;
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class DrawCurve implements MouseMotionListener, ActionListener {
static int ox, oy;
static int flag = 0;
static JFrame f;
public static void main(String arg[]) {
f = new JFrame("鼠标画曲线");
Container p = f.getContentPane();
p.setLayout(new FlowLayout());
JButton red = new JButton("红色");
JButton blue = new JButton("蓝色");
JButton green = new JButton("绿色");
red.addActionListener(new DrawCurve());
blue.addActionListener(new DrawCurve());
green.addActionListener(new DrawCurve());
p.addMouseMotionListener(new DrawCurve());
p.add(red);
p.add(blue);
p.add(green);
f.setSize(400, 400);
f.setVisible(true);
Dimension dim=Toolkit.getDefaultToolkit().getScreenSize();
f.setLocation((dim.width-f.getWidth())/2,(dim.height-f.getHeight())/2);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("红色")) {
flag=1;
}
else if (e.getActionCommand().equals("蓝色")) {
flag=2;
}
else if (e.getActionCommand().equals("绿色")){
flag=3;
}
}
public void mouseDragged(MouseEvent e) {
Container c = (Container) e.getSource();
Graphics g = c.getGraphics();
if (ox >= 0) {
if(flag==0){
g.setColor(Color.black);
g.drawLine(ox, oy, e.getX(), e.getY());
}
else if(flag == 1) {
g.setColor(Color.red);
g.drawLine(ox, oy, e.getX(), e.getY());
}
else if(flag==2){
g.setColor(Color.blue);
g.drawLine(ox, oy, e.getX(), e.getY());
}
else if(flag==3){
g.setColor(Color.green);
g.drawLine(ox, oy, e.getX(), e.getY());
}
}
ox = e.getX();
oy = e.getY();
}
public void mouseMoved(MouseEvent e) {
ox = -1;
oy = -1;
}
}