Setting location for objects/images on a background

Mateo Matijević Bošnjak :

I'm currently working on a version of the game Ludo/Dude don't get mad, I've ran into a few problems one of the bigger ones being the fact that I cannot place the tokens where I want them to be. I need to set the tokens in certain positions for the start and will need a method with which I will calculate where to move them on the screen here is the code:

public class Ludo extends JFrame{

   private BackgroundPanel imagePanel=null;
   private JLabel jlTokenBlue1=null;
   private JLabel jlTokenBlue2=null;
   ...

   public Ludo(){
          try{         
         //Creating background panel  
         imagePanel = new BackgroundPanel("ludoBoard.png");
         add(imagePanel);

         //create tokens for play
         BufferedImage imageTokenBlue1 = ImageIO.read(new File("blue1.png"));
         jlTokenBlue1 = new JLabel(new ImageIcon(imageTokenBlue1));

         BufferedImage imageTokenBlue2 = ImageIO.read(new File("blue2.png"));
         jlTokenBlue2 = new JLabel(new ImageIcon(imageTokenBlue2));

         BufferedImage imageTokenBlue3 = ImageIO.read(new File("blue3.png"));
         jlTokenBlue3 = new JLabel(new ImageIcon(imageTokenBlue3));

         BufferedImage imageTokenBlue4 = ImageIO.read(new File("blue4.png"));
         jlTokenBlue4 = new JLabel(new ImageIcon(imageTokenBlue4));

         BufferedImage imageTokenRed1 = ImageIO.read(new File("red1.png"));
         jlTokenRed1 = new JLabel(new ImageIcon(imageTokenRed1));

         BufferedImage imageTokenRed2 = ImageIO.read(new File("red2.png"));
         jlTokenRed2 = new JLabel(new ImageIcon(imageTokenRed2));

         BufferedImage imageTokenRed3 = ImageIO.read(new File("red3.png"));
         jlTokenRed3 = new JLabel(new ImageIcon(imageTokenRed3));

         BufferedImage imageTokenRed4 = ImageIO.read(new File("red4.png"));
         jlTokenRed4 = new JLabel(new ImageIcon(imageTokenRed4));

         imagePanel.add(jlTokenBlue1);
         imagePanel.add(jlTokenBlue2);
         imagePanel.add(jlTokenBlue3);
         imagePanel.add(jlTokenBlue4);
         imagePanel.add(jlTokenRed1);
         imagePanel.add(jlTokenRed2);
         imagePanel.add(jlTokenRed3);
         imagePanel.add(jlTokenRed4);

         jlTokenRed1.setLocation(250,500);
         }//End of try
      catch(IOException ioe){
         System.out.println("The background image cannot be loaded...");
      }catch(NullPointerException npe){
         npe.getMessage();
      }
.
.
.
class BackgroundPanel extends JPanel
{
  Image image;
  public BackgroundPanel(String imageName)
  {
    try
    {
      image = javax.imageio.ImageIO.read(new java.net.URL(getClass().getResource(imageName), imageName));
    }
    catch (Exception e) { /*handled in paintComponent()*/ }
  }

  @Override
  protected void paintComponent(Graphics g)
  {
    super.paintComponent(g); 
    if (image != null)
      g.drawImage(image, 0,0,this.getWidth(),this.getHeight(),this);
  }
}

I've tried using setLocation and repaint but was unable to set the tokens to be placed where I want them to be. I'd like to place the tokens into the white fields in their respective colors any help and/or suggestion is greatly appreciated. Below is a picture of how it looks right now. Here is the link to the drive containing everything if you wish to check the files your self. drive.google.com/open?id=1xap_xz2K3SF37XQfRN3Y_LHoTHg0HNox !

İmage

Gilbert Le Blanc :

I've started some code for your game. You'll have to complete the code.

Here's the GUI I created.

Ludo  board

The first thing I did was to employ a model / view / controller pattern. By separating the logical functions of the game, I could concentrate on one part at a time.

Here's the model I created. It's not complete, but I think you can see what I'm trying to do herd. I put his class in its own model package.

package com.ggl.ludo.model;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

public class LudoModel {

    private final BufferedImage gameBoard;

    private final BufferedImage[] imageTokenBlue;

    public LudoModel() {
        imageTokenBlue = new BufferedImage[4];
        imageTokenBlue[0] = getImage("blue1.png");
        imageTokenBlue[1] = getImage("blue2.png");
        imageTokenBlue[2] = getImage("blue3.png");
        imageTokenBlue[3] = getImage("blue4.png");
        gameBoard = getImage("ludoBoard.png");
    }

    public BufferedImage getGameBoard() {
        return gameBoard;
    }

    public BufferedImage[] getImageTokenBlue() {
        return imageTokenBlue;
    }

    private BufferedImage getImage(String fileName) {
        try {
            return ImageIO.read(getClass().getResourceAsStream("/" + fileName));
        } catch (IOException e) {
            return null;
        }
    }

}

All this code does is read in the images, and eventually, create the logical model of your game. It's not concerned with the view or the controller code at all.

The next class I created was the DrawingPanel class. This class draws the game board and eventually, draws the pieces on the board. The location of the pieces information goes into the model.

package com.ggl.ludo.view;

import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JPanel;

import com.ggl.ludo.model.LudoModel;

public class DrawingPanel extends JPanel {

    private static final long serialVersionUID = 1L;

    private LudoFrame frame;

    private LudoModel model;

    public DrawingPanel(LudoFrame frame, LudoModel model) {
        this.frame = frame;
        this.model = model;
        int width = model.getGameBoard().getWidth();
        int height = model.getGameBoard().getHeight();
        this.setPreferredSize(new Dimension(width, height));
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(model.getGameBoard(), 0, 0, this);
        // Your piece drawing code goes here
    }

}

Next, I created a class to hold the JFrame. The JScrollPane code can be removed. My old laptop can't display a game board as large as yours. Something to keep in mind if you want others to play this game.

I put the menu code in its own method. I like to keep separate things separate. My feeble mind can't handle too many processes at one time.

package com.ggl.ludo.view;

import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;

import com.ggl.ludo.model.LudoModel;

public class LudoFrame {

    private DrawingPanel drawingPanel;

    private LudoModel ludoModel;

    public LudoFrame(LudoModel ludoModel) {
        this.ludoModel = ludoModel;
        generateGUIControl();
    }

    private void generateGUIControl() {
        JFrame frame = new JFrame("Ludo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setJMenuBar(createMenu());

        drawingPanel = new DrawingPanel(this, ludoModel);
        JScrollPane scrollPane = new JScrollPane(drawingPanel);
        scrollPane.setPreferredSize(new Dimension(400, 400));

        frame.add(scrollPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JMenuBar createMenu() {
        JMenuBar ludoMenuBar = new JMenuBar();
        JMenu ludoMenuFirst = new JMenu("Information");
        ludoMenuBar.add(ludoMenuFirst);

        JMenuItem jmiDescription = new JMenuItem("Description");
        JMenuItem jmiHelp = new JMenuItem("Help");
        JMenuItem jmiRestart = new JMenuItem("Restart");
        JMenuItem jmiExit = new JMenuItem("Exit");

        ludoMenuFirst.add(jmiDescription);
        ludoMenuFirst.add(jmiHelp);
        ludoMenuFirst.add(jmiRestart);
        ludoMenuFirst.add(jmiExit);
        ludoMenuBar.add(ludoMenuFirst);
        return ludoMenuBar;
    }
}

Finally, the driver class that starts the whole process. You must start a Swing application on the Event Dispatch Thread(EDT). The call to SwingUtilities invokeLater ensures that your application starts on the EDT.

package com.ggl.ludo;

import javax.swing.SwingUtilities;

import com.ggl.ludo.model.LudoModel;
import com.ggl.ludo.view.LudoFrame;

public class Ludo implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Ludo());
    }

    @Override
    public void run() {
        new LudoFrame(new LudoModel());
    }
}

I hope this has been helpful to you. As far as the controllers, I suggest that you use a mouse listener and draw the game pieces on the game board.

Remember, information about the game piece locations goes into the model. The view drawing code draws from the information in the model.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=31869&siteId=1