Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Some problem in Java Applet

  Asked By: Fahimah    Date: Dec 01    Category: Java    Views: 998
  

I have some problem in my applet .
In this applet each 1 second 10 times download one picture from one site ( thread 1 ) .
From other part , Some MouseEvent and KeyboadEvent i set for that with dragMouseEvent & keypress for that to possible navigate on the picture .

Now problems is :


1. in some computer after few min browser will hang ((IE or FireFox)), ( maybe because the memory will hint too much due too downloading picture but if yes how can i solve that i need to download this much picture )
2. when the picture stick downloading for few seconds also is not possible to navigating on the picture too and whole time navigation is not smoothly movement .
3. keyoard event is not working at all I think i wrote that right but i can not understand why not work ?
4. all time the java splash finished very soon before first image initiated . therefore user will see some black background .
5. how can i do make some custom splash for myself and make it longer ?instead of the java default splash .
6. how is possible i make one spacial thread which is running in background of program for downloading picture and help to navigation in the picture smoothly .

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;
import javax.imageio.ImageIO;

public class CamViewer2 extends Applet implements MouseListener,
MouseMotionListener, KeyListener {
private Image mImg;
private int mIsStarted = 1;
private int mInterval = 60;
private String mFile = "http://wildlife.ae/img2/image.jpg";
private URL imageURL;
private static int w, h;
private int width, height;
private int x, y, mx, my;
private boolean isMouseDraggingBox = false;

public void start() {
mIsStarted = 1;
}

public void stop() {
mIsStarted = 0;
}

public void init() {
printThreadName("init is ");
addMouseMotionListener(this);
addKeyListener( this );
addMouseListener(this);
setBackground(Color.BLACK);
width = getSize().width;
height = getSize().height;
x = 0;
y = 0;
}

public void paint(Graphics g) {
URLConnection urlConnection;
try {
imageURL = new URL(mFile);
urlConnection = imageURL.openConnection();
urlConnection.setDefaultUseCaches(false);
urlConnection.setRequestProperty("Cache-Control","no-store,max-age=0,no-cache");
urlConnection.setRequestProperty("Expires", "0");
urlConnection.setRequestProperty("Pragma", "no-cache");
mImg = ImageIO.read(imageURL);
} catch (Exception e) {
System.out.println("Problem getting initial image: " + e.toString());
update(g);
return;
}
g.drawImage(mImg, x, y, 640, 480, this);
if (mIsStarted == 1) {
repaint();
try {
Thread.sleep(mInterval);
} catch (InterruptedException e) {
System.err.println("ERROR: " + e);
}
}
}

public void update(Graphics g) {
paint(g);
}
private void printThreadName(String prefix) {
String name = Thread.currentThread().getName();
System.out.println(prefix + name);
}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}

public void mousePressed(MouseEvent e) {
mx = e.getX();
my = e.getY();
isMouseDraggingBox = true;
e.consume();
}

public void mouseReleased(MouseEvent e) {
isMouseDraggingBox = false;
e.consume();
}

public void mouseDragged(MouseEvent e) {
if (isMouseDraggingBox) {
// get the latest mouse position
int new_mx = e.getX();
int new_my = e.getY();
if (x < 1 && y < 1 && x > -320 && y > -200) {
x += -(new_mx - mx);
y += -(new_my - my);
if (x > -1) {
x = -1;
}
if (x < -319) {
x = -319;
}
if (y > -1) {
y = -1;
}
if (y < -199) {
y = -199;
}
}
// update our data
mx = new_mx;
my = new_my;
repaint();
e.consume();
}
}

public void mouseMoved(MouseEvent e) {}

public void keyPressed(KeyEvent evt) {
int key = evt.getKeyCode();

if (key == KeyEvent.VK_LEFT) {
if (w < 0) {
w = w + 10;
System.out.println("Left :"+w);
this.repaint();
}
}
else if (key == KeyEvent.VK_RIGHT) {
if (w > -320) {
w = w - 10;
System.out.println("Right :"+w);
repaint();
}
}
else if (key == KeyEvent.VK_UP) {
if (h < 0) {
h = h + 10;
System.out.println("Up : "+ h );
repaint();
}
}
else if (key == KeyEvent.VK_DOWN) {
if (h > -200) {
h = h - 10;
System.out.println("Down : "+ h);
repaint();
}
}

}
public void keyReleased(KeyEvent e) {}

public void keyTyped(KeyEvent e) {}
}

</code end>

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Randall Franklin     Answered On: Dec 01

1. Calling repaint method from paint method is wrong. Try another mechanism (such thread  , a thead that sleeps every n milliseconds an wakes up and calls the repaint.).

2. Try get information in another method , save them on global variables , and use in paint update method seem to be good place.

3.if the image  is constant (not change) , use jar file and insert image as a resource to the jar.

 
Didn't find what you were looking for? Find more on Some problem in Java Applet Or get search suggestion and latest updates.




Tagged: