Thread: [sdljava-users] Locked devices and strange solor in images
Status: Beta
Brought to you by:
ivan_ganza
From: Nuno S. <dev...@im...> - 2006-06-06 01:33:27
|
Hi, About the locked devices even with event it hangs the devices. Sometimes i dont even need to run the program once. It happens in the first time. About the images... i managed to create the file to the filesystem first and then show it. It keeps the strange colors. How do i make disappear the mouse arrow? Many thx Nuno This is my code, just for you to see the initialization and event handling as well as the image loading (marked in red): import java.awt.*; import java.awt.image.*; import java.io.*; import javax.imageio.*; import com.sun.image.codec.jpeg.JPEGImageDecoder; import com.sun.image.codec.jpeg.JPEGCodec; import java.io.ByteArrayInputStream; import java.net.*; import java.sql.*; import java.util.Timer; import java.util.TimerTask; public class Reactiva { int muppie=1; Timer timer; boolean working; ResultSet result; Database bd; Pacman p; SDLSurface image; static SDLSurface fb; public Reactiva() throws SDLException { SDLMain.init(SDLMain.SDL_INIT_VIDEO); fb = SDLVideo.setVideoMode(800, 600, 32, (long)SDLVideo.SDL_HWSURFACE|SDLVideo.SDL_DOUBLEBUF); // fb = SDLVideo.setVideoMode(800,600,24,(long)SDLVideo.SDL_HWSURFACE| // SDLVideo.SDL_FULLSCREEN| // SDLVideo.SDL_DOUBLEBUF); } public void start() { bd = new Database(); p = new Pacman(); result = bd.runST("select NR, TITULO, CONTENT, DURACAO from ANUNCIO"); timer = new Timer(); timer.schedule(new RemindTask(),0,5*1000); } public void stop() { SDLMain.quit(); } public void changeImage(ResultSet result) throws SDLException { int id=0,duration=0; String title = ""; try { System.out.println("Loading image..."); try { duration = result.getInt("DURACAO"); id = result.getInt("NR"); title = result.getString("TITULO"); System.out.println("Id: " + id + " Title: " + title + " Announce duration: " + duration + " seconds"); Blob blob = result.getBlob("CONTENT"); long blobLength = blob.length(); int pos = 1; int len = (int) blob.length(); byte[] bytes = blob.getBytes(pos, len); ByteArrayInputStream bis = new ByteArrayInputStream (bytes,0,bytes.length); JPEGImageDecoder dec = JPEGCodec.createJPEGDecoder(bis); try { BufferedImage bim = dec.decodeAsBufferedImage(); ImageIO.write(bim, "jpg", new File("actualimage.jpg")); } catch (Exception e) { } System.out.println("Image loaded"); image = SDLImage.load("actualimage.jpg"); image.setAlpha(0,255); fb.fillRect(0x000000); image.blitSurface(null,fb,null); fb.updateRect(); } catch (SDLException e) { System.out.println("Error: Couln't load image"); System.out.println(e.getMessage()); } } catch (Exception e) { e.printStackTrace(); } } class RemindTask extends TimerTask { int duration=0,number=0; ResultSet auxres; public void run() { try { Sensor aux = null; Answer ans = null; ans = p.getAnswer(); aux = p.getSensor(); if (ans!=null) { System.out.println("Request received with value " + ans.getAddid()); auxres = bd.runST("SELECT * FROM ANUNCIO WHERE NR=" + ans.getAddid()); if (auxres.next()) { try { duration = auxres.getInt("DURACAO"); changeImage(auxres); } catch (SDLException e) { System.out.println("Error: Couln't load image"); System.out.println(e.getMessage()); } } else System.out.println("There is no add with the id " + ans.getAddid()); } else { if (aux!=null) { System.out.println("Type of sensor: " + aux.getType()); System.out.println("Value: " + aux.getValue()); System.out.println("Sensor found... sending request with value " + aux.getValue()); File out = new File("/var/blackboard/PEDIDO-" + muppie + "-" + aux.getValue()); try { if (!out.exists()) { FileWriter msg = new FileWriter(out); msg.close(); } } catch (IOException e) { } } else if (result.next()) { try { duration = result.getInt("DURACAO"); changeImage(result); } catch (SDLException e) { System.out.println("Error: Couln't load image"); System.out.println(e.getMessage()); } } else result.beforeFirst(); } } catch (SQLException e) { System.out.println("Error: SQL error"); System.out.println(e); } timer.cancel(); timer = new Timer(); timer.schedule(new RemindTask(),duration*1000,1); } } public static void main(String[] args) { Reactiva r = null; try { r = new Reactiva(); if (r!=null) { r.start(); } while(true) { SDLEvent e = SDLEvent.waitEvent(); if (e instanceof SDLKeyboardEvent) { SDLKeyboardEvent keyboardEvent = (SDLKeyboardEvent) e; if (keyboardEvent.getSym() == SDLKey.SDLK_ESCAPE) { r.stop(); } } } } catch (SDLException e) { e.printStackTrace(); } finally { if (r != null) r.stop(); } } |