import?java.awt.BasicStroke;
公司主營(yíng)業(yè)務(wù):成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、移動(dòng)網(wǎng)站開(kāi)發(fā)等業(yè)務(wù)。幫助企業(yè)客戶(hù)真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。成都創(chuàng)新互聯(lián)公司是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開(kāi)放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來(lái)的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶(hù)帶來(lái)驚喜。成都創(chuàng)新互聯(lián)公司推出固始免費(fèi)做網(wǎng)站回饋大家。
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);
}
}
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實(shí)現(xiàn)了MouseListener接口
public void mousePressed(MouseEvent e){
MyFrame f=(MyFrame )e getSource();
f addPoint(new Point(e getX() e getY()));
f repaint(); //讓Frame強(qiáng)制經(jīng)行重畫(huà)
}
lishixinzhi/Article/program/Java/hx/201311/26376
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);
}
}
這個(gè)簡(jiǎn)單
可以調(diào)用方法drawline(int x1, int y1, int x2, int y2)
其中(x1, y1), (x2, y2)分別為直線(xiàn)起點(diǎn)和終點(diǎn)的坐標(biāo)
特意給你寫(xiě)了個(gè)小例子,希望能幫到你
***************************************
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);
}
}
****************************
將程序復(fù)制到記事本中并改名為DrawLine.java(注意大小寫(xiě))
然后在命令行中用 CD+文件夾 使命令范圍到DrawLine.java的文件夾中
然后執(zhí)行命令javac DrawLine.java
再執(zhí)行命令java DrawLine
你會(huì)看到結(jié)果
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()?{
//鼠標(biāo)下壓事件
@Override
public?void?mousePressed(MouseEvent?e)?{
x?=?e.getX();
y?=?e.getY();
}
//鼠標(biāo)彈起事件
@Override
public?void?mouseReleased(MouseEvent?e)?{
System.out.println("鼠標(biāo)彈起事件");
}
});
addMouseMotionListener(new?MouseMotionAdapter()?{
//鼠標(biāo)下壓移動(dòng)事件
@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();
}
}