关于Applet中的闪烁问题(100分)

  • 主题发起人 主题发起人 guoDY
  • 开始时间 开始时间
G

guoDY

Unregistered / Unconfirmed
GUEST, unregistred user!
在Applet中如何使用图像缓冲来消除闪烁。 请给一个
例子
 
来不及帮你裁剪了,都给你吧。反正就是双缓冲了。
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;
import java.lang.Math;
import netscape.javascript.JSObject;
import netscape.javascript.JSException;
//主地图
public class Applet1 extends Applet implements Runnable,MouseListener, MouseMotionListener{
double dmapx,dmapy; //用于XYMapToPlot函数计算的变量
Thread appletThread;

Point FirstPt,CurPt,OldPt;

int nDrawMode; /*当前绘图模式
0 Null
1 Point
2 Line
3 Rect
4 Ellipse
5 Poly
*/

String szHost; //服务器地址
String szPage; //主页名称 1.html
String szMap; //地图名称 1.map

int nOperation; /*当前操作类型
1 ZoomIn
2 ZoomOut
3 Pan
4 AddHotPoint
5 Select Hot Point
*/

Point offset; //偏移
Image img,bufImg;
Graphics buf;
///////////////////////////////以下为为网上效果图新作的变量
int nImgCount; //有多少个图片

Vector imgList; //图片链表,用来存放热点链表
Vector hotList;
int nSelHotID; //当前在mousePress中选定的热区的标号,无则为负
int nSelImg; //当前选中的图片ID
int nBufWidth,nBufHeight;
JSObject win,doc,form,listField,text1;
///////////////////////////////以下为为网上效果图新作的变量

public void updateSelect1(){
HotPoint hpt;
for (int i = 0;i < hotList.size();i++){
hpt = (HotPoint)hotList.elementAt(i);
try{
win.eval("addHotPoint(/"" + (new Integer(hpt.ID).toString()) + "/")");
}
catch(Exception ex){

}
}
}

public void deleteHotPoint(){
if (nSelHotID < 0 ) return;
HotPoint hpt;
for (int i = 0;i < hotList.size();i++){
hpt = (HotPoint)hotList.elementAt(i);
if (hpt.ID == nSelHotID) {
hotList.removeElementAt(i);
break;
}
}

try{
win.eval("deleteHotPoint(/"" + new Integer(nSelHotID).toString() + "/")");
}
catch(Exception ex){

}
update();
}

public int setComment(int htID,String comment){
HotPoint hpt;
for (int i = 0;i < hotList.size();i++){
hpt = (HotPoint)hotList.elementAt(i);
if (hpt.ID == htID){
hpt.comment = comment;
return 1;
}
}
return 0;
}
public String getComment(int ID){
HotPoint hpt;
for (int i = 0;i < hotList.size();i++){
hpt = (HotPoint)hotList.elementAt(i);
if (hpt.ID == ID){
return hpt.comment;
}
}
return "";
}

public void setDrawMode(int nMode){
nDrawMode = nMode;
}
public void setOperation(int nOp){
nOperation = nOp;
}

public void setHostURL(String szURL){
szHost = szURL;
}
public void sendImage(){
URLConnection urlc;
try{
URL url = new URL("http://localhost:81/1.htm");
urlc = url.openConnection();


}
catch(Exception ex){

}
}

public void init() {
addMouseMotionListener(this);
addMouseListener(this);

FirstPt = new Point(0,0);
OldPt = new Point(0,0);
CurPt = new Point(0,0);
this.setBackground(Color.white);
nDrawMode = 3; //初始化为矩形
nOperation = 3; //初始化为放大

nSelHotID = -1;

offset = new Point(0,0);

nBufWidth = Integer.parseInt(getParameter("bufwidth"));
nBufHeight = Integer.parseInt(getParameter("bufheight"));

bufImg = createImage(nBufWidth,nBufHeight);
buf = bufImg.getGraphics();

//获得页面
try{
win = JSObject.getWindow(this);
}
catch (JSException ex){
}
//得到图片数量

nImgCount = Integer.parseInt(getParameter("imgCount"));

//缺省选中第一张图片,同时该ID对应ImgList的下标
if (nImgCount > 0) nSelImg = 0;
else
nSelImg = -1;

imgList = new Vector();

for (int i = 0;i<nImgCount;i++){
hotList = new Vector();
imgList.addElement(hotList);
}
getServerImage(0,getParameter("iniimg"));
}

public void start(){
appletThread = new Thread(this);
appletThread.start();
}

public void stop(){
appletThread.stop();
appletThread = null;
}

public void run(){
}

public void addcomment(int id,int x,int y,int radius,String comment){
HotPoint hpt = new HotPoint(x,y);
hpt.ID = id;
hpt.radius = radius;
hpt.comment = comment;
hotList.addElement(hpt);
}

public void getServerImage(int ID,String ImageName){
offset.x = 0;
offset.y = 0;
this.showStatus("Loading image " + ImageName + " ...");
img = this.getImage(this.getDocumentBase(),ImageName);

this.getGraphics().drawImage(img,0,0,this);
nSelImg = ID;
hotList = this.getSelHotVector(nSelImg);
repaint();
}

public Vector getSelHotVector(int ID){
if (imgList.size() > 0) return (Vector)imgList.elementAt(ID);
else
return null;
}

public String getModifiersText(InputEvent e) {
String returnString = "";
if (e.isControlDown()) {
returnString += "CTRL + ";
}
if (e.isAltDown()) {
returnString += "ALT + ";
}
if (e.isShiftDown()) {
returnString += "SHIFT + ";
}
return returnString;
}

public void displayMouseInfo(String eventName, MouseEvent e) {
showStatus(getModifiersText(e)
+ eventName
+ "("
+ String.valueOf(e.getX())
+ ", "
+ String.valueOf(e.getY())
+ ")");
}
// Mouse methods required by the MouseMotionListener interface
public void mouseMoved(MouseEvent e) {

}

public void mouseDragged(MouseEvent e) {
switch (nOperation){
case 1 : { //ZoomIn
Graphics g = this.getGraphics();
g.setXORMode(this.getBackground());
//g.drawRect(FirstPt.x,FirstPt.y,Math.abs(FirstPt.x - CurPt.x),Math.abs(FirstPt.y - CurPt.y));
DrawShape(g);

CurPt.x = e.getX();
CurPt.y = e.getY();
this.setForeground(Color.red);
g.setXORMode(this.getBackground());
//g.setPaintMode();
//g.drawRect(FirstPt.x,FirstPt.y,Math.abs(FirstPt.x - CurPt.x),Math.abs(FirstPt.y - CurPt.y));
DrawShape(g);
break;
}
case 2 : { //ZoomOut
Graphics g = this.getGraphics();
g.setXORMode(this.getBackground());
//g.drawRect(FirstPt.x,FirstPt.y,Math.abs(FirstPt.x - CurPt.x),Math.abs(FirstPt.y - CurPt.y));
DrawShape(g);

CurPt.x = e.getX();
CurPt.y = e.getY();
this.setForeground(Color.red);
g.setXORMode(this.getBackground());
//g.setPaintMode();
//g.drawRect(FirstPt.x,FirstPt.y,Math.abs(FirstPt.x - CurPt.x),Math.abs(FirstPt.y - CurPt.y));
DrawShape(g);
break;
}
case 3 : { //Pan
if (offset.x >= 0 &amp;&amp;
offset.y >= 0 &amp;&amp;
offset.x <= img.getWidth(this) &amp;&amp;
offset.y <= img.getHeight(this)){
CurPt.x = e.getX();
CurPt.y = e.getY();

offset.x = offset.x + CurPt.x - OldPt.x;
offset.y = offset.y + CurPt.y - OldPt.y;
OldPt.x = e.getX();
OldPt.y = e.getY();

update();
}
break;
}
case 4 : {
/* HotPoint hpt = (HotPoint)(hotList.elementAt(nSelHotID));

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

Graphics g = this.getGraphics();
g.setXORMode(this.getBackground());
g.drawOval(hpt.centerX - hpt.radius/2 - CurPt.x + FirstPt.x,hpt.centerY - hpt.radius/2 - CurPt.y + FirstPt.y,hpt.radius,hpt.radius);

this.setForeground(Color.red);
g.setXORMode(this.getBackground());
//g.setPaintMode();
//g.drawRect(FirstPt.x,FirstPt.y,Math.abs(FirstPt.x - CurPt.x),Math.abs(FirstPt.y - CurPt.y));
g.drawOval(hpt.centerX - hpt.radius/2 - CurPt.x + FirstPt.x,hpt.centerY - hpt.radius/2 - CurPt.y + FirstPt.y,hpt.radius,hpt.radius);
*/
break;

}
}
}

// Mouse methods required by the MouseListener interface
public void mouseClicked(MouseEvent e) {
if (e.isMetaDown()) {
displayMouseInfo("Right Mouse Clicked", e);
}
else
{
displayMouseInfo("Left Mouse Clicked", e);
}
}

public void mousePressed(MouseEvent e) {
switch (nOperation){
case 1: {
Graphics g = this.getGraphics();
if (e.isMetaDown()) {
displayMouseInfo("Right Mouse Pressed", e);
}
else
{
displayMouseInfo("Left Mouse Pressed", e);
}
FirstPt.x = e.getX();
FirstPt.y = e.getY();
CurPt.x = e.getX();
CurPt.y = e.getY();
this.setForeground(Color.red);
//g.setPaintMode();
g.setXORMode(this.getBackground());
// g.drawRect(FirstPt.x,FirstPt.y,Math.abs(FirstPt.x - CurPt.x),Math.abs(FirstPt.y - CurPt.y));
DrawShape(g);
break;
}
case 2: {
Graphics g = this.getGraphics();
if (e.isMetaDown()) {
displayMouseInfo("Right Mouse Pressed", e);
}
else
{
displayMouseInfo("Left Mouse Pressed", e);
}
FirstPt.x = e.getX();
FirstPt.y = e.getY();
CurPt.x = e.getX();
CurPt.y = e.getY();

this.setForeground(Color.red);
//g.setPaintMode();
g.setXORMode(this.getBackground());
// g.drawRect(FirstPt.x,FirstPt.y,Math.abs(FirstPt.x - CurPt.x),Math.abs(FirstPt.y - CurPt.y));
DrawShape(g);
break;}
case 3: {
FirstPt.x = e.getX();
FirstPt.y = e.getY();
CurPt.x = e.getX();
CurPt.y = e.getY();
OldPt.x = e.getX();
OldPt.y = e.getY();
break;}
case 4 : {
HotPoint hpt;
FirstPt.x = e.getX();
FirstPt.y = e.getY();
CurPt.x = e.getX();
CurPt.y = e.getY();

break;
}//case 4

}
}
public HotPoint getSelHotPoint(int x,int y){
HotPoint hpt;
for (int i = 0;i<hotList.size();i++){
hpt = (HotPoint)(hotList.elementAt(i));
if (Math.sqrt(Math.pow(x - offset.x - hpt.centerX,2) + Math.pow(y - offset.y - hpt.centerY,2)) < hpt.radius/2){
return hpt;
}
}
return null;
}
public String getData(){
String databuf = "";
HotPoint hpt;
for (int i = 0;i < hotList.size();i++){
hpt = (HotPoint)hotList.elementAt(i);
if (hpt.comment != null)
databuf = databuf + hpt.ID + "|" + Integer.toString(hpt.centerX) + "|" + Integer.toString(hpt.centerY) + "|" + Integer.toString(hpt.radius) + "|" + hpt.comment + "/n";
}
return databuf;
}

public void selectHotPointById(int ID){
nSelHotID = ID;
repaint();
}

public void mouseReleased(MouseEvent e){
InputStream istream;
String line;
String returnValue = "";
BufferedReader in = null;
int tx,ty,tx1,ty1;
switch (nOperation){
case 1 : {
Graphics g = this.getGraphics();

g.setXORMode(this.getBackground());
DrawShape(g);

//计算新的范围
if (FirstPt.x < e.getX()) {
tx = FirstPt.x;
tx1 = e.getX();
}
else
{
tx = e.getX();
tx1 = FirstPt.x;
}

if (FirstPt.y < e.getY()) {
ty = FirstPt.y;
ty1= e.getY();
}else
{
ty = e.getY();
ty1 = FirstPt.y;
}


break;
}
case 2 : {
break;
}
case 3 : {
//计算新的范围
break;
}
case 4 : {// add hot point
HotPoint hpt = getSelHotPoint(e.getX(),e.getY());
if (hpt != null){
nSelHotID = hpt.ID;
try{
win.eval("selecthotpoint(/"" + new Integer(nSelHotID).toString() + "/")");
}
catch(Exception ex){

}
repaint();
break;
}
hpt = new HotPoint(e.getX() - offset.x,e.getY() - offset.y);
if (hotList.size() > 0) {
HotPoint hpttmp = (HotPoint)(hotList.elementAt(hotList.size() - 1));
hpt.setID(hpttmp.ID + 1);
}
else
hpt.setID(0);

hotList.addElement(hpt);

try{
win.eval("addHotPoint(/"" + (new Integer(hpt.ID).toString()) + "/")");
}
catch(Exception ex){

}
update();
break;
}
case 5 : {
HotPoint hpt = getSelHotPoint(e.getX(),e.getY());
if (hpt != null){
nSelHotID = hpt.ID;
try{
win.eval("selecthotpoint(/"" + new Integer(nSelHotID).toString() + "/")");
}
catch(Exception ex){

}
repaint();
break;
}

break;
}
}
}

public void update(){
paint(this.getGraphics());
}

public void mouseEntered(MouseEvent e) {

}

public void mouseExited(MouseEvent e) {

}

public void paint(Graphics g){
buf.clearRect(0,0,nBufWidth,nBufHeight);
buf.setPaintMode();
buf.drawImage(img,offset.x,offset.y,this);
HotPoint hpt;
buf.setXORMode(this.getBackground());
Font ft = new Font("宋体",0,20);
for (int i=0;i<hotList.size();i++){
hpt = (HotPoint)(hotList.elementAt(i));
if (nSelHotID == hpt.ID) buf.setColor(Color.red);
else
buf.setColor(Color.black);
buf.drawOval(hpt.centerX - hpt.radius/2 + offset.x,hpt.centerY - hpt.radius/2 + offset.y,hpt.radius,hpt.radius);
buf.setColor(Color.blue);
buf.setFont(ft);
buf.drawString(new Integer(hpt.ID).toString(),hpt.centerX - (int)(hpt.radius * Math.sqrt(2)/4) + offset.x ,(hpt.centerY - (int)(hpt.radius * Math.sqrt(2)/4) + offset.y));
}
g.setPaintMode();
g.drawImage(bufImg,0,0,this);
}

public void DrawShape(Graphics g){
int tx,ty,tx1,ty1;
switch (nDrawMode){
case 1:
case 2:
case 3:
tx = FirstPt.x;
ty = FirstPt.y;
tx1 = CurPt.x;
ty1 = CurPt.y;

if (tx > tx1){
tx = tx1 + tx;
tx1 = tx - tx1;
tx = tx - tx1;
}
if (ty > ty1){
ty = ty1 + ty;
ty1 = ty - ty1;
ty = ty - ty1;
}
g.drawRect(tx,ty,tx1 - tx, ty1 - ty);
break;
case 4:
g.drawArc(FirstPt.x,FirstPt.y,Math.abs(FirstPt.x - CurPt.x),Math.abs(FirstPt.y - CurPt.y),FirstPt.x,FirstPt.y);
break;
}
}

public void clearHotPt(){
hotList.removeAllElements();
paint(this.getGraphics());
}
}
////////////////////////////////////////////////////////////////////////////////////////////
class GeoRect extends Object{
double left,top,right,bottom;
}
////////////////////////////////////////////////////////////////////////////////////////////
class Rect extends Object{
int left,top,right,bottom;
}
////////////////////////////////////////////////////////////////////////////////////////////
class HotPoint extends Object{
int ID;
int centerX,centerY;
int radius;
String comment;

public HotPoint(int x,int y){
centerX = x;
centerY = y;

radius = 80;
}

public void setRadius(int r){
radius = r;
}

public void setID(int id){
ID = id;
}
}
 
用双缓冲,JAVA的书里一般都有。
 
问题关键在于不要在paint()中重新绘图,
而是要在内建的一个Image上绘图,
paint(),update()方法只需要把这个Image输出即可...
 
多人接受答案了。
 
后退
顶部