Skip to content

Commit

Permalink
resource get
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasMW committed Mar 3, 2017
1 parent 9bc15b8 commit 0e0a468
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/com/menezesworks/warclient/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class Client
public Client(int socketNumber)
{
this.socketNumber = socketNumber;
this.ipLocalhost = "127.0.0.1";
this.ipLocalhost = "45.55.91.148";
}
public void connect(String ip, String identifierName, int colorNum) throws UnknownHostException, IOException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ public void actionPerformed(ActionEvent ae) {

private void generateDice() {
for (int i = 0; i < WarLogic.MAX_DICE; i++) {
ImageIcon iconA = new ImageIcon("resources/dice/dado_ataque_1.png");
ImageIcon iconB = new ImageIcon("resources/dice/dado_defesa_1.png");
ImageIcon iconA = new ImageIcon(ResourceUtil.getResource("dice/dado_ataque_1.png"));
ImageIcon iconB = new ImageIcon(ResourceUtil.getResource("dice/dado_defesa_1.png"));
JLabel diceA = new JLabel(iconA);
JLabel diceB = new JLabel(iconB);
diceA.setVisible(false);
Expand Down Expand Up @@ -158,8 +158,8 @@ private void rollDice(boolean attack) {
int i = 0;
for (int result : attack ? this.attackResults : this.defenseResults) {
ImageIcon imgX;
imgX = new ImageIcon(String.format("resources/dice/dado_%s_%d.png",
attack ? "ataque" : "defesa", result));
imgX = new ImageIcon(ResourceUtil.getResource(String.format("dice/dado_%s_%d.png",
attack ? "ataque" : "defesa", result)));
JLabel dice = attack ? attackerDice.get(i) : defenderDice.get(i);
dice.setIcon(imgX);
dice.setVisible(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,16 @@ public CardSelectionFrame(Player p, int maxNumberOfCards,
if (c instanceof TerritoryCard) {
TerritoryCard tc = (TerritoryCard) c;
imagePath = String.format(
"resources/cards/war_carta_%s.png",
"cards/war_carta_%s.png",
tc.getTerritory().getName().toLowerCase()
.replaceAll("\\s+", ""));
imagePath = Normalizer
.normalize(imagePath, Normalizer.Form.NFD);
imagePath = imagePath.replaceAll("[^\\x00-\\x7F]", "");
} else {
imagePath = "resources/cards/war_carta_coringa.png";
imagePath = "cards/war_carta_coringa.png";
}
ImageIcon cardImage = new ImageIcon(imagePath);
ImageIcon cardImage = new ImageIcon(ResourceUtil.getResource(imagePath));
Image resizedImage = cardImage.getImage().getScaledInstance(
cardLabel.getWidth(), cardLabel.getHeight(),
Image.SCALE_SMOOTH);
Expand Down
17 changes: 12 additions & 5 deletions src/org/puc/rio/inf1636/hglm/war/viewcontroller/MapPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;
import java.util.Observable;
Expand All @@ -22,6 +23,8 @@
import org.puc.rio.inf1636.hglm.war.model.Player;
import org.puc.rio.inf1636.hglm.war.model.Territory;

import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader;

@SuppressWarnings("serial")
public class MapPanel extends JPanel implements Observer {

Expand All @@ -33,9 +36,11 @@ public class MapPanel extends JPanel implements Observer {
private Dimension mapSize;
private List<JLabel> armiesLabels = new LinkedList<JLabel>();
private boolean labelsHidden = false;



public MapPanel() {
this.setBackgroundImage("resources/maps/war_tabuleiro_completo.png");

this.setBackgroundImage(ResourceUtil.getResource("maps/war_tabuleiro_completo.png"));
Dimension gameSize = Util.getGameSize();
this.mapSize = new Dimension((int) (gameSize.width * MULTIPLIER_X),
(int) (gameSize.height * MULTIPLIER_Y));
Expand Down Expand Up @@ -200,21 +205,23 @@ public void mouseReleased(MouseEvent e) {
}
}

public void setBackgroundImage(String path) {
public void setBackgroundImage(URL path) {
try {
System.out.println(path.toString());
this.backgroundImage = new ImageIcon(path).getImage();
} catch (Exception e) {
System.out.println(e.getMessage());
return;
}
}


public void toggleMapDisplay() {
this.labelsHidden = !this.labelsHidden;
if (labelsHidden) {
this.setBackgroundImage("resources/maps/war_tabuleiro_com_nomes.png");
this.setBackgroundImage(ResourceUtil.getResource("maps/war_tabuleiro_com_nomes.png"));
} else {
this.setBackgroundImage("resources/maps/war_tabuleiro_completo.png");
this.setBackgroundImage(ResourceUtil.getResource("maps/war_tabuleiro_completo.png"));

}
this.updateArmyLabels(false);
Expand Down
35 changes: 35 additions & 0 deletions src/org/puc/rio/inf1636/hglm/war/viewcontroller/ResourceUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.puc.rio.inf1636.hglm.war.viewcontroller;

import java.net.URL;

import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader;

public class ResourceUtil {

//from http://stackoverflow.com/questions/3861989/preferred-way-of-loading-resources-in-java
public static URL getResource(String resource){

URL url ;

//Try with the Thread Context Loader.
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if(classLoader != null){
url = classLoader.getResource(resource);
if(url != null){
return url;
}
}

//Let's now try with the classloader that loaded this class.
classLoader = Loader.class.getClassLoader();
if(classLoader != null){
url = classLoader.getResource(resource);
if(url != null){
return url;
}
}

//Last ditch attempt. Get the resource from the classpath.
return ClassLoader.getSystemResource(resource);
}
}

0 comments on commit 0e0a468

Please sign in to comment.