I need to create a processing app with 4 buttons (each having different strokecolor, fillcolor and textSize). I have followed the instructions given to me from uni and created 3 classes, for buttons, styles and styleCollection. i am stuck at this point:
"Modify the display function in the Button class to use the text label of the button to get the Style associated with that name from the StyleCollection and use the background, fill, stroke, and text size from that style to style the button using those settings. Now display your buttons on the canvas. Update your draw function to display the buttons. Use a while or for statement to iterate through the array of buttons, calling the display function of each button. ."
I have tried to modify the button.display function but it keeps saying nullPointerException error.
//Setup tab
String message;
String[] styleNames={"dark","light","red","blue"};
StyleCollection styles;
Style currentStyle;
Button b1;
Button[] buttons;
float bx=150;
void setupButtons(){
buttons = new Button[styleNames.length];
for (int index=0;index<styleNames.length;index++){
buttons[index]= new Button(bx,500);
buttons[index].setLabel(styleNames[index]);
bx=bx+150;
}
}
void setupStyles(){
styles = new StyleCollection();
Style darkStyle= new Style("dark",0,255,255,15);
styles.addStyle(darkStyle);
Style lightStyle= new Style("light",255,0,0,15);
styles.addStyle(lightStyle);
Style redStyle= new Style("red",#D69797,#FF0000,#FF0000,15);
styles.addStyle(redStyle);
Style blueStyle= new Style("blue",#8788EA,#0000FF,#0000FF,15);
styles.addStyle(redStyle);
styles.setDefaultStyle(blueStyle);
currentStyle=styles.getDefaultStyle();
}
void setup(){
size(800,600);
setupStyles();
setupButtons();
bx = 150;
b1 = new Button(100,200);
message = ("Lorem Ipsum is simply dummy text of the printing and typesetting industry. " +
"Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, "+
"when an unknown printer took a galley of type and scrambled it to make a type "+
"specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, "+
"remaining essentially unchanged. It was popularised in the 1960s with the release of "+
"Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing "+
"software like Aldus PageMaker including versions of Lorem Ipsum.");
println(styleNames.length);
}
void draw(){
background(currentStyle.getBackground());
fill(currentStyle.getFillColor());
textSize(currentStyle.getTextSize());
text(message,100,100, width/2,height);
buttons[0].display();
buttons[1].display();
buttons[2].display();
buttons[3].display();
}
class Style{
private int fillColor;
private int strokeColor;
private int bgColor;
private int textSize;
private String name;
public Style(String nm, int bgCol, int strCol, int fC, int tSize){
name=nm;
bgColor=bgCol;
strokeColor=strCol;
fillColor=fC;
textSize=tSize;
}
public String getSName(){ return name;}
public int getBackground(){return bgColor;}
public int getFillColor(){return fillColor;}
public int getStrokeColor(){return strokeColor;}
public int getTextSize(){return textSize;}
public String toString(){return name+ " "+bgColor;}
}
import java.util.HashMap;
class StyleCollection{
private HashMap <String,Style> styles;
public StyleCollection(){
styles= new HashMap <String, Style>();
}
public void addStyle(Style s){
styles.put(s.getSName(),s);
}
public void setDefaultStyle(Style sD){
styles.put("default", sD);
}
public Style getStyle(String stname){
return styles.get(stname);
}
public Style getDefaultStyle(){
return styles.get("default");
}
}
class Button{
StyleCollection s;
private float x,y,w,h;
private String label;
Style format;
public Button(float _x, float _y){
s=new StyleCollection();
x=_x;
y=_y;
w=100;
h=75;
}
public void display(){
format = s.getStyle(label);
fill(255);
rect(x,y,w,h);
textAlign(CENTER);
fill(0);
//textSize(format.getTextSize());
text(label,x+w/2,y+h/2);
}
public void setLabel(String _label){
label=_label;
}
public String getText(){
return label;
}
boolean isInside(float _mx, float _my){
float mx,my;
mx=_mx;
my=_my;
if((mx>x)&&(mx<x+w)&&(my>y)&&(my<y+h)){
return true;
}
else{
return false;
}
}
}