Newsgroup: it.comp.java
Titolo: [lungo + sourcecode] Primi esperimenti con FullScreenApi

Finalmente sono riuscito a farlo andare. So che è un po' una vaccata
(dove si è mai visto un programma Java composto da una classe con
tutti metodi statici?) però mi ha dato le basi per comprendere i
problemi che possono sorgere durante la programmazione con le
FullScreen APIs. Ma soprattutto funziona (almeno sul mio pc).
Avete voglia di testarlo sulle vostre piattaforme?
A proposito: per uscire premere la barra spaziatrice
Commenti (belli e brutti) sempre graditi ;)
---file MainClass.java---
import java.awt.Frame;
import java.awt.GraphicsEnvironment;
import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.GraphicsConfiguration;
import java.awt.DisplayMode;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferStrategy;
import java.awt.image.IndexColorModel;
import java.awt.image.WritableRaster;
import java.awt.image.MultiPixelPackedSampleModel;
import java.awt.image.BufferedImage;
public class MainClass {
   private static int[] kbuf=new int[100];
   private static int kbhead=0;
   private static int kbtail=0;
   private static boolean kbover=false;
   private static Object kblock=new Object();
   private static Object kbwait=new Object();
   private static Object dmwait=new Object();
   private static Frame theFrame=null;
   private static Graphics gr=null;
   public static void main(String[] args) {
      try {
         GraphicsEnvironment gEnv=
            GraphicsEnvironment.getLocalGraphicsEnvironment();
         GraphicsDevice gDev=
            gEnv.getDefaultScreenDevice();
         GraphicsConfiguration gConf=gDev.getDefaultConfiguration();
         DisplayMode dispMode=new DisplayMode(640,480,32,0);
         DisplayMode oldMode=null;
         Rectangle bounds=null;
         BufferStrategy bmgr=null;
         Toolkit tk=Toolkit.getDefaultToolkit();
         theFrame=new Frame(gConf);
         theFrame.setUndecorated(true);
         theFrame.setIgnoreRepaint(true);
         theFrame.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
               synchronized (kblock) {
                  if (kbover) {
                     System.out.println("Keyboard buffer overflow!");
                     Toolkit.getDefaultToolkit().beep();
                     return;
                  }
                  kbuf[kbhead]=e.getKeyCode();
                  kbhead++;
                  if (kbhead==100) kbhead=0;
                  if (kbhead==kbtail) kbover=true;
                  synchronized (kbwait) {kbwait.notify();}
               }
            }
         });
         theFrame.addComponentListener(new ComponentAdapter() {
            public void componentResized(ComponentEvent e) {
               if (e.getComponent()!=theFrame) return;
               synchronized (dmwait) {dmwait.notify();}
            }
         });
         theFrame.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
               if ((e.getButton()!=MouseEvent.BUTTON1) || 
                  (gr==null))
                  return;
               for(int i=0;i<21;i++) {
                  gr.setColor(new Color(i*10,0,0));
                  gr.drawOval(e.getX()-i,e.getY()-i,(i*2),(i*2));
               }
            }
         });
         //Customizzazione del cursore
         //Creiamo la mappa dei colori
         int[][] cmap_i={
            {0x00, 0xFF, 0x7F, 0xFF, 0x00, 0x00, 0x00, 0x00,
             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
            {0x00, 0xFF, 0x7F, 0xFF, 0x00, 0x00, 0x00, 0x00,
             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
            {0x00, 0xFF, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00,
             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
            {0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
             0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
         };
         //Forziamo la conversione in byte
         byte[][] cmap=new byte[cmap_i.length][cmap_i[0].length];
         for(int i1=0;i1<cmap.length;i1++)
            for(int i2=0;i2<cmap[i1].length;i2++)
               cmap[i1][i2]=(byte)cmap_i[i1][i2];
         IndexColorModel icm=
            new IndexColorModel(4,16, cmap[0],
                                      cmap[1],
                                      cmap[2],
                                      cmap[3]);
         //Bitmap del nuovo cursore
         int[][] bitmap={
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2},
{1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,2},
{1,0,1,2,2,2,2,2,2,2,2,2,2,2,1,0,1,2,2,2,2,2,2,2,2,2,2,2,1,0,1,2},
{1,0,1,2,2,2,2,2,2,2,2,2,2,2,1,0,1,2,2,2,2,2,2,2,2,2,2,2,1,0,1,2},
{1,0,1,2,2,2,2,2,2,2,2,2,2,2,1,0,1,2,2,2,2,2,2,2,2,2,2,2,1,0,1,2},
{1,0,1,2,2,2,2,2,2,2,2,2,2,2,1,0,1,2,2,2,2,2,2,2,2,2,2,2,1,0,1,2},
{1,0,1,2,2,2,2,2,2,2,2,2,2,2,1,0,1,2,2,2,2,2,2,2,2,2,2,2,1,0,1,2},
{1,0,1,2,2,2,2,2,2,2,2,2,2,2,1,0,1,2,2,2,2,2,2,2,2,2,2,2,1,0,1,2},
{1,0,1,2,2,2,2,2,2,2,2,2,2,2,1,0,1,2,2,2,2,2,2,2,2,2,2,2,1,0,1,2},
{1,0,1,2,2,2,2,2,2,2,2,2,2,2,1,0,1,2,2,2,2,2,2,2,2,2,2,2,1,0,1,2},
{1,0,1,2,2,2,2,2,2,2,2,1,1,1,1,0,1,1,1,1,2,2,2,2,2,2,2,2,1,0,1,2},
{1,0,1,2,2,2,2,2,2,2,2,1,0,0,0,0,0,0,0,1,2,2,2,2,2,2,2,2,1,0,1,2},
{1,0,1,2,2,2,2,2,2,2,2,1,0,1,1,1,1,1,0,1,2,2,2,2,2,2,2,2,1,0,1,2},
{1,0,1,1,1,1,1,1,1,1,1,1,0,1,2,2,2,1,0,1,1,1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,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2},
{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}
         };
         WritableRaster wrs=
icm.createCompatibleWritableRaster(bitmap.length,bitmap[0].length);
         for(int iy=0;iy<bitmap.length;iy++)
            for(int ix=0;ix<bitmap[0].length;ix++)
              wrs.setPixel(ix,iy,new int[]{bitmap[iy][ix]});
         BufferedImage cursorImage=
            new BufferedImage(icm,wrs,false,null);
         Cursor holeCursor=
            tk.createCustomCursor(cursorImage,new Point(15,15),
                                  "Hole");
         try {
            oldMode=gDev.getDisplayMode();
            gDev.setFullScreenWindow(theFrame);
            gDev.setDisplayMode(dispMode);
            MainClass.waitDisplayMode();
            bounds=theFrame.getBounds();
            theFrame.createBufferStrategy(1);
            bmgr=theFrame.getBufferStrategy();
            theFrame.setVisible(true);
            theFrame.toFront();
            bmgr.show();
            theFrame.setCursor(holeCursor);
            gr=bmgr.getDrawGraphics();
            gr.setColor(Color.black);
            gr.fillRect(0, 0, bounds.width, bounds.height);
            for(int i=0;i<240;i++) {
               gr.setColor(new Color(0,0,i));
               gr.drawRect(0,i*2,bounds.width,1);
            }
            for(int wk=0;wk!=32;wk=MainClass.waitKey());
            gr.dispose();
         }
         catch (Exception ex) {
            ex.printStackTrace();
         }
         finally {
            if (gr!=null) gr.dispose();
            gDev.setDisplayMode(oldMode);
            gDev.setFullScreenWindow(null);
         }
         System.exit(0);
      }
      catch (Exception ex) {
         ex.printStackTrace();
      }
   }
   public static int getKey() {
      int res=-1;
      synchronized (kblock) {
         if ((kbhead==kbtail) && !kbover) return -1;
         else {
            kbover=false;
            res=kbuf[kbtail];
            kbtail++;
            if (kbtail==100) kbtail=0;
         }
      }
      return res;
   }
   public static void waitDisplayMode() {
      waitDisplayMode(0L);
   }
   public static void waitDisplayMode(long timeout) {
      synchronized (dmwait) {
         try {dmwait.wait(timeout);}
         catch(InterruptedException ex) {}
      }
   }
   public static int waitKey() {
      return waitKey(0L);
   }
   public static int waitKey(long timeout) {
      synchronized (kbwait) {
         try {kbwait.wait(timeout);}
         catch(InterruptedException ex) {}
      }
      return getKey();
   }
}
---fine file---
--
"tanto va la gatta al largo che affoga"
(per rispondere personalmente leva IlDitoDalCulo)

 
WinXP SP1 + JRE 1.4.2 funzica.
Marco
P.S. certo che questo sarebbe un bel modo di distribuire un trojan :)

