I have the object 'Team' with some score variables, the name (String) and a logo. The logo has the type ImageIcon:
public class Team {
public String name;
public ImageIcon logo;
public int points;
public int plusGoals;
public int minGoals;
public int goalsTotal;
public Team (String name, ImageIcon logo, int points, int plusGoals, int minGoals, int goalsTotal){
this.name = name;
this.logo = logo;
this.points = points;
this.plusGoals = plusGoals;
this.minGoals = minGoals;
goalsTotal = plusGoals - minGoals;
When I want to create a new object, and I enter the values of the object properties, I don't know how I can add the ImageIcon path.
So:
Team Blabla = new Team("Blabla", ..., 0, 0, 0, 0);
I tried this things, but they doens't work:
Team Blabla = new Team("Blabla", C:\\Users\\path.png, 0, 0, 0, 0);
Team Blabla = new Team("Blabla", "C:\\Users\\path.png", 0, 0, 0, 0);
Team Blabla = new Team("Blabla", ImageIcon("C:\\Users\\path.png"), 0, 0, 0, 0);
How can I directly add an image path in this line?
You can make a modification like:
public Team(String name, String location, int points, int plusGoals,
int minGoals, int goalsTotal) {
this.logo = new ImageIcon(location); // using ImageIcon(URL location)
}
Note: Here we are using ImagIcon class Constructor -> ImageIcon(URL location) which Creates an ImageIcon from the specified URL.
Working code
import javax.swing.ImageIcon;
class Team {
public String name;
public ImageIcon logo;
public int points;
public int plusGoals;
public int minGoals;
public int goalsTotal;
public Team(String name, String location, int points, int plusGoals,
int minGoals, int goalsTotal) {
this.logo = new ImageIcon(location); // using ImageIcon(URL location)
this.name = name;
this.points = points;
this.plusGoals = plusGoals;
this.minGoals = minGoals;
goalsTotal = plusGoals - minGoals;
}
public void print() {
System.out.println("\n" + name + "\n" + logo + "\n" + points + "\n"
+ plusGoals + "\n" + minGoals + "\n" + goalsTotal);
}
}
public class imageicon {
public static void main(String[] args) {
Team obj = new Team("a", "C:\\Users\\path.png", 1, 2, 3, 4);
obj.print();
}
}