1

this is the GUI class:

package view;


import javax.swing.*;

import controller.RollDiceWindowController;
import model.GameEngineImpl;

import java.awt.FlowLayout;


public class RollDiceWindow extends JPanel{
GameEngineImpl gameengineimpl;

private ImageIcon dice1=new ImageIcon("src/dice1.png");
private ImageIcon dice2=new ImageIcon("src/dice2.png");
private ImageIcon dice3=new ImageIcon("src/dice3.png");
private ImageIcon dice4=new ImageIcon("src/dice4.png");
private ImageIcon dice5=new ImageIcon("src/dice5.png");
private ImageIcon dice6=new ImageIcon("src/dice6.png");

public RollDiceWindow(GameEngineImpl gameengineimpl){
    setLayout(new FlowLayout(FlowLayout.LEFT,50,50));
    JButton jbtRoll= new JButton("Roll for player");
    add(jbtRoll);

    jbtRoll.addMouseListener(new RollDiceWindowController(gameengineimpl,this));
    add(new JLabel(dice1));
    add(new JLabel(dice1));




}

int dice=(int)(Math.random()*6+1);
int dicee=(int)(Math.random()*6+1);
public int getdice(){

    return dice;
}
public int getdicee(){
    return dicee;
}



public static void main(String[] args) {
    // TODO Auto-generated method stub
    GameEngineImpl gameengineimpl=new GameEngineImpl();
    RollDiceWindow frame=new RollDiceWindow(gameengineimpl);
    frame.setSize(600, 600);
    frame.setVisible(true);
}



}

this is the controller class:

package controller;

import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

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

import view.MainWindow;
import view.RollDiceWindow;
import model.GameEngineImpl;
import model.SimplePlayer;
import model.interfaces.DicePair;
import model.interfaces.GameEngine;


public class RollDiceWindowController extends JPanel implements MouseListener{

private GameEngine gameengine;
private RollDiceWindow rollDiceWindow;
private ImageIcon dice1=new ImageIcon("src/dice1.png");
private ImageIcon dice2=new ImageIcon("src/dice2.png");
private ImageIcon dice3=new ImageIcon("src/dice3.png");
private ImageIcon dice4=new ImageIcon("src/dice4.png");
private ImageIcon dice5=new ImageIcon("src/dice5.png");
private ImageIcon dice6=new ImageIcon("src/dice6.png");
private JLabel label;
//private int initialDelay=1;
//private int finalDelay=100;
//private int delayIncrement=20;
private SimplePlayer simplePlayer;
MainWindow mainwindow;
public RollDiceWindowController(GameEngine gameengine, RollDiceWindow   rollDiceWindow){
    this.gameengine=gameengine;
    this.rollDiceWindow=rollDiceWindow;

}







@Override
public void mouseClicked(MouseEvent arg0) {
    int initialDelay=1;
    int finalDelay=100;
    int delayIncrement=20;
    //simplePlayer=new SimplePlayer("1","jason",200);
    //gameengine.rollPlayer(simplePlayer, initialDelay, finalDelay, delayIncrement);
    label.add(new JLabel(dice1));

}









@Override
public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent arg0) {
    // TODO Auto-generated method stub

}


}

My question is in the controller class, I try to add a new label which is a PNG, I tried a couple of ways, but the PNG never shows in the frame.

Radiodef
  • 37,180
  • 14
  • 90
  • 125
Jason6666
  • 43
  • 5
  • `src/dice1.png` <- You should NEVER reference the `src` directory in ANY path element. Instead you will probably need to use `new ImageIcon(getClass().getResource("/dice1.png"))` – MadProgrammer Apr 24 '15 at 02:05
  • `rollDiceWindow.add(new JLabel(...));`...? – MadProgrammer Apr 24 '15 at 02:06
  • Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. – Andrew Thompson Apr 24 '15 at 02:38
  • @MadProgrammer Hi sir, i think it is not the problem of the src, since i can see my label shows in my frame by add(new JLabel(dice1)); – Jason6666 Apr 24 '15 at 07:24
  • It might not be the "immediate" problem but it "will" be a problem. Try making a Jar out of the project and running it outside of the IDE – MadProgrammer Apr 24 '15 at 07:26

0 Answers0