java鼠标画线代码 java实现鼠标拖动画矩形
用JAVA实现画线,不是直线,而是点住鼠标左键划到哪里哪里就画线,就
import java.awt.BasicStroke;
公司主营业务:成都网站设计、成都做网站、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联公司推出固始免费做网站回馈大家。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MouseDrawPanel extends Frame
{
private static final long serialVersionUID = 1L;
MouseEvent e = null;
int startX = -1;
int startY = -1;
int endX = -1;
int endY = -1;
boolean left = false;
public MouseDrawPanel ( String title )
{
setTitle (title);
setLayout (new BorderLayout ());
setResizable (false);
setSize (500, 400);
setLocationRelativeTo (null);
addWindowListener (new WindowAdapter ()
{
@Override
public void windowClosing ( WindowEvent e )
{
System.exit (0);
}
});
}
@Override
public void paintComponents ( Graphics g )
{
float lineWidth = 8.0f;
( (Graphics2D) g ).setStroke (new BasicStroke (lineWidth));
g.setColor (Color.YELLOW);
g.drawLine (startX, startY, endX, endY);
g.dispose ();
}
public static void main ( String[] args )
{
final MouseDrawPanel mdp = new MouseDrawPanel ("Use Mouse Draw");
Panel panel = new Panel ();
panel.setLayout (new FlowLayout (FlowLayout.LEFT));
Label startL = new Label ("start: ");
Label endL = new Label ("end: ");
final Label nowL = new Label ("now: ");
final Label startR = new Label ("000,000");
final Label endR = new Label ("000,000");
final Label nowN = new Label ("000,000");
panel.add (startL);
panel.add (startR);
panel.add (endL);
panel.add (endR);
panel.add (nowL);
panel.add (nowN);
mdp.add (panel, "South");
mdp.addMouseMotionListener (new MouseMotionListener ()
{
@Override
public void mouseMoved ( MouseEvent e )
{
if (mdp.left)
{
nowN.setText (e.getX () + " , " + e.getY ());
}
}
@Override
public void mouseDragged ( MouseEvent e )
{
if (mdp.left)
{
mdp.endX = e.getX ();
mdp.endY = e.getY ();
mdp.paintComponents (mdp.getGraphics ());
mdp.startX = mdp.endX;
mdp.startY = mdp.endY;
endR.setText (mdp.endX + " , " + mdp.endY);
}
}
});
mdp.addMouseListener (new MouseAdapter ()
{
@Override
public void mousePressed ( MouseEvent e )
{
if (e.getButton () == MouseEvent.BUTTON1)
{
mdp.startX = e.getX ();
mdp.startY = e.getY ();
startR.setText (mdp.startX + " , " + mdp.startY);
mdp.left = true;
}
else
{
mdp.left = false;
}
}
@Override
public void mouseReleased ( MouseEvent e )
{
if (mdp.left)
{
endR.setText (e.getX () + " , " + e.getY ());
}
}
});
mdp.setVisible (true);
}
}
实现鼠标画图的Java程序
import java awt Color;
import java awt Frame;
import java awt Graphics;
import java awt Point;
import java awt event MouseAdapter;
import java awt event MouseEvent;
import java awt event MouseListener;
import java util ArrayList;
import java util Iterator;
public class MyMouseAdapter {
public static void main(String[] args) {
new MyFrame ( drawing );
}
}
class MyFrame extends Frame{
ArrayList points=null;
MyFrame (String s){
super(s);
points=new ArrayList();
setLayout(null);
setBounds( );
this setBackground(new Color( ));
setVisible(true);
this addMouseListener(new Monitor ());
}
public void paint(Graphics g){
Iterator i=erator();
while(i hasNext()){
Point p=(Point)i next();
g setColor(Color blue);
g fillOval(p x p y );
}
}
public void addPoint(Point p){
points add(p);
}
}
class Monitor extends MouseAdapter{ //MouseAdapter实现了MouseListener接口
public void mousePressed(MouseEvent e){
MyFrame f=(MyFrame )e getSource();
f addPoint(new Point(e getX() e getY()));
f repaint(); //让Frame强制经行重画
}
lishixinzhi/Article/program/Java/hx/201311/26376
实现java程序,要求单击鼠标画线,双击鼠标擦除所有画的线。
package zhidao;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MouseDrawPanel extends Frame
{
private static final long serialVersionUID = 1L;
MouseEvent e = null;
static int startX = -1;
static int startY = -1;
static int endX = -1;
static int endY = -1;
public MouseDrawPanel ( String title )
{
setTitle (title);
setLayout (new BorderLayout ());
setResizable (false);
setSize (500, 400);
setLocationRelativeTo (null);
addWindowListener (new WindowAdapter ()
{
@Override
public void windowClosing ( WindowEvent e )
{
System.exit (0);
}
});
}
@Override
public void paintComponents ( Graphics g )
{
g.setColor (Color.RED);
g.drawLine (startX, startY, endX, endY);
g.dispose ();
}
public static void main ( String[] args )
{
final MouseDrawPanel mdp = new MouseDrawPanel ("Use Mouse Draw");
Panel panel = new Panel ();
panel.setLayout (new FlowLayout (FlowLayout.LEFT));
Label startL = new Label ("start: ");
Label endL = new Label ("end: ");
final Label nowL = new Label ("now: ");
final Label startR = new Label ("000,000");
final Label endR = new Label ("000,000");
final Label nowN = new Label ("000,000");
panel.add (startL);
panel.add (startR);
panel.add (endL);
panel.add (endR);
panel.add (nowL);
panel.add (nowN);
mdp.add (panel, "South");
mdp.addMouseMotionListener (new MouseMotionListener ()
{
@Override
public void mouseMoved ( MouseEvent e )
{
nowN.setText (e.getX () + " , " + e.getY ());
}
@Override
public void mouseDragged ( MouseEvent e )
{
endX = e.getX ();
endY = e.getY ();
mdp.paintComponents (mdp.getGraphics ());
startX = endX;
startY = endY;
endR.setText (endX + " , " + endY);
}
});
mdp.addMouseListener (new MouseAdapter ()
{
@Override
public void mousePressed ( MouseEvent e )
{
startX = e.getX ();
startY = e.getY ();
startR.setText (startX + " , " + startY);
}
@Override
public void mouseReleased ( MouseEvent e )
{
endR.setText (e.getX () + " , " + e.getY ());
}
});
mdp.setVisible (true);
}
}
java 绘图板 如何实现画直线的功能
这个简单
可以调用方法drawline(int x1, int y1, int x2, int y2)
其中(x1, y1), (x2, y2)分别为直线起点和终点的坐标
特意给你写了个小例子,希望能帮到你
***************************************
import javax.swing.*;
import java.awt.*;
public class DrawLine extends JPanel {
public static void main(String[] args) {
JFrame frame = new JFrame("DrawLine");
frame.getContentPane().add(new DrawLine());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setVisible(true);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(50, 50, 200, 250);
}
}
****************************
将程序复制到记事本中并改名为DrawLine.java(注意大小写)
然后在命令行中用 CD+文件夹 使命令范围到DrawLine.java的文件夹中
然后执行命令javac DrawLine.java
再执行命令java DrawLine
你会看到结果
小弟菜鸟一枚java鼠标跟随问题,我想让下面代码在鼠标“快速”移动时画完整的线,不是断断续续的哟!
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
public class MouseDrawLine extends JFrame {
int x, y;
int endX, endY;
public MouseDrawLine() {
addMouseListener(new MouseAdapter() {
//鼠标下压事件
@Override
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
}
//鼠标弹起事件
@Override
public void mouseReleased(MouseEvent e) {
System.out.println("鼠标弹起事件");
}
});
addMouseMotionListener(new MouseMotionAdapter() {
//鼠标下压移动事件
@Override
public void mouseDragged(MouseEvent e) {
Graphics g = getGraphics();
g.setColor(Color.BLACK);
endX = e.getX();
endY = e.getY();
g.drawLine(x, y, endX, endY);
x = endX;
y = endY;
}
});
setSize(600, 500);
setLocation(200,100);
setVisible(true);
setDefaultCloseOperation(3);
}
public static void main(String[] args) {
new MouseDrawLine();
}
}
网页题目:java鼠标画线代码 java实现鼠标拖动画矩形
本文路径:http://azwzsj.com/article/hgdecd.html