跪求Java高手帮忙在我的程序上补写画笔程序,急需
展开全部
是实现java的鼠标事件吧,就是可以在窗体上画画写字什么的?
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class AppletLine extends Applet implements MouseListener{
int sx,sy,ex,ey;
public void init(){
this.setBackground(Color.black);
}
@Override
public void start() {
// TODO Auto-generated method stub
this.addMouseListener(this);
}
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
g.setColor(Color.yellow);//鼠标画线的颜色
g.drawLine(sx,sy, ex,ey);
}
@Override
public void update(Graphics g) { //重写update方法画线什么的 在写的时候就不会被清除
// TODO Auto-generated method stub
this.paint(g);
}
@Override
public void stop() {
// TODO Auto-generated method stub
super.stop();
}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
this.sx=e.getX();
this.sy=e.getY();
}
public void mouseReleased(MouseEvent e) {
this.ex=e.getX();
this.ey=e.getY();
this.repaint();
}
}
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class AppletLine extends Applet implements MouseListener{
int sx,sy,ex,ey;
public void init(){
this.setBackground(Color.black);
}
@Override
public void start() {
// TODO Auto-generated method stub
this.addMouseListener(this);
}
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
g.setColor(Color.yellow);//鼠标画线的颜色
g.drawLine(sx,sy, ex,ey);
}
@Override
public void update(Graphics g) { //重写update方法画线什么的 在写的时候就不会被清除
// TODO Auto-generated method stub
this.paint(g);
}
@Override
public void stop() {
// TODO Auto-generated method stub
super.stop();
}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
this.sx=e.getX();
this.sy=e.getY();
}
public void mouseReleased(MouseEvent e) {
this.ex=e.getX();
this.ey=e.getY();
this.repaint();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询