File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Applets and the fly likes Image trail when moved with mouse problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "Image trail when moved with mouse problem" Watch "Image trail when moved with mouse problem" New topic
Author

Image trail when moved with mouse problem

paul sims
Greenhorn

Joined: Aug 12, 2012
Posts: 3
Hi,I have a problem when moving my runnable image which leaves a trail behind it with my awt applet,not using swing or jframe for simplicity.

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package res3;


import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Label;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.TextField;
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Choice;


/**
*
* @author po
*/
public class resthree extends Applet implements Runnable, MouseListener, MouseMotionListener,ActionListener {

//problem with adding single image as background

Label counter1;
Label counter2;

int bgx = 0;
int bgy = 0;

//////////////////////////////////////////////
//ghosting without change

int b1xo = 300;////////brick1x old
int b1yo = 0;

int b1xn;//brick1new
int b1yn;
//////////////////////////////////////////////////////
int x2 = 450;
int y2 = 0;

Thread thread1;
boolean thread1bool = true;

Thread thread2;
boolean thread2bool = true;

Image bg1Image;
boolean bg1Imagebool = true;
boolean repaintbgbool = false;

Image[] res1Images = new Image[4];
int res1int = 0;
int res1count = 0;
boolean res1Imagesbool = true;

Image[] res2Images = new Image[4];
int res2int = 0;
int res2count = 0;

private Image Buffer;
private Graphics gBuffer;

MediaTracker tr;

Label mcoordsx;
Label mcoordsy;

Integer mx;
Integer my;

String mouseover = "No Object";
boolean brick1bool;
String brickbool = "brick 1 boolean = true";

TextField textfield1;
Button button1;
///////////////////////////////////////////////////////
public void init() {

setLayout(null);
setSize(960,661);
setForeground(Color.BLACK);

tr = new MediaTracker(this);

// Buffer=createImage(size().width,size().height);
Buffer = createImage (this.getSize ().width, this.getSize ().height);

gBuffer=Buffer.getGraphics();

bg1Image = getImage(getDocumentBase(), "tile1.gif");
//animation images
res1Images[0] = getImage(getDocumentBase(), "reso1.gif");
res1Images[1] = getImage(getDocumentBase(), "reso2.gif");
res1Images[2] = getImage(getDocumentBase(), "reso3.gif");
res1Images[3] = getImage(getDocumentBase(), "reso4.gif");


res2Images[0] = getImage(getDocumentBase(), "reso1.gif");
res2Images[1] = getImage(getDocumentBase(), "reso2.gif");
res2Images[2] = getImage(getDocumentBase(), "reso3.gif");
res2Images[3] = getImage(getDocumentBase(), "reso4.gif");

//background image
//add images to mediatracker
tr.addImage(bg1Image,0); //id = 0,media tracker

tr.addImage(res1Images[0], 1);// res1Images id =,1 mediatracker
tr.addImage(res1Images[1], 1);
tr.addImage(res1Images[2], 1);
tr.addImage(res1Images[3], 1);

tr.addImage(res2Images[0], 2);// res1Images id =,2 mediatracker
tr.addImage(res2Images[1], 2);
tr.addImage(res2Images[2], 2);
tr.addImage(res2Images[3], 2);
///////////////////////////////////////////////////////

counter1 = new Label("res1");
counter1.setBounds(800,30,75,20);
counter1.setForeground(Color.blue);
counter1.setBackground(Color.LIGHT_GRAY);
add(counter1);

counter2 = new Label("res2");
counter2.setBounds(800,70,75,20);
counter2.setForeground(Color.blue);
counter2.setBackground(Color.LIGHT_GRAY);
add(counter2);

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
mcoordsx = new Label("");
mcoordsx.setBounds(700,510,35,20);
mcoordsx.setForeground(Color.blue);
mcoordsx.setBackground(Color.black);
add(mcoordsx);

mcoordsy = new Label("");
mcoordsy.setBounds(735,510,35,20);
mcoordsy.setForeground(Color.blue);
mcoordsy.setBackground(Color.black);
add(mcoordsy);

addMouseListener( this );
addMouseMotionListener( this );

textfield1= new TextField("byebye");
textfield1.setBounds(400, 150, 50, 20);
add(textfield1);
textfield1.setVisible(false);


button1 = new Button("Move brick");
button1.setBounds(b1xo,b1yo+110,75,20);
add(button1);
button1.setVisible(false);
button1.addActionListener(this);

}

public void start(){
if(thread1bool == true){
(thread1 = new Thread(this)).start();
}
if(thread2bool == true){
(thread2 = new Thread(this)).start();
}

}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
public void stop(){
thread1 = null;
thread2 = null;
}

public void run(){
try{
tr.waitForID(0);
tr.waitForID(1);
tr.waitForID(2);

} catch (InterruptedException e) {
return;
}
//background goes here ///////////////////////
if(bg1Imagebool==true){
gBuffer.drawImage(bg1Image,0,0,this);
}

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// animations frames
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
if(thread1bool == true){
try {
while (thread1 == Thread.currentThread()) {
res1int = (res1int+1)%res1Images.length;
//gBuffer.drawImage(bg1Image[bg1int],0,0,this);//causes no flicker res 1,res
gBuffer.drawImage(res1Images[res1int],b1xo,b1yo,this);
repaint();
Thread.sleep(500);
res1count = res1count + 1;
counter1.setText("res1 "+res1count);
}
} catch (Exception e) {
}
}

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
if(thread2bool == true){
try {
while (thread2 == Thread.currentThread()) {
res2int = (res2int+1)%res2Images.length;
gBuffer.drawImage(res2Images[res2int],x2,y2,this);
repaint();
Thread.sleep(500);
res2count = res2count + 1;
counter2.setText("res2 "+res2count);
}
} catch (Exception e) {}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
} //animations end runnables
repaint();
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
public void mouseEntered( MouseEvent e ){}
public void mouseExited( MouseEvent e ){}
public void mouseClicked( MouseEvent e ){

Integer mx = e.getX();
Integer my = e.getY();

if(mx>b1xo && mx<b1xo+100 && my>b1yo && my<b1yo+100){ ///here coords chage
button1.setVisible(true);
}
else {
button1.setBounds(b1xo,b1yo+110,75,20);
button1.setVisible(false);
}

if(brick1bool==true){ //selected)

//get xy coords
//attatch to mouse
//repaint bg so no trail
//place image

b1xo = e.getX()-50;//on button click image attatch to mouse xy - 50 centre
b1yo = e.getY()-50;

brick1bool = false;//attach button1 to brick1
}
}
/////////////////////////////////////////////////////
public void mousePressed( MouseEvent e ){}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
public void mouseReleased( MouseEvent e ){}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
public void mouseMoved(MouseEvent e ){

// get the latest mouse position
Integer mx = e.getX();
Integer my = e.getY();

mcoordsx.setText(""+mx.intValue());
mcoordsy.setText(""+my.intValue());

if(mx>b1xo && mx<b1xo+100 && my>b1yo && my<b1yo+100){ //here coords change
mouseover = "brick1";
button1.setBounds(b1xo,b1yo+110,75,20);

}
else{
mouseover = "nothing";
}
/////////////////////////////////////////////
if(brick1bool==true){
// attach animation to mouse

b1xo = mx - 50;
b1yo = my - 50;
button1.setVisible(false);
}
////////////////////////////////////////////////

}//end mouse MOVEMENT
public void mouseDragged(MouseEvent e){}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
////////////////////////////////////////////////

public void actionPerformed(ActionEvent evt){


if(evt.getSource()==button1){
brick1bool = true;
}


//xxxxxxxxxxxxxxxxxxxxxxxxxxxxend action event
}



////////////////////////////////////////////////////////

public void paint(Graphics g){

if(tr.checkAll()==false){

g.drawString("Loading...", 460, 330);
}

else{
g.drawImage (Buffer, 0, 0, this);


g.drawString(mouseover, 800, 525);

}
}
///////////////////////////////////////////////////////////////////////////
public void update(Graphics g){
paint(g);
}
}

Thanks for any suggestions.

Kathleen Angeles
Ranch Hand

Joined: Aug 06, 2012
Posts: 122

Hi,

I am not sure if we have the same problem.

My applet is very tall. When the user uses the mouse' scroll wheel, the applet display is distorted or damaged. Hiding the browser and then showing it again (e.g. by minimizing the browser window), returns the correct applet display.

I approached it in 2 ways.

1) I used below.



2) I specifically tell user that they use the browser's vertical scroll bar (e.g. dragging it), instead of using the mouse scroll wheel. (The browser's vertical scroll bar getting dragged doesnt damage my applet view.)
paul sims
Greenhorn

Joined: Aug 12, 2012
Posts: 3
Hi,the problem is different.My question is referring to moving a image by selecting it with the mouse.Nothing to do with the scroll bar.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4543
    
    5

paul sims wrote:... with my awt applet,not using swing or jframe for simplicity.

Whatever gave you the idea that Swing, which is double buffered by default, would be less simple than that horribly complicated, antiquated AWT code?

Oh, and welcome to the Ranch! and UseCodeTags. Get familiar with Java coding conventions: class names should start with an uppercase letter.


luck, db
There are no new questions, but there may be new answers.
paul sims
Greenhorn

Joined: Aug 12, 2012
Posts: 3
Simple question,simple answer is required only,ask and ye shall recieve(god).
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

paul sims wrote:Simple question,simple answer is required only,ask and ye shall recieve(god).


I think you're making a couple of incorrect assumptions there. One of them is that a simple question necessarily has a simple answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Image trail when moved with mouse problem