import java.awt.*
import java.awt.event.*
import java.awt.geom.*
import javax.swing.*
//不规则图形的绘制
public class IrregularShapeDemo extends JFrame {
GeneralPath gPath= new GeneralPath()//GeneralPath对象实例
Point aPoint
//构造函数
public IrregularShapeDemo() {
super("不规则图形的绘制")//调用父类构造函数
enableEvents(AWTEvent.MOUSE_EVENT_MASK|AWTEvent.MOUSE_MOTION_EVENT_MASK)//允许事件
setSize(300, 200)//设置窗口尺寸
setVisible(true)//设置窗口可视
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)//关闭窗口时退出程序
}
public void paint(Graphics g) { //重载窗口组件的paint()方法
Graphics2D g2D = (Graphics2D)g//获取图形环境
g2D.draw(gPath)//绘制路径
}
public static void main(String[] args) {
new IrregularShapeDemo()
}
protected void processMouseEvent(MouseEvent e) { //鼠标事件处理
if(e.getID() == MouseEvent.MOUSE_PRESSED) {
aPoint = e.getPoint()//得到当前鼠标点
gPath = new GeneralPath()//重新实例化GeneralPath对象
gPath.moveTo(aPoint.x,aPoint.y)//设置路径点
}
}
protected void processMouseMotionEvent(MouseEvent e) { //鼠标运动事件处理
if(e.getID() == MouseEvent.MOUSE_DRAGGED) {
aPoint = e.getPoint()//得到当前鼠标点
gPath.lineTo(aPoint.x, aPoint.y)//设置路径
gPath.moveTo(aPoint.x, aPoint.y)
repaint()//重绘组件
}
}
}
// 粗细,颜色都符合你的,你就不要不讲诚信了,我也跟你没有仇,你视而不见就没有必要了import java.awt.BasicStroke
import java.awt.BorderLayout
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
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 )
{
float lineWidth = 8.0f
((Graphics2D)g).setStroke(new BasicStroke(lineWidth))
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)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)