From 59f884846609cc6d8c445837c00c025c720b1878 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Wed, 24 Feb 2016 19:08:42 +0100 Subject: [PATCH] Gestion du mode headless --- .../spaceinvader/MegaSpaceInvader.java | 31 +++++++++++++- .../iut_lens/spaceinvader/client/Client.java | 2 +- .../spaceinvader/launcher/LauncherDialog.java | 2 +- .../spaceinvader/sprites_manager/Sprite.java | 42 ++++++++++++++++--- .../sprites_manager/SpriteStore.java | 20 ++++----- 5 files changed, 78 insertions(+), 19 deletions(-) diff --git a/src/fr/univ_artois/iut_lens/spaceinvader/MegaSpaceInvader.java b/src/fr/univ_artois/iut_lens/spaceinvader/MegaSpaceInvader.java index 1b9176a..1d67fec 100644 --- a/src/fr/univ_artois/iut_lens/spaceinvader/MegaSpaceInvader.java +++ b/src/fr/univ_artois/iut_lens/spaceinvader/MegaSpaceInvader.java @@ -1,5 +1,6 @@ package fr.univ_artois.iut_lens.spaceinvader; +import java.awt.GraphicsEnvironment; import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; @@ -65,10 +66,13 @@ public static void main(String[] args) { shutdownHook(); Thread.currentThread().setName("Main"); + boolean headless = GraphicsEnvironment.isHeadless(); + try { // donne à l'interface graphique le thème associé au système d'exploitation - UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + if (!headless) + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); } @@ -84,6 +88,12 @@ public static void main(String[] args) { // si la ligne de commande ne donne rien de concluant, affichage du launcher. if (launchConfig == null) { + if (headless) { + showCommandLineHelp(); + System.exit(1); + } + + LauncherDialog diag = new LauncherDialog(); ConfigurationSaver saver = new ConfigurationSaver(); @@ -116,7 +126,8 @@ public static void main(String[] args) { - if (launchConfig.clientEnabled) { + if (launchConfig.clientEnabled && !headless) { + String addr = launchConfig.clientConnectionAddress; int port; @@ -151,6 +162,22 @@ public static void main(String[] args) { + private static void showCommandLineHelp() { + System.out.println("paramètres : [-s [PORT_NUMBER [score]]] [-c NICKNAME [SERVER_ADDR:PORT]]"); + System.out.println(); + System.out.println("-s : activer le serveur local"); + System.out.println(" PORT_NUMBER : numéro de port attribué au serveur (par défaut 34567)"); + System.out.println(" 'score' pour activer le système de score (désactivé par défaut)"); + System.out.println("-c : activer le client local (ignoré si le mode headless est activé)"); + System.out.println(" NICKNAME le pseudo du joueur"); + System.out.println(" SERVER_ADDR:PORT : adresse de connexion au serveur. Ignoré si le serveur local est démarré, obligatoire sinon."); + } + + + + + + private static void shutdownHook() { diff --git a/src/fr/univ_artois/iut_lens/spaceinvader/client/Client.java b/src/fr/univ_artois/iut_lens/spaceinvader/client/Client.java index a1b021c..511d71f 100644 --- a/src/fr/univ_artois/iut_lens/spaceinvader/client/Client.java +++ b/src/fr/univ_artois/iut_lens/spaceinvader/client/Client.java @@ -114,7 +114,7 @@ public Client(InetSocketAddress serverAddress, String playerName) throws IOExcep // create a frame to contain our game container = new JFrame("Méga Space Invader"); - container.setIconImage(SpriteStore.get().getSprite("sprites/ComplexShot.png").image); + container.setIconImage(SpriteStore.get().getSprite("sprites/ComplexShot.png").getAWTImage()); // get hold the content of the frame and set up the resolution of the game JPanel panel = (JPanel) container.getContentPane(); diff --git a/src/fr/univ_artois/iut_lens/spaceinvader/launcher/LauncherDialog.java b/src/fr/univ_artois/iut_lens/spaceinvader/launcher/LauncherDialog.java index 3645653..f25590c 100644 --- a/src/fr/univ_artois/iut_lens/spaceinvader/launcher/LauncherDialog.java +++ b/src/fr/univ_artois/iut_lens/spaceinvader/launcher/LauncherDialog.java @@ -49,7 +49,7 @@ public class LauncherDialog extends JFrame { * Create the dialog. */ public LauncherDialog() { - setIconImage(SpriteStore.get().getSprite("sprites/ComplexShot.png").image); + setIconImage(SpriteStore.get().getSprite("sprites/ComplexShot.png").getAWTImage()); setResizable(false); setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE); diff --git a/src/fr/univ_artois/iut_lens/spaceinvader/sprites_manager/Sprite.java b/src/fr/univ_artois/iut_lens/spaceinvader/sprites_manager/Sprite.java index af80692..5b3be10 100644 --- a/src/fr/univ_artois/iut_lens/spaceinvader/sprites_manager/Sprite.java +++ b/src/fr/univ_artois/iut_lens/spaceinvader/sprites_manager/Sprite.java @@ -2,6 +2,8 @@ import java.awt.Graphics; import java.awt.Image; +import java.awt.Transparency; +import java.awt.image.BufferedImage; /** * A sprite to be displayed on the screen. Note that a sprite @@ -14,8 +16,10 @@ */ public class Sprite { private static int identifierIncrement = 0; + + private final BufferedImage bImage; /** The image to be drawn for this sprite */ - public final Image image; + private Image image = null; public final int id; /** @@ -23,8 +27,16 @@ public class Sprite { * * @param image The image that is this sprite */ - public Sprite(Image image) { - this.image = image; + public Sprite(BufferedImage bI) { + bImage = bI; + + if (SpriteStore.graphicsConfig != null) { + // create an accelerated image of the right size to store our sprite in + image = SpriteStore.graphicsConfig.createCompatibleImage(bI.getWidth(),bI.getHeight(),Transparency.TRANSLUCENT); + // draw our source image into the accelerated image + image.getGraphics().drawImage(bI,0,0,null); + } + id = identifierIncrement++; } @@ -34,7 +46,7 @@ public Sprite(Image image) { * @return The width in pixels of this sprite */ public int getWidth() { - return image.getWidth(null); + return bImage.getWidth(); } /** @@ -43,7 +55,27 @@ public int getWidth() { * @return The height in pixels of this sprite */ public int getHeight() { - return image.getHeight(null); + return bImage.getHeight(); + } + + + + /** + * + * @return l'image à afficher, ou null si on est en mode headless + */ + public Image getAWTImage() { + return image; + } + + + + /** + * + * @return le buffer contenant l'image + */ + public BufferedImage getBufferedImage() { + return bImage; } /** diff --git a/src/fr/univ_artois/iut_lens/spaceinvader/sprites_manager/SpriteStore.java b/src/fr/univ_artois/iut_lens/spaceinvader/sprites_manager/SpriteStore.java index 556f5af..c541747 100644 --- a/src/fr/univ_artois/iut_lens/spaceinvader/sprites_manager/SpriteStore.java +++ b/src/fr/univ_artois/iut_lens/spaceinvader/sprites_manager/SpriteStore.java @@ -2,8 +2,7 @@ import java.awt.GraphicsConfiguration; import java.awt.GraphicsEnvironment; -import java.awt.Image; -import java.awt.Transparency; +import java.awt.HeadlessException; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.URL; @@ -30,6 +29,14 @@ public class SpriteStore { /** The single instance of this class */ private static SpriteStore single = new SpriteStore(); + static GraphicsConfiguration graphicsConfig = null; + + static { + try { + graphicsConfig = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration(); + } catch (HeadlessException e) { } + } + /** * Get the single instance of this class * @@ -80,15 +87,8 @@ public synchronized Sprite getSprite(String ref) { fail("Failed to load: "+ref); } - // create an accelerated image of the right size to store our sprite in - GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration(); - Image image = gc.createCompatibleImage(sourceImage.getWidth(),sourceImage.getHeight(),Transparency.TRANSLUCENT); - - // draw our source image into the accelerated image - image.getGraphics().drawImage(sourceImage,0,0,null); - // create a sprite, add it the cache then return it - Sprite sprite = new Sprite(image); + Sprite sprite = new Sprite(sourceImage); sprites.put(ref,sprite); newSprites.put(ref, sprite);