做一个用鼠标绘图的java程序
要求很简单,可以通过鼠标操作,可以用直线,圆等基本几何图形画图的程序。另外除了可以绘图外,还要可以实现“橡皮擦”的功能。网上的类似程序看了很多,没有完全符合这一要求的,希...
要求很简单,可以通过鼠标操作,可以用直线,圆等基本几何图形画图的程序。
另外除了可以绘图外,还要可以实现“橡皮擦”的功能。
网上的类似程序看了很多,没有完全符合这一要求的,希望有高手帮忙写一段~~不胜感激
没啥想说的了,请愿意帮忙的朋友帮帮忙,不胜感激 展开
另外除了可以绘图外,还要可以实现“橡皮擦”的功能。
网上的类似程序看了很多,没有完全符合这一要求的,希望有高手帮忙写一段~~不胜感激
没啥想说的了,请愿意帮忙的朋友帮帮忙,不胜感激 展开
4个回答
展开全部
写了一个最基本的,可以照了代码改复杂,功能都实现没有问题。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class DrawImage extends JFrame{
private Container c = null;
private JButton lineButton = null,
rectangleButton = null,
ovalButton = null,
colorChooser=null,
clearButton = null;
private JTextField textField = null;
private static int DRAW_LINE=0,
DRAW_RECTANGLE=1,
DRAW_OVAL=2,
CLEAR=3;
private int drawMode = DRAW_RECTANGLE;
private int lineWidth = 5;
private Color color = Color.RED;
private JPanel drawPane,p=null;
private DrawImage frame = null;
private ActionListener buttonListener=null;
private MouseListener mouseListener = null;
private Point lp=new Point(-1,-1), //鼠标落下的点
cp=new Point(-1,-1); //鼠标拖拽的点
public DrawImage(){
frame = this;
initListener();
initFrame();
}
public void initListener(){
buttonListener = new ActionListener(){
public void actionPerformed(ActionEvent e) {
String str = ((JButton)e.getSource()).getText();
if(str.compareTo("画线")==0){
drawMode = DRAW_LINE;
}else if(str.compareTo("方形")==0){
drawMode = DRAW_RECTANGLE;
}else if(str.compareTo("画圆")==0){
drawMode = DRAW_OVAL;
}else if(str.compareTo("擦除")==0){
drawMode = CLEAR;
}else if(str.compareTo("颜色")==0){
color = JColorChooser.showDialog(frame, "取色板",color);
colorChooser.setBackground(color);
}
}
};
mouseListener = new MouseListener(){
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
lp = e.getPoint();
}
public void mouseReleased(MouseEvent e) {
cp = e.getPoint();
drawPane.repaint();
}
};
}
public void initFrame(){
c=this.getContentPane();
lineButton = new JButton("画线");
rectangleButton = new JButton("方形");
ovalButton = new JButton("画圆");
clearButton = new JButton("擦除");
colorChooser = new JButton("颜色");
colorChooser.setBackground(color);
textField = new JTextField(5);
drawPane = new JPanel(){
public void paint(Graphics g){
g.setColor(color);
if(drawMode==DRAW_LINE){
g.drawLine(lp.x,lp.y, cp.x, cp.y);
}else if(drawMode==DRAW_RECTANGLE){
g.drawRect(lp.x, lp.y, Math.abs(lp.x-cp.x), Math.abs(lp.y-cp.y));
}else if(drawMode==DRAW_OVAL){
g.drawOval(lp.x, lp.y, Math.abs(lp.x-cp.x), Math.abs(lp.y-cp.y));
}else if(drawMode==CLEAR){
g.clearRect(lp.x, lp.y, Math.abs(lp.x-cp.x), Math.abs(lp.y-cp.y));
}
}
};
p = new JPanel();
p.setPreferredSize(new Dimension(100,800));
p.setLayout(new FlowLayout(FlowLayout.LEADING));
p.add(lineButton);
p.add(rectangleButton);
p.add(ovalButton);
p.add(clearButton);
p.add(colorChooser);
p.add(textField);
lineButton.addActionListener(buttonListener);
rectangleButton.addActionListener(buttonListener);
ovalButton.addActionListener(buttonListener);
colorChooser.addActionListener(buttonListener);
clearButton.addActionListener(buttonListener);
drawPane.addMouseListener(mouseListener);
drawPane.setBorder(BorderFactory.createLineBorder(Color.red,3));
c.add(p,BorderLayout.WEST);
c.add(drawPane,BorderLayout.CENTER);
}
public static void main(String[] args){
DrawImage frame = new DrawImage();
frame.setVisible(true);
frame.setSize(600,600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class DrawImage extends JFrame{
private Container c = null;
private JButton lineButton = null,
rectangleButton = null,
ovalButton = null,
colorChooser=null,
clearButton = null;
private JTextField textField = null;
private static int DRAW_LINE=0,
DRAW_RECTANGLE=1,
DRAW_OVAL=2,
CLEAR=3;
private int drawMode = DRAW_RECTANGLE;
private int lineWidth = 5;
private Color color = Color.RED;
private JPanel drawPane,p=null;
private DrawImage frame = null;
private ActionListener buttonListener=null;
private MouseListener mouseListener = null;
private Point lp=new Point(-1,-1), //鼠标落下的点
cp=new Point(-1,-1); //鼠标拖拽的点
public DrawImage(){
frame = this;
initListener();
initFrame();
}
public void initListener(){
buttonListener = new ActionListener(){
public void actionPerformed(ActionEvent e) {
String str = ((JButton)e.getSource()).getText();
if(str.compareTo("画线")==0){
drawMode = DRAW_LINE;
}else if(str.compareTo("方形")==0){
drawMode = DRAW_RECTANGLE;
}else if(str.compareTo("画圆")==0){
drawMode = DRAW_OVAL;
}else if(str.compareTo("擦除")==0){
drawMode = CLEAR;
}else if(str.compareTo("颜色")==0){
color = JColorChooser.showDialog(frame, "取色板",color);
colorChooser.setBackground(color);
}
}
};
mouseListener = new MouseListener(){
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
lp = e.getPoint();
}
public void mouseReleased(MouseEvent e) {
cp = e.getPoint();
drawPane.repaint();
}
};
}
public void initFrame(){
c=this.getContentPane();
lineButton = new JButton("画线");
rectangleButton = new JButton("方形");
ovalButton = new JButton("画圆");
clearButton = new JButton("擦除");
colorChooser = new JButton("颜色");
colorChooser.setBackground(color);
textField = new JTextField(5);
drawPane = new JPanel(){
public void paint(Graphics g){
g.setColor(color);
if(drawMode==DRAW_LINE){
g.drawLine(lp.x,lp.y, cp.x, cp.y);
}else if(drawMode==DRAW_RECTANGLE){
g.drawRect(lp.x, lp.y, Math.abs(lp.x-cp.x), Math.abs(lp.y-cp.y));
}else if(drawMode==DRAW_OVAL){
g.drawOval(lp.x, lp.y, Math.abs(lp.x-cp.x), Math.abs(lp.y-cp.y));
}else if(drawMode==CLEAR){
g.clearRect(lp.x, lp.y, Math.abs(lp.x-cp.x), Math.abs(lp.y-cp.y));
}
}
};
p = new JPanel();
p.setPreferredSize(new Dimension(100,800));
p.setLayout(new FlowLayout(FlowLayout.LEADING));
p.add(lineButton);
p.add(rectangleButton);
p.add(ovalButton);
p.add(clearButton);
p.add(colorChooser);
p.add(textField);
lineButton.addActionListener(buttonListener);
rectangleButton.addActionListener(buttonListener);
ovalButton.addActionListener(buttonListener);
colorChooser.addActionListener(buttonListener);
clearButton.addActionListener(buttonListener);
drawPane.addMouseListener(mouseListener);
drawPane.setBorder(BorderFactory.createLineBorder(Color.red,3));
c.add(p,BorderLayout.WEST);
c.add(drawPane,BorderLayout.CENTER);
}
public static void main(String[] args){
DrawImage frame = new DrawImage();
frame.setVisible(true);
frame.setSize(600,600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
展开全部
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class DrawImage extends JFrame{
private Container c = null;
private JButton lineButton = null,
rectangleButton = null,
ovalButton = null,
colorChooser=null,
clearButton = null;
private JTextField textField = null;
private static int DRAW_LINE=0,
DRAW_RECTANGLE=1,
DRAW_OVAL=2,
CLEAR=3;
private int drawMode = DRAW_RECTANGLE;
private int lineWidth = 5;
private Color color = Color.RED;
private JPanel drawPane,p=null;
private DrawImage frame = null;
private ActionListener buttonListener=null;
private MouseListener mouseListener = null;
private Point lp=new Point(-1,-1), //鼠标落下的点
cp=new Point(-1,-1); //鼠标拖拽的点
public DrawImage(){
frame = this;
initListener();
initFrame();
}
public void initListener(){
buttonListener = new ActionListener(){
public void actionPerformed(ActionEvent e) {
String str = ((JButton)e.getSource()).getText();
if(str.compareTo("画线")==0){
drawMode = DRAW_LINE;
}else if(str.compareTo("方形")==0){
drawMode = DRAW_RECTANGLE;
}else if(str.compareTo("画圆")==0){
drawMode = DRAW_OVAL;
}else if(str.compareTo("擦除")==0){
drawMode = CLEAR;
}else if(str.compareTo("颜色")==0){
color = JColorChooser.showDialog(frame, "取色板",color);
colorChooser.setBackground(color);
}
}
};
mouseListener = new MouseListener(){
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
lp = e.getPoint();
}
public void mouseReleased(MouseEvent e) {
cp = e.getPoint();
drawPane.repaint();
}
};
}
public void initFrame(){
c=this.getContentPane();
lineButton = new JButton("画线");
rectangleButton = new JButton("方形");
ovalButton = new JButton("画圆");
clearButton = new JButton("擦除");
colorChooser = new JButton("颜色");
colorChooser.setBackground(color);
textField = new JTextField(5);
drawPane = new JPanel(){
public void paint(Graphics g){
g.setColor(color);
if(drawMode==DRAW_LINE){
g.drawLine(lp.x,lp.y, cp.x, cp.y);
}else if(drawMode==DRAW_RECTANGLE){
g.drawRect(lp.x, lp.y, Math.abs(lp.x-cp.x), Math.abs(lp.y-cp.y));
}else if(drawMode==DRAW_OVAL){
g.drawOval(lp.x, lp.y, Math.abs(lp.x-cp.x), Math.abs(lp.y-cp.y));
}else if(drawMode==CLEAR){
g.clearRect(lp.x, lp.y, Math.abs(lp.x-cp.x), Math.abs(lp.y-cp.y));
}
}
};
p = new JPanel();
p.setPreferredSize(new Dimension(100,800));
p.setLayout(new FlowLayout(FlowLayout.LEADING));
p.add(lineButton);
p.add(rectangleButton);
p.add(ovalButton);
p.add(clearButton);
p.add(colorChooser);
p.add(textField);
lineButton.addActionListener(buttonListener);
rectangleButton.addActionListener(buttonListener);
ovalButton.addActionListener(buttonListener);
colorChooser.addActionListener(buttonListener);
clearButton.addActionListener(buttonListener);
drawPane.addMouseListener(mouseListener);
drawPane.setBorder(BorderFactory.createLineBorder(Color.red,3));
c.add(p,BorderLayout.WEST);
c.add(drawPane,BorderLayout.CENTER);
}
public static void main(String[] args){
DrawImage frame = new DrawImage();
frame.setVisible(true);
frame.setSize(600,600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class DrawImage extends JFrame{
private Container c = null;
private JButton lineButton = null,
rectangleButton = null,
ovalButton = null,
colorChooser=null,
clearButton = null;
private JTextField textField = null;
private static int DRAW_LINE=0,
DRAW_RECTANGLE=1,
DRAW_OVAL=2,
CLEAR=3;
private int drawMode = DRAW_RECTANGLE;
private int lineWidth = 5;
private Color color = Color.RED;
private JPanel drawPane,p=null;
private DrawImage frame = null;
private ActionListener buttonListener=null;
private MouseListener mouseListener = null;
private Point lp=new Point(-1,-1), //鼠标落下的点
cp=new Point(-1,-1); //鼠标拖拽的点
public DrawImage(){
frame = this;
initListener();
initFrame();
}
public void initListener(){
buttonListener = new ActionListener(){
public void actionPerformed(ActionEvent e) {
String str = ((JButton)e.getSource()).getText();
if(str.compareTo("画线")==0){
drawMode = DRAW_LINE;
}else if(str.compareTo("方形")==0){
drawMode = DRAW_RECTANGLE;
}else if(str.compareTo("画圆")==0){
drawMode = DRAW_OVAL;
}else if(str.compareTo("擦除")==0){
drawMode = CLEAR;
}else if(str.compareTo("颜色")==0){
color = JColorChooser.showDialog(frame, "取色板",color);
colorChooser.setBackground(color);
}
}
};
mouseListener = new MouseListener(){
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
lp = e.getPoint();
}
public void mouseReleased(MouseEvent e) {
cp = e.getPoint();
drawPane.repaint();
}
};
}
public void initFrame(){
c=this.getContentPane();
lineButton = new JButton("画线");
rectangleButton = new JButton("方形");
ovalButton = new JButton("画圆");
clearButton = new JButton("擦除");
colorChooser = new JButton("颜色");
colorChooser.setBackground(color);
textField = new JTextField(5);
drawPane = new JPanel(){
public void paint(Graphics g){
g.setColor(color);
if(drawMode==DRAW_LINE){
g.drawLine(lp.x,lp.y, cp.x, cp.y);
}else if(drawMode==DRAW_RECTANGLE){
g.drawRect(lp.x, lp.y, Math.abs(lp.x-cp.x), Math.abs(lp.y-cp.y));
}else if(drawMode==DRAW_OVAL){
g.drawOval(lp.x, lp.y, Math.abs(lp.x-cp.x), Math.abs(lp.y-cp.y));
}else if(drawMode==CLEAR){
g.clearRect(lp.x, lp.y, Math.abs(lp.x-cp.x), Math.abs(lp.y-cp.y));
}
}
};
p = new JPanel();
p.setPreferredSize(new Dimension(100,800));
p.setLayout(new FlowLayout(FlowLayout.LEADING));
p.add(lineButton);
p.add(rectangleButton);
p.add(ovalButton);
p.add(clearButton);
p.add(colorChooser);
p.add(textField);
lineButton.addActionListener(buttonListener);
rectangleButton.addActionListener(buttonListener);
ovalButton.addActionListener(buttonListener);
colorChooser.addActionListener(buttonListener);
clearButton.addActionListener(buttonListener);
drawPane.addMouseListener(mouseListener);
drawPane.setBorder(BorderFactory.createLineBorder(Color.red,3));
c.add(p,BorderLayout.WEST);
c.add(drawPane,BorderLayout.CENTER);
}
public static void main(String[] args){
DrawImage frame = new DrawImage();
frame.setVisible(true);
frame.setSize(600,600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
开四个变量currentX, currentY, previousX, previousY, 把每次鼠标所在点存在变量currentX, currentY中,下一次存的时候就把currentX和currentY的值放到previousX和previousY中,这样就能方便的访问倒数第二个点的坐标了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2011-01-22
展开全部
这个不简单,看来用分很难找到人帮你!
如果用RMB来解决,我想一定能行。
QQ:40398857
如果用RMB来解决,我想一定能行。
QQ:40398857
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询