2

Dear wonderful people of stackoverflow

A group of my friends are attempting to make a level editor in Java.

We have a Jpanel instead of a Jframe and we are trying to put small images onto the Jpanel from a filepath saved as a string. In the end we want a list of images that you can just drop on. So far we have tried a few methods with no luck.

We can load the images, however we can't get these images to actually display, What would be the best means of solving said problem?

below is a sample of what we have so far.

EnemyPlacementGrid = new JPanel();
EnemyPlacementGrid.addMouseListener(new MouseAdapter() {
    //@Override
    public int mouseX;
    public int mouseY;
    public void mouseClicked(MouseEvent arg0) { //what happens when you click in the EnemyPlacementGrid
        System.out.println("Correct Area for placement");
        mouseX = arg0.getX();
        mouseY = arg0.getY();
        //System.out.println("X:" + mouseX + ", Y:" + mouseY );
        Enemy newEnemy = workingEnemy.cloneSelf();
        newEnemy.setLocation(mouseX, mouseY);
        System.out.println("newEnemy object: " + newEnemy);
        System.out.println(newEnemy.weaponList);
        currentWave.addEnemy(newEnemy);
        System.out.print(currentLevel);
    }
});

Any and all help is greatly appreciated.

UPDATE:

As of now I have an image appearing, however I can't update said image. Note code below:

public void run() {
                try {
                     BufferedImage img = ImageIO.read(new File(IMG_PATH));
                     ImageIcon icon = new ImageIcon(img);
                     WaveScreen frame = new WaveScreen();

                     JPanel panel = (JPanel)frame.getContentPane();  
                     JLabel label = new JLabel();  
                     label.setIcon(new ImageIcon("images/map_on.png"));// your image here  
                     panel.add(label);  


                    frame.setVisible(true);
                    panel.add(label);
                    panel.repaint(); 

                } catch (Exception e) {
                    e.printStackTrace();
                } 

update, method tried from comments:

Graphics2D g = null;
                        Graphics2D g2 = (Graphics2D)g;
                        Image imageVariable = new ImageIcon("images/map_on.png").getImage();
                       g.drawImage(imageVariable, mouseX, mouseY, null);
Drew L. Facchiano
  • 283
  • 3
  • 5
  • 12

2 Answers2

2

Well, i'd say to try using Graphics, meaning you need to override the paint method; i'd recommend that you put the mouseX and mouseY as global variables though…

// creating global image variable for use later
Image imageVariable = new ImageIcon("image path").getImage();

public void paintComponent(Graphics g) {
   // here you could either create a Graphics2D object
   // Graphics2D g2 = (Graphics2D)g;
   // or you could use the g parameter as it is, doesn't matter.
   // use the global variable for the image to be drawn onto the screen
   // use the global value of the mouseX and mouseY for where you click the mouse
   // to place the image, and this should be it 
   g.drawImage(imageVariable, mouseX, mouseY, null);
}

Hope this helps!

user2277872
  • 2,963
  • 1
  • 21
  • 22
  • getting the image each time the `paint` method runs? It would be wiser to load the image once and only draw it during `paint`. – bas Aug 11 '13 at 01:17
  • 2
    yes, you are correct about that. I had completely left that out of play. Thanks will update – user2277872 Aug 11 '13 at 01:18
  • @bas @user2277872 Wouldn't `paintComponent` be more ideal? – ndfkj asdkfh Aug 11 '13 at 01:26
  • well I guess it could be, but I've always used paint instead of paintComponent. – user2277872 Aug 11 '13 at 01:27
  • paintComponent is used mainly for separate components, paint is ideally used for the entire frame. Either would work well. – bas Aug 11 '13 at 01:28
  • Well I've never really done any kind of drawing that wasn't used for certain components, so i've only dealt with paint – user2277872 Aug 11 '13 at 01:41
  • 2
    Overriding `paint` of a top level component is ill advised, as it is not double buffered and you have to contend with the frame decorations as well. Unless you have significant reason to do otherwise, I would strongly advise to always use `paintComponent`. It will allow you to mix components and custom painting if or when required - IMHO – MadProgrammer Aug 11 '13 at 03:54
  • well you're not doing the drawing within the paintComponent method in the first place. you have to create a separate method named public void paintComponent(Graphics g) so that you can override it. Then you can draw.. just put your Graphics2D object and image variable(s)/drawImage method into the paintComponent method – user2277872 Aug 15 '13 at 15:36
  • I have done as you suggested. No errors are thrown but the image is not being redrawn. The method is being hit, I think I'm not updating properly. Any thoughts? – Drew L. Facchiano Aug 17 '13 at 19:40
  • instead of panel.repaint() try just repaint() – user2277872 Aug 18 '13 at 15:39
2

If the game is simple, user2277872's solution will work and you can use graphics2D from java. However, if you are planning on a more sophisticated game (lots of interaction, lots of textures), then the default Java framework for 2D graphics will prove to be too slow.

If you are planning on such a game, I can highly recommend either learning OpenGL or using an existing framework for graphics, such as

JMonkeyEngine (http://jmonkeyengine.com/) or Slick (http://slick.cokeandcode.com/index.php)

More information: What should I use to display game graphics?

Community
  • 1
  • 1
bas
  • 1,678
  • 12
  • 23