当前位置: 技术问答>java相关
怎样在applet中,用键盘中的“delete”删除我选中的对象(用鼠标点击选中),比如可活动的图片、箭头。
来源: 互联网 发布时间:2017-04-20
本文导语: 如上: | 这个是联系你上一个问题的例子,用到的还是canvas,右键鼠标可以在区域里添加,选中一个canvas,然后按delete可以删除选中的对象,做到这里,我相信要实现流程图的绘制应该不难了...
如上:
|
这个是联系你上一个问题的例子,用到的还是canvas,右键鼠标可以在区域里添加,选中一个canvas,然后按delete可以删除选中的对象,做到这里,我相信要实现流程图的绘制应该不难了
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.applet.*;
public class applet6 extends Applet {
canvas curCom=null;
Image img1=null;
int width=0,height=0;
MediaTracker mt=new MediaTracker(this);
public void init() {
img1=this.getImage(this.getCodeBase(),"1.gif");//装载图片
this.setLayout(null);
mt.addImage(img1,0);
try {
mt.waitForAll();
} catch(Exception ex) {System.err.println(ex.toString());}
width=img1.getWidth(this);
height=img1.getHeight(this);
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
addcanvas(e);
}
});
}
public void addcanvas(MouseEvent e) {
if (e.getModifiers() == MouseEvent.BUTTON3_MASK) {
canvas can = new canvas();
this.add(can);
can.setBounds(e.getX(), e.getY(), width, height);
this.validate();
curCom = can;
}
}
public void removecanvas(KeyEvent e) {
this.remove(this.curCom);
this.validate();
this.repaint();
}
class canvas extends Canvas {
public canvas() {
super();
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
((applet6)((canvas)e.getSource()).getParent()).curCom=(canvas)e.getSource();
}
});
this.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
((applet6)((canvas)e.getSource()).getParent()).removecanvas(e);
}
});
}
public void paint(Graphics g) {
g.drawImage(img1,0,0,this);
}
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.applet.*;
public class applet6 extends Applet {
canvas curCom=null;
Image img1=null;
int width=0,height=0;
MediaTracker mt=new MediaTracker(this);
public void init() {
img1=this.getImage(this.getCodeBase(),"1.gif");//装载图片
this.setLayout(null);
mt.addImage(img1,0);
try {
mt.waitForAll();
} catch(Exception ex) {System.err.println(ex.toString());}
width=img1.getWidth(this);
height=img1.getHeight(this);
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
addcanvas(e);
}
});
}
public void addcanvas(MouseEvent e) {
if (e.getModifiers() == MouseEvent.BUTTON3_MASK) {
canvas can = new canvas();
this.add(can);
can.setBounds(e.getX(), e.getY(), width, height);
this.validate();
curCom = can;
}
}
public void removecanvas(KeyEvent e) {
this.remove(this.curCom);
this.validate();
this.repaint();
}
class canvas extends Canvas {
public canvas() {
super();
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
((applet6)((canvas)e.getSource()).getParent()).curCom=(canvas)e.getSource();
}
});
this.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
((applet6)((canvas)e.getSource()).getParent()).removecanvas(e);
}
});
}
public void paint(Graphics g) {
g.drawImage(img1,0,0,this);
}
}
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。