Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    Markgm is offline Member
    Join Date
    Nov 2010
    Location
    VT, USA
    Posts
    31
    Rep Power
    0

    Default Images, Device dependent? Can they be device independent?

    Hi,
    I'm just getting into my first real Java app, which is for playing card games. I have images for cards in a PNG file, and I can load that into Image objects in java which it says is in a device-dependent form. It says this is not a serializable object (Image) that can be recreated on different machines.
    I would like to be able to run an Applet with images that go with it, and if those images can come from an image file (above-mentioned PNG) that downloads with the applet, or if they can be put in data form then that can be serialized to/from a database.
    1) If I put the image files in with the JAR for the applet, will they be accessible to loading them from disk, or do they have to be un-jared for access?
    2) Is there a device-independent way to put image data (so i can put that into my db)?
    3) How do you generally handle graphics images that are needed in distributed programs?

    Thanks much,
    Mark

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    I haven't done much programming in Java Applets, but in Java Applications you can hold the image inside the jar file and obtain it that way or have the data reside in a database if you want.

    Since you know the images you want to display I would suggest just packing the images inside the jar file.

  3. #3
    Markgm is offline Member
    Join Date
    Nov 2010
    Location
    VT, USA
    Posts
    31
    Rep Power
    0

    Default

    Quote Originally Posted by StormyWaters View Post
    I haven't done much programming in Java Applets, but in Java Applications you can hold the image inside the jar file and obtain it that way or have the data reside in a database if you want.

    Since you know the images you want to display I would suggest just packing the images inside the jar file.
    I have noticed that Applets versus Apps means the working directory is accessed differently. With applet, its Image myimage = getImage( getDocumentBase(), "myfile.png"), the applet has a root dir where it is running, but without it would be different. ImageIcon imageicon = new ImageIcon("myfile.png");

    I'll take your response as a vote in the direction of putting needed image files in the JAR. I'm just a newbie at this.

  4. #4
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default

    This tutorial is a little more targeted at what you need
    How to Make Applets (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
    (and the rest of that page).

    Also, if you're using the AWT Applet, I strongly suggest that you refactor your code to use the Swing JApplet.

    db

  6. #6
    Markgm is offline Member
    Join Date
    Nov 2010
    Location
    VT, USA
    Posts
    31
    Rep Power
    0

    Default

    Thanks for the tips - I'm on my way. I am still trying to find where Image or ImageIcon objects get put up on screen. I don't see the code for it in the examples provided (above). These are not Component objects, and an ImageObserver is called for on pane.drawImage calls. I noticed in a card game example I have that the Card object extends JLabel, and maybe that was a way to make it a component? Well, I'm still not sure how it paints itself.

    Here is some code from Card.java... (calculation source) I don't understand how it paints itself.
    - Mark

    private static ImageIcon back_img;
    private static int width;
    private static int height;
    private static String back_image_file;
    private ImageIcon face_img;
    private ImageIcon curr_img;
    private ImageIcon mark_img;
    private int value;
    private int suit;
    private boolean face_up;

    static
    {
    back_image_file = "deck0.png";
    back_img = new ImageIcon(back_image_file);
    width = back_img.getIconWidth();
    height = back_img.getIconHeight();
    }

    public Card(int i, int j, String s)
    {
    this(i, j, new ImageIcon(s));
    }

    public Card(int i, int j, ImageIcon imageicon)
    {
    value = i;
    suit = j;
    face_up = false;
    curr_img = back_img;
    face_img = imageicon;
    int k = face_img.getIconWidth();
    int l = face_img.getIconHeight();
    setSize(k, l);
    setMinimumSize(new Dimension(k, l));
    setPreferredSize(new Dimension(k, l));
    setMaximumSize(new Dimension(k, l));
    BufferedImage bufferedimage = new BufferedImage(k, l, 1);
    BufferedImage bufferedimage1 = new BufferedImage(k, l, 1);
    Graphics2D graphics2d = bufferedimage.createGraphics();
    graphics2d.drawImage(face_img.getImage(), 0, 0, this);
    byte abyte0[] = new byte[256];
    for(int i1 = 0; i1 < abyte0.length - 1; i1++)
    abyte0[i1] = (byte)i1;

    abyte0[abyte0.length - 1] = -128;
    ByteLookupTable bytelookuptable = new ByteLookupTable(0, abyte0);
    (new LookupOp(bytelookuptable, null)).filter(bufferedimage, bufferedimage1);
    face_img = new ImageIcon(bufferedimage);
    mark_img = new ImageIcon(bufferedimage1);
    }

    public Card(int i, String s)
    {
    this(i / 4 + 2, i % 4, s);
    }

    public void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    setIcon(curr_img);
    }

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    who wrote this code? I haven't looked at all of it, but this looks very odd to me:

    Java Code:
    public void paintComponent(Graphics g)
    {
      super.paintComponent(g);
      setIcon(curr_img);
    }
    calling setIcon in a paintComponent method doesn't smell quite right to me. Am I missing something?

  8. #8
    Markgm is offline Member
    Join Date
    Nov 2010
    Location
    VT, USA
    Posts
    31
    Rep Power
    0

    Default

    Maybe it's a bad code example. Free Java Calculation Card Game, Free Java Source Code

    Where I am at is to have Image objects, and to want to put them into the GUI (but they are not components). What is the mechanism for putting them in the GUI (and keeping them painted)?

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Markgm View Post
    I think you are right as that is code that doesn't belong in a paintComopnent method as he appears to be calling program logic within this method.

    Where I am at is to have Image objects, and to want to put them into the GUI (but they are not components). What is the mechanism for putting them in the GUI (and keeping them painted)?
    One way is to place the images into ImageIcons and the ImageIcons into JLabels, so that they become components. Another way is to draw the images in a JComponent's paintComponent method.

  10. #10
    Markgm is offline Member
    Join Date
    Nov 2010
    Location
    VT, USA
    Posts
    31
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    ... One way is to place the images into ImageIcons and the ImageIcons into JLabels, so that they become components. Another way is to draw the images in a JComponent's paintComponent method.
    Thanks for that clarity. Is the Applet's paintComponent where one uses a draw-all approach? How would you do the paint for a JLabel with a card image in it (or would that maintain itself)?

    I will have to be drawing cards stacked, a few pixels offset for each but in the same general physical space. Can a stack be rendered in a JLabel?

    Cheers, Mark

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Markgm View Post
    Thanks for that clarity. Is the Applet's paintComponent where one uses a draw-all approach? How would you do the paint for a JLabel with a card image in it (or would that maintain itself)?
    First off, you'll want to use Swing/JApplets (as recommended above). Next treat Applet as a top-level container, and don't draw directly in it. Instead if you're going to draw using paintComponent (which JApplet doesn't even have), do it in a JComponent such as a JPanel.


    I will have to be drawing cards stacked, a few pixels offset for each but in the same general physical space. Can a stack be rendered in a JLabel?
    Sure. If you're going to be using the JLabel route, then the key here will be the layouts used for placing your components. You'd probably best use a JLayeredPane and possibly its default null layout if you're going to do any animation.

  12. #12
    Markgm is offline Member
    Join Date
    Nov 2010
    Location
    VT, USA
    Posts
    31
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    ... Sure. If you're going to be using the JLabel route, then the key here will be the layouts used for placing your components. You'd probably best use a JLayeredPane and possibly its default null layout if you're going to do any animation.
    Thank you. I think that for stacks of cards laid out, that I will have to be able to do two things. One will be to know when the mouse is where the card-image is, and two will be for the stacks of cards to paint themselves, bottom card up, using exact pixels-from-top-left of a drawing area. Each stack will have up to 4 cards in it, but up to 25 stacks running at the same time in the total playing area.

    When a move gets made, one card can be animated across the top by some kind of XOR ability to float over what is behind it - I hope. But card moves have to be set up by the player here by placing markers in a couple of spots first, then cards flip and/or move. Long story short, I was wondering if I was going to absolutely position everything within a single drawing space for all 25 stacks, or make 25 "stack" objects that are each self-managing components within. Would you rather oversee image (card) objects stacked in each of 25 locations, or use a single component for your own absolute positioning of all 52 card images within that space?

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Markgm View Post
    I think that for stacks of cards laid out, that I will have to be able to do two things. One will be to know when the mouse is where the card-image is, and two will be for the stacks of cards to paint themselves, bottom card up, using exact pixels-from-top-left of a drawing area. Each stack will have up to 4 cards in it, but up to 25 stacks running at the same time in the total playing area.

    When a move gets made, one card can be animated across the top by some kind of XOR ability to float over what is behind it - I hope.
    I'm no pro at this, so please take what I say with a grain of salt, but if using JLabels with ImageIcons, then each JLabel would worry about drawing its own image, and you won't have to. If you're using a JLayeredPane, then you'd simply move the moving card to the DRAG_LAYER, and it will move above all other components below it.

    But card moves have to be set up by the player here by placing markers in a couple of spots first, then cards flip and/or move.
    The JLabels would have a MouseListener and MouseMotionListener (probably combined as a MouseAdapter) added and the behavior will vary (flip vs. move) depending upon where on the JLabel one clicks. To flip, you'd simply swap the JLabel's ImageIcon.

    Long story short, I was wondering if I was going to absolutely position everything within a single drawing space for all 25 stacks, or make 25 "stack" objects that are each self-managing components within. Would you rather oversee image (card) objects stacked in each of 25 locations, or use a single component for your own absolute positioning of all 52 card images within that space?
    Regardless, you are likely going to need a non-GUI model to oversee the logic of what you describe above. The model then directs the GUI to change components and images.

  14. #14
    Markgm is offline Member
    Join Date
    Nov 2010
    Location
    VT, USA
    Posts
    31
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    ...Regardless, you are likely going to need a non-GUI model to oversee the logic of what you describe above. The model then directs the GUI to change components and images.
    I think I'm going to have to paint the deck each time there is a system-paint call, and update only what needs to be to handle individual plays. What this leaves me wondering is what types to use for my own absolute positioning of all the cards within one space? Such as from top-left as 0,0, and I'll paint all the cards in on that. I may try the layered pane for when moving one card, too.

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  16. #16
    Markgm is offline Member
    Join Date
    Nov 2010
    Location
    VT, USA
    Posts
    31
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    You could extend from the Shape interface or one of its subclasses.
    Just looking at shape and jlabel... Is there an object where I can just specify, say 300 x 300 pixels, and I handle the painting inside of that? Such as, where I can put/map images or ImageIcons into that space and ask them to paint themselves there.

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Markgm View Post
    Just looking at shape and jlabel... Is there an object where I can just specify, say 300 x 300 pixels, and I handle the painting inside of that? Such as, where I can put/map images or ImageIcons into that space and ask them to paint themselves there.
    JLabels handle ImageIcons well, and you can even extend them if necessary if you want to also add custom drawing code. Or you can create your own class for this. There are many avenues of potential attack here.

  18. #18
    Markgm is offline Member
    Join Date
    Nov 2010
    Location
    VT, USA
    Posts
    31
    Rep Power
    0

    Default

    I've got one card per JLabel, but I am going to need to paint stacks of cards. When there are four in a stack, then they need to be offset a few pixels each to show how many are in the stack. So they overlap. But what I haven't done yet, is either use absolute coordinates to position a paint action in a space (to paint my own stack of cards), or to have one card drawn overlapping another.

  19. #19
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Here is a quickly written example of moving JLabels in a JLayeredPane. If you left-click on the cards and drag them, you can move them. Right-click on them and they show/hide their faces. Since the jar file is too big to upload here, I've uploaded it to mediafire, and it can be found (probably temporarily) here:

    CardTableEg.jar

    It is called CardTableEg.jar

    The images were obtained from here: Playing Card Images, and are available under the Creative Commons ShareALike License.


    The Java files, minus the image files and package declarations are:

    Suit.java
    Java Code:
    public enum Suit {
       CLUBS, DIAMONDS, HEARTS, SPADES
    }
    Rank.java
    Java Code:
    public enum Rank {
       ACE("a"), TWO("2"), THREE("3"), FOUR("4"), FIVE("5"), 
       SIX("6"), SEVEN("7"), EIGHT("8"), NINE("9"), 
       TEN("10"), JACK("j"), QUEEN("q"), KING("k");
       
       private String text;
       private Rank(String text) {
          this.text = text;
       }
       
       public String getText() {
          return text;
       }
    }
    Card.java
    Java Code:
    public class Card {
       private Suit suit;
       private Rank rank;
       public Card(Suit suit, Rank rank) {
          this.suit = suit;
          this.rank = rank;
       }
       public Suit getSuit() {
          return suit;
       }
       public Rank getRank() {
          return rank;
       }
       
       @Override
       public String toString() {
          return "[" + suit + ", " + rank + "]";
          
       }
    }
    GuiCard
    Java Code:
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class GuiCard extends JLabel {
       private Card card;
       private static ImageIcon back;
       private ImageIcon face;
       
       public GuiCard(Card card, ImageIcon face) {
          super(face);
          this.card = card;
          this.face = face;
       }
       public Card getCard() {
          return card;
       }
       
       public void showFace(boolean show) {
          if (show) {
             setIcon(face);
          } else {
             setIcon(back);
          }
       }
       
       public static void setBack(ImageIcon bk) {
          back = bk;
       }
       public static ImageIcon getBack() {
          return back;
       }
       public ImageIcon getFace() {
          return face;
       }
       public boolean isFaceShowing() {
          return getIcon() == face;
       }
       
       
    }
    CardTable.java
    Java Code:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Image;
    import java.awt.Point;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    /**
     * Card Images from http://www.eludication.org/playingcards.html
     * They are available for use under the Creative Commons Share-alike
     * License linked to on the site.
     * @author Peter
     * Date: 11/22/2010
     *
     */
    @SuppressWarnings("serial")
    public class CardTable extends JLayeredPane {
       public static final String IMAGE_PATH = "75/";
       private static final Dimension MAIN_SIZE = new Dimension(600, 450);
       private static final Color BCKGRD = Color.green.darker().darker();
       private static final int GAP = 23;
       private List<GuiCard> deck = new ArrayList<GuiCard>();
       
       public CardTable() throws IOException {
          Image backImg = ImageIO.read(getClass().getResourceAsStream(IMAGE_PATH + "back-blue-75-1.png"));
          Image img = null;
          GuiCard.setBack(new ImageIcon(backImg));
          for (Suit suit : Suit.values()) {
             for (Rank rank : Rank.values()) {
                String fileStr = IMAGE_PATH + suit.toString().toLowerCase() + "-" +
                   rank.getText() + "-75.png";
                img = ImageIO.read(getClass().getResourceAsStream(fileStr));
                ImageIcon icon = new ImageIcon(img);
                GuiCard card = new GuiCard(new Card(suit, rank), icon);
                card.setSize(card.getPreferredSize());
                deck.add(card);
             }
          }
          
          Collections.shuffle(deck);
          
          setPreferredSize(MAIN_SIZE);
          setOpaque(true);
          setBackground(BCKGRD);
          MyMouseAdapter mouseAdapter = new MyMouseAdapter();
          
          int y = (MAIN_SIZE.height / 4) - deck.get(0).getPreferredSize().height / 2;
          for (int i = 0; i < 10; i++) {
             GuiCard card = deck.remove(0);
             card.setLocation(100 + i * GAP, y);
             card.showFace(false);
             card.addMouseListener(mouseAdapter);
             card.addMouseMotionListener(mouseAdapter);
             add(card, JLayeredPane.DEFAULT_LAYER);
             moveToFront(card);
          }
          
          y = MAIN_SIZE.height - y - deck.get(0).getPreferredSize().height;
          for (int i = 0; i < 10; i++) {
             GuiCard card = deck.remove(0);
             card.setLocation(100 + i * GAP, y);
             card.showFace(true);
             card.addMouseListener(mouseAdapter);
             card.addMouseMotionListener(mouseAdapter);
             add(card, JLayeredPane.DEFAULT_LAYER);
             moveToFront(card);
             
          }
          
          
       }
       
       private class MyMouseAdapter extends MouseAdapter {
          private GuiCard card = null;
          private Point deltaLocation = null;
          
          @Override
          public void mousePressed(MouseEvent e) {
             card = (GuiCard)e.getSource();
             if (e.getButton() == MouseEvent.BUTTON3) {
                card.showFace(!card.isFaceShowing());
                card = null;
             } else {
                remove(card);
                add(card, JLayeredPane.DRAG_LAYER);
                Point p1 = card.getLocation();
                Point p2 = e.getLocationOnScreen();
                deltaLocation = new Point(p1.x - p2.x, p1.y - p2.y);
             }
          }
          
          @Override
          public void mouseReleased(MouseEvent e) {
             if (card == null) {
                return;
             }
             remove(card);
             add(card, JLayeredPane.DEFAULT_LAYER);
             moveToFront(card);
             card = null;
          }
          
          @Override
          public void mouseDragged(MouseEvent e) {
             if (card == null) {
                return;
             }
             Point p1 = deltaLocation;
             Point p2 = e.getLocationOnScreen();
             int x = p1.x + p2.x;
             int y = p1.y + p2.y;
             card.setLocation(x, y);
    
             repaint();
    
          }
       }
    
       private static void createAndShowUI() {
          JFrame frame = new JFrame("CardTable");
          try {
             frame.getContentPane().add(new CardTable());
          } catch (IOException e) {
             e.printStackTrace();
          }
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }

  20. #20
    Markgm is offline Member
    Join Date
    Nov 2010
    Location
    VT, USA
    Posts
    31
    Rep Power
    0

    Default

    That is a very awesome demo of this! Thank you! I was just going over the JLayeredPane example (Layered Pane Demo) from before, and I saw the main class extends JPanel, and the layered pane was on top of that, but that it had the absolute positioning. This is a beautiful demo you've got (which I will use!), and a nice link to a card source. Maybe they'll like my card games when I'm done with them. Thanks a million - Mark
    Last edited by Markgm; 11-23-2010 at 01:14 AM.

Page 1 of 2 12 LastLast

Similar Threads

  1. JMF: Cannot connect to device
    By jonsamwell in forum New To Java
    Replies: 2
    Last Post: 06-10-2011, 07:20 PM
  2. Device Independant
    By shajuantony in forum AWT / Swing
    Replies: 2
    Last Post: 05-25-2009, 10:15 PM
  3. USB Device Copy
    By Mir in forum New To Java
    Replies: 3
    Last Post: 08-25-2008, 11:44 AM
  4. Porting to a Nokia Device
    By David55 in forum CLDC and MIDP
    Replies: 1
    Last Post: 06-30-2008, 01:44 PM
  5. Redirect to null device
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-05-2008, 09:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •