1 Reply Latest reply: Oct 23, 2006 11:38 PM by 807598 RSS

    Loading JPEG files into a java program

    807598
      I want to load JPeg files from my C drive rather than an URL through the net on someelses computer

      import java.awt.*;
      import java.awt.geom.AffineTransform;
      import javax.swing.ImageIcon;
      import java.awt.image.BufferedImage;
      import java.net.*;

      /**
      * A class for displaying images of cards on a graphics object.
      * The primary method of this class is "draw" -- it could have
      * been incorporated into the Card class, but is kept separate
      * so that it can be distributed to students as a stand-alone class.
      *
      * @author Richard Lobb
      * @version 18 February 2006.
      */
      public class CardImage
      {
      private static final int HIGHLIGHT_WIDTH = 4;
      private static ImageIcon backIcon;
      private static int cardHeight;
      private static int cardWidth;

      /**
      * Get the width in pixels of a card image.
      * @return the width in pixels of a card image, as defined by
      * the width of the image of the back of a card.
      */
      public static int getWidth()
      {
      return cardWidth;
      }

      /**
      * Get the height in pixels of the card image.
      * @return the height in pixels of a card image, as defined by
      * the height of the image of the back of a card.
      */
      public static int getHeight()
      {
      return cardHeight;
      }

      /**
      * Draw a given card on a graphics device.
      * @param card the card to be drawn. Might be null, in which case a dotted outline of a
      * card is drawn instead (see "drawNullCard").
      * @param g the Graphics object onto which the image should be drawn.
      * @param x the x-coordinate of the top left hand corner of the drawn card
      * @param y the y-coordinate of the top left hand corner of the drawn card
      * @param faceUp true if the front of the card is to be drawn, false if the back is to be drawn
      * @param selected true iff the card is to be drawn with a yellow outline to indicate that it is
      * currently selected.
      */
      public static void draw(Card card, Graphics g, int x, int y, boolean faceUp, boolean selected)
      {
      if (card == null) {
      drawNullCard(g, x, y);
      }
      else {
      if (selected) {
      Graphics2D g2 = (Graphics2D) g;
      Stroke stroke = new BasicStroke(HIGHLIGHT_WIDTH);
      g2.setStroke(stroke);
      g2.setColor(Color.YELLOW);
      g2.drawRect(x-HIGHLIGHT_WIDTH, y-HIGHLIGHT_WIDTH, getWidth()+2*HIGHLIGHT_WIDTH, getHeight()+2*HIGHLIGHT_WIDTH);
      }
      ImageIcon icon;
      if (!faceUp) {
      icon = backIcon;
      }
      else {

      // Construct file name for card image.
      // This is roughly but not exactly the same as the card's toString with the extension .png.

      String suitChars[] = {"s", "h", "d", "c"}; // WARNING -- must agree with public suit constants
      String pipStrings[] = {"ERROR","1","2","3","4","5","6","7","8","9","10","j","q","k"};
      String fileName = suitChars[card.getSuit()] + pipStrings[card.getNumber()] + ".png";
      URL imageURL = card.getClass().getResource(fileName);
      if (imageURL == null) {
      System.err.println("Error in constructing CardImage -- image icon not found");
      System.exit(1);
      }
      icon = new ImageIcon(imageURL);
      }
      icon.paintIcon(null, g, x, y);
      }
      }

      /**
      * Draw a "null card" on a graphics device, i.e., a dotted rectangle representing a place where
      * a card might be placed.
      * @param g the Graphics object onto which the rectangle should be drawn.
      * @param x the x-coordinate of the top left hand corner of the rectangle
      * @param y the y-coordinate of the top left hand corner of the rectangle
      */
      public static void drawNullCard(Graphics g, int x, int y)
      {
      Graphics2D g2 = (Graphics2D) g;
      Stroke stroke = new BasicStroke(1, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_BEVEL, 0,
      new float[] {1,3}, 0);
      g2.setStroke(stroke);
      g2.setColor(Color.BLACK);
      g2.drawRect(x, y, getWidth(), getHeight());
      }

      // Static initialisation block

      static {
      Card dummy = new Card(1,1);
      // Why can't I use the SystemClassLoader or the ContextClassLoader to load the images as resources?
      // Damned if I know!
      URL imageURL = dummy.getClass().getResource("b1fv.png");
      if (imageURL == null) {
      System.err.println("Error in initialising CardImage class -- image icon not found");
      System.exit(0);
      }
      else {
      backIcon = new ImageIcon(imageURL);
      cardWidth = backIcon.getIconWidth();
      cardHeight = backIcon.getIconHeight();
      }
      }
      }