java 窗口中画一个点_自制一个Java程序,可以在窗口中随便画,但是出现一个问题...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

package DrawRandom;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.Line2D;

public class DrawRandom extends JFrame {

public static void main(String args[]) {

new DrawRandom();

}

public DrawRandom() {

super.setTitle("myFrame");

this.setVisible(true);

this.setBackground(Color.GREEN);

this.setLayout(null);

Canvas cas = new myCanvas();

this.add(cas);

this.setDefaultCloseOperation(3);

this.setSize(800, 768);

this.setLocationRelativeTo(null);

cas.addMouseListener(new MouseAdapter() {

public void mouseExited(MouseEvent e) {

System.exit(0);

}

});

}

@SuppressWarnings("serial")

class myCanvas extends Canvas implements MouseMotionListener {

int x, y;

public myCanvas() {

this.addMouseMotionListener(this);

this.setBackground(Color.GREEN);

this.setSize(800, 768);

}

public void paint(Graphics g) {

// super.paint(g);

Graphics2D g2d = (Graphics2D) g;

g2d.setColor(Color.red);

BasicStroke bs = new BasicStroke(4);

g2d.setStroke(bs);

g2d.draw(new Line2D.Double(x, y, x, y));

}

@Override

public void mouseDragged(MouseEvent e) {

// TODO Auto-generated method stub

x = e.getX();

y = e.getY();

repaint();

System.out.println("drag");

}

public void update(Graphics g) {

paint(g);

}

@Override

public void mouseMoved(MouseEvent e) {

// TODO Auto-generated method stub

// x=e.getX();y=e.getY();

// System.out.println(""+x+y);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值