用java编写一个简单的画图程序。不用复杂

如题所述

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(); //重绘组件
}
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-04-21
package s;//包名
  import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  public class Test extends JFrame {
  int x1,y1,x2,y2;public Test(){
  setVisible(true);
  setSize(300,300) ;
  addWindowListener(new WindowAdapter() {
  public void windowClosing(WindowEvent e){
  System.exit(0) ; }});
  addMouseListener(
  new MouseAdapter() {
  public void mousePressed(MouseEvent e){
  x1=e.getX();
  y1=e.getY();} });
  addMouseMotionListener(new MouseMotionAdapter() {
  public void mouseDragged(MouseEvent e){
  x2=e.getX() ;
  y2=e.getY() ;
  repaint(); }});
  }
  public void paint(Graphics g)
  {
  g.drawLine(x1,y1,x2,y2);
  x1=x2;
  y1=y2;
  }
  public static void main(String args[])
  {
  new Test();
  }
  }
  本回答被网友采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网