鼠标画图编程
⑴ Windows编程中如何用鼠标画多边形
1.多边形数据结构可以用链表
2.鼠标左键事件添加节点到多边形链表,启动绘制多边形
3.鼠标移动事件,先擦除先前画的(xor)最后一点与鼠标点和第一点与鼠标点之间的连线再画上当前鼠标点与第一最后一点连线,记录鼠标坐标点。
4.鼠标右键事件响应擦除最后一点与倒数第二点,
以及最后一点与鼠标点之间的连线,从多边形坐标点中移出最后一点,启动多边形绘制
5.左键双击事件,当前鼠标点加入多边形链表,结束鼠标绘制事件的响应多边形链表数据处理保存绘制。
⑵ 做一个用鼠标绘图的java程序
写了一个最基本的,可以照了代码改复杂,功能都实现没有问题。
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);
}
}
⑶ java 鼠标画图
上学的时候写过,不过不在公司的电脑里面。呵呵,回去想到了给你……
package guitest.myboard;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
//the point
//impress the info of one point,the x and y
class OnePoint implements Serializable {
int x;
int y;
int tool;
Color c;
int border;
public OnePoint(int x,int y,int tool,Color cc,int border){
this.x=x;
this.y=y;
this.tool=tool;
this.c=cc;
this.border=border;
}
}
class DrawingBoard extends Frame implements MouseListener,ItemListener,ActionListener,MouseMotionListener{
Button pen;
Button line ;
Button ellipse ;
Button rect ;
Button clear ;
Button colorboard ;
Button storebutton;
Button openbutton;
Choice sizechoice ;
Choice colorchoice ;
Label pensize;
Label pencolor;
Panel panel ;
FileDialog storefile;
FileDialog openfile;
FileInputStream filein;
FileOutputStream fileout;
ObjectInputStream objectin;
ObjectOutputStream objectout;
int flagtool=0;
Color flagcolor;
int border;
BasicStroke size;
OnePoint p1,p2;
Vector<OnePoint> points=new Vector<OnePoint>();
public DrawingBoard(){
pen=new Button("画笔");
line=new Button("直线");
ellipse=new Button("圆");
rect=new Button("矩形");
clear=new Button("清除");
colorboard=new Button("调色板");
storebutton=new Button("存储文件");
openbutton=new Button("打开文件");
pensize=new Label("画笔大小");
pencolor=new Label("画笔颜色");
storefile=new FileDialog(this,"存储文件",FileDialog.SAVE);
storefile.setVisible(false);
storefile.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
storefile.setVisible(false);
}
});
openfile=new FileDialog(this,"打开文件",FileDialog.LOAD);
openfile.setVisible(false);
openfile.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
openfile.setVisible(false);
}
});
sizechoice=new Choice();
sizechoice.add("1");
sizechoice.add("2");
sizechoice.add("4");
sizechoice.add("6");
sizechoice.add("8");
sizechoice.addItemListener(this);
colorchoice=new Choice();
colorchoice.add("black");
colorchoice.add("red");
colorchoice.add("blue");
colorchoice.add("green");
colorchoice.addItemListener(this);
pen.addActionListener(this);
line.addActionListener(this);
ellipse.addActionListener(this);
rect.addActionListener(this);
clear.addActionListener(this);
colorboard.addActionListener(this);
storebutton.addActionListener(this);
openbutton.addActionListener(this);
panel=new Panel();
panel.add(storebutton);
panel.add(openbutton);
panel.add(pen);
panel.add(line);
panel.add(ellipse);
panel.add(rect);
panel.add(clear);
panel.add(sizechoice);
panel.add(pensize);
panel.add(colorchoice);
panel.add(pencolor);
panel.add(colorboard);
add(panel,BorderLayout.NORTH);
setBounds(100,100,700,600);
setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
/**
* 添加鼠标事件的监听器,否则,鼠标的移动和点击都将无法识别!
* */
addMouseListener(this);
addMouseMotionListener(this);
}
public void paint(Graphics g){
Graphics2D g2d=(Graphics2D)g;
if(flagtool==2){ //qing chu
g.clearRect(0,0,getSize().width,getSize().height);
}
for(int i=0;i<points.size()-1;i++){
p1=(OnePoint)points.elementAt(i);
p2=(OnePoint)points.elementAt(i+1);
g2d.setColor(p1.c); //////////////需要使用Graphics2D从Graphics类中继承下来的方法 setColor()设置当前的颜色
size=new BasicStroke(p1.border,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
g2d.setStroke(size);
if(p1.tool==p2.tool){
switch(p1.tool){
case 0:
Line2D.Double line1=new Line2D.Double(p1.x,p1.y,p2.x,p2.y);
g2d.draw(line1);
break;
case 1:
Line2D.Double line2=new Line2D.Double(p1.x,p1.y,p2.x,p2.y);
g2d.draw(line2);
break;
case 3:
Ellipse2D.Double ellipse=new Ellipse2D.Double(p1.x,p1.y,Math.abs(p2.x-p1.x),Math.abs(p2.y-p1.y));
g2d.draw(ellipse);
break;
case 4:
Rectangle2D.Double rect=new Rectangle2D.Double(p1.x,p1.y,Math.abs(p2.x-p1.x),Math.abs(p2.y-p1.y));
g2d.draw(rect);
break;
default:
}
}
}
}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) { //鼠标点下时候,将当前的点信息记录
OnePoint pp1=new OnePoint(e.getX(),e.getY(),flagtool,flagcolor,border);
points.addElement(pp1);
//repaint();
}
public void mouseReleased(MouseEvent e) {//鼠标松开时候,如果是画笔,则当前截断,是其余状态记下一枚点信息
if(flagtool==0){
points.addElement(new OnePoint(-1,-1,22,flagcolor,border));
}
else{
OnePoint pp2=new OnePoint(e.getX(),e.getY(),flagtool,flagcolor,border);
points.addElement(pp2);
points.add(new OnePoint(-1,-1,22,flagcolor,border));
}
repaint();
}
public void itemStateChanged(ItemEvent e) {
if(e.getSource()==colorchoice){
String selected=colorchoice.getSelectedItem();
if(selected=="black"){
flagcolor=new Color(0,0,0);
}
else if(selected=="red"){
flagcolor=new Color(255,0,0);
}
else if(selected=="blue"){
flagcolor=new Color(0,0,255);
}
else if(selected=="green"){
flagcolor=new Color(0,255,0);
}
}
else if(e.getSource()==sizechoice){
String selected=sizechoice.getSelectedItem();
if (selected=="1"){
border=1;
}
else if(selected=="2"){
border=2*2;
}
else if(selected=="4"){
border=4*2;
}
else if(selected=="6"){
border=6*2;
}
else if(selected=="8"){
border=8*2;
}
}
}
public void update(Graphics g) { //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
paint(g);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==pen){
flagtool=0;
}
else if(e.getSource()==line){
flagtool=1;
}
else if(e.getSource()==clear){
flagtool=2;
points.removeAllElements();
repaint(); //此语要有,否则今生无法删除!
}
else if(e.getSource()==ellipse){
flagtool=3;
}
else if(e.getSource()==rect){
flagtool=4;
}
else if(e.getSource()==colorboard){
/*
* 使用 javax.swing.×包中的 JColorChooser 类的静态方法showDialog(Component component,String title,Color color ),
* 该方法的参数,component是当前显示对话框的父框架,color是设置调色板初始的被选颜色
*
* 该方法返回被选的颜色,类型为Color
* */
Color color=JColorChooser.showDialog(this, "调色板",flagcolor);
flagcolor=color;
}
else if(e.getSource()==openbutton){
openfile.setVisible(true);
if(openfile.getFile()!=null){
int temp=flagtool;
flagtool=2;
repaint();
try{
points.removeAllElements();
File file=new File(openfile.getDirectory(),openfile.getFile());
filein=new FileInputStream(file);
objectin=new ObjectInputStream(filein);
points=(Vector)objectin.readObject();
objectin.close();
filein.close();
flagtool=temp;
repaint();
}
catch(Exception ee){
System.out.println(ee.toString());
}
}
}
else if(e.getSource()==storebutton){
storefile.setVisible(true);
if(storefile.getFile()!=null){
try {
File file=new File(storefile.getDirectory(),storefile.getFile());
fileout=new FileOutputStream(file);
objectout=new ObjectOutputStream(fileout);
objectout.writeObject(points);
objectout.close();
fileout.close();
repaint();
}
catch (FileNotFoundException e1) {
System.out.println(e1.toString());
e1.printStackTrace();
} catch (IOException ee) {
System.out.println(ee.toString());
ee.printStackTrace();
}
}
}
}
public void mouseDragged(MouseEvent e) {//鼠标拖动时候,//当且仅当 flagtool==0,或者表示为橡皮的时候
//才将拖动过程中涉及到的点全部记录下来,并且调用repain()方法,重画当前
// TODO Auto-generated method stub
if(flagtool==0){
OnePoint pp3=new OnePoint(e.getX(),e.getY(),flagtool,flagcolor,border);
points.addElement(pp3);
repaint();
}
}
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
}
public class PaintBoard{
public static void main(String[] args){
DrawingBoard oneBorder=new DrawingBoard();
}
}
⑷ CNC画图编程用什么鼠标好
罗技。CNC画图编程对鼠标的要求是最低的,基本上没有要求。计算机辅助设计,本身就是有弥补手工制图的缺点,以及对手工的依赖性的作用在里面。推荐买个普通能用的就行,建议无线鼠标,因为方便。没必要买游戏鼠标,贵且不说,为了高DPI和高刷新率,更加费电,电池用不到多久。所以罗技鼠标就行,便宜也好用。
⑸ 【QT画图】怎么实现用鼠标画图功能
获取鼠标点击的位置然后画一个点,点的粗细可以自己定,这样不断的画点,所有点连起来就是连续的线了
⑹ 如何用C语言编写一个图形编辑器并支持鼠标和画图功能
直接用C语言实现的话很麻烦 在这我说一句要实现一个图形系统是件困难的是一种语言是不够的要结合多种语言 比如C C++ 汇编 matlab
等等!!!还设计很多理论知识 所以比较复杂 你可以用VC++编写一个简单的差不多和windows子带的差不多 VC++环境有很多图形函数可以帮助你很好的实现自己的目的....程序设计的道路是漫长的.....要时刻动手动脑.....编程是枯燥的但是要写好一个程序回头看会有成就感的...祝你成功!!
⑺ 鼠标如何进行编程
在java.awt.*包下面有一个Robot类,可以生成输入事件,例如,Robot.mouseMove 将移动鼠标光标
这个类除了模拟鼠标键盘操作以外,还可以用来截取屏幕,只演示一下怎么模拟鼠标键盘操作,具体api参考javadoc。这个演示完成了弹出QQ和移动窗口的功能。代码如下:
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
/**
* @author bean
*
*/
public class RobotDemo {
private Robot robot = null;
public RobotDemo() {
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
}
/** 可以弹出QQ */
public void keyBoardDemo() {
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_Z);
robot.keyRelease(KeyEvent.VK_Z);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_ALT);
}
/** 前提是有个最大化的窗口,功能是移动到标题栏,然后拖拽到600,600的位置*/
public void mouseDemo(){
robot.mouseMove(80, 10);
robot.mousePress(KeyEvent.BUTTON1_MASK);
try {
⑻ 怎样用matlab编程用鼠标画直线
我以为你想知道的是以下几个问题,
1、画直线,在绘图界面调出绘图编辑工具栏,如下所示:
之后可画直线
2、获得曲线上点,使用ginput命令
如下命令
t=-10:0.1:10;
Y=sin(t).^2.*exp(-0.1*t)-0.5*abs(t);
clf,
plot(t,Y,'r')
hold on
plot(t,zeros(size(t)),'k')
xlabel('t'),ylabel('y(t)')
hold off
zoom on
[tt,yy]=ginput(10);
zoom off;
tt
yy
3、曲线拟合
x=1:100;
y=randn(1,100);
plot(x,y,'*');%散点图
p = polyfit(x,y,1);
hold on
a=p(1)
b=p(2)
z=a*x+b;
plot(x,z)%拟合曲线
hold off
⑼ 怎样用鼠标绘画啊
使用下载的绘图软件例如SAI、PS(Photoshop)之类的,或者是网页里本身带的(例如百田涂鸦板、4399涂鸦板之类的,但相比之前两种还是专业性差了一些,网速不好慎用,很容易卡住,我自己也曾经碰上过明明保存了再打开一塌糊涂的境地。所以建议还是在网上下个绘图软件,个人喜欢SAI,防抖)
鼠标绘画其实很磨时间的。相对于使用板子(事实上板子线稿也很麻烦,不过打草稿简单很多)会难很多。主要就是要不停的修改,橡皮擦和铅笔反复修稿,最终呈现出干净的线稿。至于上色就相对简单一些,至少有前人带领,并且和板绘差不多,可以去SAI吧、PS吧之类的地方转一转,熟悉一下软件的各种性能(其实我也在摸索中),也可以去报个班 。
有耐心、有毅力、有充分的艺术修养影响一幅画的好坏,多多练习才是正道。