P.S. certo che questo sarebbe un bel modo di distribuire un trojan :)
Beh, è open source (ti sfido a dire il contrario!). Chiunque può
esaminarlo e dire se c'è o no un trojan... Però c'è sempre l'incognita
del principiante che copia-incolla-compila-esegue alla cieca...



Funziona e anche bene su Win Xp sp2
complimenti
ciao Luigi
-- 
Le grandi scoperte
nascono dal sogno ad occhi aperti
di qualcuno....


Windows XP Home Editon SP1
J2SDK 1.4.1
Funge
Ciao,
CC

[Ech3L0n] NSA ha scritto:
[...]
Su Windows 2000 SP4 va, ma su un altro pc con Windows 2000 SP4 no ora vedo
se capisco che problema può essere...
Ottimo comunque il lavoro...
Complimenti
Ivan
-- 
questo articolo e` stato inviato via web dal servizio gratuito 
http://www.newsland.it/news
 segnala gli abusi ad 
ab...@newsland.it

se capisco che problema può essere...
Potrebbero essere i driver della scheda video. So che in modalità
fullscreen la JVM di Sun non è compatibile con le schede ATI. Prova ad
installare i driver più recenti, e ad aggiornare anche le librerie
DirectX (giunte alla versione 9.0b)


 se capisco che problema può essere...
Su Windows 2000 con aggiornamenti live funziona (java version "1.4.1_02")
Complimenti
Francesco

 (per rispondere personalmente leva IlDitoDalCulo)


 detto...
Ora che ci penso l'altro non ha le DirectX aggiornate... Per lavoro non
stiamo dietro ad aggiornarle
