Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (116)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  easier way to implement this code;  (Read 1551 times)
0 Members and 1 Guest are viewing this topic.
Offline civilwar

Senior Newbie





« Posted 2011-03-26 18:36:38 »


Basically I want to display around 50 to 300 images that I will also be allowed to click on to do an event.  If I do it this way I have to have 300 imagepanels(which just load a bg image into a panel).  Can anyone think of a easier way to do this?
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
  int counter = 0;
                         while (rs.next ())
                         {
                             String idVal = rs.getString (1);
                             if(idVal.contentEquals("1") & tokens[6].contentEquals(Level))
                             {
                                System.out.println(tokens[7]);
                                if(counter == 0)
                                {
                                cards = new ImagePanel(new ImageIcon("tokens[7]" + ".jpg").getImage());
                                cards.setBounds(296, 38, 100, 127);
                                frame.getContentPane().add(cards);
                                cards.addMouseListener(new MouseAdapter() {
                                       @Override
                                       public void mouseClicked(MouseEvent arg0) {
                                          //click event data
                                         
                                       }
                                    });
                                counter++;
                                }
                                else if(counter ==1)
                                {
                                   cards1 = new ImagePanel(new ImageIcon("tokens[7]" + ".jpg").getImage());
                                    cards1.setBounds(396, 38, 100, 127);
                                    frame.getContentPane().add(cards1);
                                    cards1.addMouseListener(new MouseAdapter() {
                                         @Override
                                         public void mouseClicked(MouseEvent arg0) {
                                            //click event data
                                           
                                         }
                                      });
                                    counter++;
                                }
                                else if(counter == 2)
                                {
                                   cards2 = new ImagePanel(new ImageIcon("tokens[7]" + ".jpg").getImage());
                                    cards2.setBounds(496, 20, 100, 127);
                                    frame.getContentPane().add(cards2);
                                    cards2.addMouseListener(new MouseAdapter() {
                                           @Override
                                           public void mouseClicked(MouseEvent arg0) {
                                              //click event data
                                             
                                           }
                                        });
                                    counter++;
                                }
                               
                               
                               
                                frame.setVisible(true);

                             }
Offline ra4king

JGO Kernel


Medals: 346
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2011-03-26 20:57:56 »

Maybe instead of using a class that extends JPanel, have a class that has a getBounds() method that returns the image's point in space.
Then your MouseListener can loop through each one and test if getBounds().contains(mouseEvent.getPoint());

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
public class ImageEntity {
    private Image image;
    private Rectangle2D.Double bounds;
   
    public ImageEntity(Image image, Rectangle2D.Double bounds) {
        this.image = image;
        this.bounds = bounds;
    }
   
    public Rectangle2D.Double getBounds() {
        return bounds;
    }
   
    public void draw(Graphics2D g) {
        g.drawImage(image,(int)Math.round(bounds.x),(int)Math.round(bounds.y),(int)Math.round(bounds.width),(int)Math.round(bounds.height));
    }
}


Also why are you using String.contentEquals(CharSequence)? You can just use String.equals(String).

Offline civilwar

Senior Newbie





« Reply #2 - Posted 2011-03-27 02:21:03 »

I also have another problem I am trying to load images w/ imagepanel class that loads images into a panel background and I am running into secruity errors.  I thought if the images where on the same domain as the applet they would load?

i.e.
ImagePanel cards = new ImagePanel(new ImageIcon("back.jpg").getImage()); 

how might I fix this for an applet?

aused by: java.security.AccessControlException: access denied (java.io.FilePermission back.jpg read)
   at java.security.AccessControlContext.checkPermission(Unknown Source)
   at java.security.AccessController.checkPermission(Unknown Source)
   at java.lang.SecurityManager.checkPermission(Unknown Source)
   at java.lang.SecurityManager.checkRead(Unknown Source)
   at sun.awt.SunToolkit.getImageFromHash(Unknown Source)
   at sun.awt.SunToolkit.getImage(Unknown Source)
   at javax.swing.ImageIcon.<init>(Unknown Source)
   at javax.swing.ImageIcon.<init>(Unknown Source)
   at foursidecard.strauss.<init>(strauss.java:44)
   at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
   at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
   at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
   at java.lang.reflect.Constructor.newInstance(Unknown Source)
   at java.lang.Class.newInstance0(Unknown Source)
   at java.lang.Class.newInstance(Unknown Source)
   at sun.plugin2.applet.Plugin2Manager$12.run(Unknown Source)
   at java.awt.event.InvocationEvent.dispatch(Unknown Source)
   at java.awt.EventQueue.dispatchEvent(Unknown Source)
   at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
   at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
   at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
   at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
   at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
   at java.awt.EventDispatchThread.run(Unknown Source)
Exception: java.lang.reflect.InvocationTargetException
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline deadly72

Junior Member





« Reply #3 - Posted 2011-03-27 03:59:13 »

Try using 

ImagePanel cards = new ImagePanel(new ImageIcon(getClass().getResource("back.jpg").getImage());

this should work if you are loading the files from a jar and if I'm not mistaken java will not allow you to read files outside an applet for security reasons.  I know this is one of the reasons and I'm pretty sure it's related to how you are trying to read the file.

Good luck.
Offline civilwar

Senior Newbie





« Reply #4 - Posted 2011-03-27 05:03:25 »

tried the above code but it wasn't seeming to work wasn't liking the .getimage at the end wanted .getfile .   I am using image panel class for loading background panel imageswhich i got from the net.

ImagePanel cards = new ImagePanel(new ImageIcon("back.jpg").getImage()); 

breaks secruity rules for applet  i suppose so I am trying to get past that by loading from jar archive?
any other ideas

code of imagepanel class
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ImageTest {

  public static void main(String[] args) {
    ImagePanel panel = new ImagePanel(new ImageIcon("images/background.png").getImage());

    JFrame frame = new JFrame();
    frame.getContentPane().add(panel);
    frame.pack();
    frame.setVisible(true);
  }
}

class ImagePanel extends JPanel {

  private Image img;

  public ImagePanel(String img) {
    this(new ImageIcon(img).getImage());
  }

  public ImagePanel(Image img) {
    this.img = img;
    Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
    setPreferredSize(size);
    setMinimumSize(size);
    setMaximumSize(size);
    setSize(size);
    setLayout(null);
  }

  public void paintComponent(Graphics g) {
    g.drawImage(img, 0, 0, null);
  }

}
Offline ra4king

JGO Kernel


Medals: 346
Projects: 3
Exp: 5 years


I'm the King!


« Reply #5 - Posted 2011-03-27 08:25:35 »

deadly forgot an extra parentheses after the getResource("back.jpg") call
1  
new ImageIcon(getClass().getResource("back.jpg")).getImage()


And if that image is in the same server or jar as this class then it should work.

Offline civilwar

Senior Newbie





« Reply #6 - Posted 2011-03-27 17:57:07 »

I am sorry I am not understanding I think I need to change the image panel class also which I posted above.  The above code says can not convert from image panel to image.  or if I try it w/ an image icon it says can not convert from image icon to image.
Offline ra4king

JGO Kernel


Medals: 346
Projects: 3
Exp: 5 years


I'm the King!


« Reply #7 - Posted 2011-03-27 18:04:10 »

ImageIcon is not a Type of Image. ImageIcon houses an Image but IS NOT an Image.
The best (and non-n00bish) way is to use javax.imageio.ImageIO.

1  
2  
3  
4  
5  
6  
7  
8  
ImagePanel panel;
try{
    panel = new ImagePanel(ImageIO.read(new URL(getCodeBase(),"back.jpg")));
}
catch(Exception exc) {
    System.out.println("Image not found: " + exc);
    return;
}

Offline civilwar

Senior Newbie





« Reply #8 - Posted 2011-03-27 23:57:01 »

I tried the above and it just just would not work : ( .  I ended up signing the applet and its working well now : )  maybe l8er down the road ill try to figure out what it doesnt like. ty guys!
Offline ra4king

JGO Kernel


Medals: 346
Projects: 3
Exp: 5 years


I'm the King!


« Reply #9 - Posted 2011-03-27 23:59:43 »

Glad to help Grin

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline civilwar

Senior Newbie





« Reply #10 - Posted 2011-03-28 23:16:08 »

could I create an array of class ImagePanel?  I know you can have an array of panel? how would that look?

panel1[1] = new ImagePanel(new ImageIcon(getClass().getResource("back.jpg")).getImage());  Huh?

heres the class for ImagePanel

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
class ImagePanel extends JPanel {

     private Image img;
     
     public ImagePanel(String img) {
       this(new ImageIcon(img).getImage());
     }
     public ImagePanel(Image img) {
       this.img = img;
     
       Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
       setPreferredSize(size);
       setMinimumSize(size);
       setMaximumSize(size);
       setSize(size);
       setLayout(null);
     }

     public void paintComponent(Graphics g) {
       g.drawImage(img, 0, 0, null);
     }

   }
Offline ra4king

JGO Kernel


Medals: 346
Projects: 3
Exp: 5 years


I'm the King!


« Reply #11 - Posted 2011-03-28 23:21:01 »

Why couldn't you create an array of ImagePanel? You can create an array of any type Smiley

EDIT: Using JPanel just to draw an image is not very efficient. Look at my first reply.

Offline civilwar

Senior Newbie





« Reply #12 - Posted 2011-03-28 23:28:10 »

I like your suggestion but I need to add events so panels work just as fast.  My problem when turning it into into an array is I can not add it or proper way to declare.
1  
2  
3  
4  
5  
6  
7  
8  
ImagePanel[] cards1[];

cards1[i] = new ImagePanel[10];

// not sure how to set
cards1[0] = new ImagePanel(new ImageIcon(getClass().getResource("name.jpg")).getImage());

add(cards1[0]);
Offline civilwar

Senior Newbie





« Reply #13 - Posted 2011-03-28 23:29:19 »

edit^ I will try your way( i see how you are doing mouse events) but how would that implementation work for above if you don't mind?
Offline ra4king

JGO Kernel


Medals: 346
Projects: 3
Exp: 5 years


I'm the King!


« Reply #14 - Posted 2011-03-29 01:29:39 »

How about using ArrayList?

And for event, your main class would have a nested class that implements ActionListener, and when you get a mouse click, you loop through the array of ImagePanels and test if the point of the click is inside one.
1  
2  
3  
4  
5  
for(ImagePanel ip : panels) {
    if(ip.getBounds().contains(actionEvent.getPoint().x,actionEvent.getPoint().y)) {
        //code when clicking here
   }
}

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

radar3301 (12 views)
2014-09-21 23:33:17

BurntPizza (30 views)
2014-09-21 02:42:18

BurntPizza (20 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (28 views)
2014-09-20 20:14:06

BurntPizza (32 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (102 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!