internal class methods seem invisible?
in
Programming Questions
•
3 years ago
Hello,
I am writing a program that, in its setup, wants to read words from a text file and save them into an object of class word, along with the coordinates of the word-objects center, and a randomly generated greyscale value fill. I want processing to arrange the word squares in a grid pattern, which doesn't seem like it would be too tricky (I'm new to both processing and programming) but I'm having some issues:
This is my code so far:
ArrayList words;
void setup() {
size(600, 600);
words= new ArrayList();
BufferedReader reader = createReader("Story.txt");
String currentLine;
String wholeStory;
//add all of the txt to one string
try {
while ((currentLine = reader.readLine()) != null) {
wholeStory+=currentLine;
}
}
catch (Exception e) {
e.printStackTrace();
}
//take out all punctuation using regex, make new word object containing a word,
//and add it to the array
//Pattern parserPat = Pattern.compile("([a-zA-Z]*?)");
//Matcher wordFinder = parserPat.matcher(wholeStory);
/*while (wordFinder.find()) {
Word word = new Word();
word.setWord("hi");
words.add(word);
}*/
Word word = new Word();
word.setWord("hi");
words.add(word);
System.out.println(words);
System.out.println("here");
//assign an x and y coordinate to each word object
int closestSquare=(int)(Math.ceil(Math.sqrt(words.size())));
//length of one side of board
int squareSide=width/closestSquare;
int count=0;
for (int i=0; i<closestSquare; i++){
for (int j=0; j<closestSquare; j++){
words.get(count).setCenterX((squareSide/2)+squareSide*j);
words.get(count).setCenterY((squareSide/2)+squareSide*i);
words.get(count).setRGB(int(random(255)));
count++;
}
}
//draw the squares
rectMode(CENTER);
for(int k=0; k<words.size(); k++){
fill=word.getRGB();
rect(word.getCenterX(), word.getCenterY(), squareSide, squareSide);
}
}
void Draw(){
rectMode(CENTER);
}
public class Word {
/*this class is the word object which contains variables for the value of the
word, the coordinates of its center, and its fill color.*/
String value;
int xCenter;
int yCenter;
int greyRGB;
public void setWord(String input){
input=value;
}
public String getWord(){
return word;
}
public void setCenterX (int x){
xCenter= x;
}
public int getCenterX (){
return centerX;
}
public void setCenterY (int y){
yCenter= y;
}
public int getCenterY (){
return centerX;
}
public void setColor (int rgb) {
greyRGB= rgb;
}
public int getColor(){
return greyRGB;
}
public String toString(){
return value;
}
}
and the issue is that not only is it not seeing internal methods like setCenterX(), it's not even printing my println, even when outside of the while-loop. Also, was fretting about my regex, and not compliling "ArrayList<Word>" words.
I'm thinking about ditching my internal class entirely and using hashmaps, but it seems like I'm missing some trivial beans.
I am writing a program that, in its setup, wants to read words from a text file and save them into an object of class word, along with the coordinates of the word-objects center, and a randomly generated greyscale value fill. I want processing to arrange the word squares in a grid pattern, which doesn't seem like it would be too tricky (I'm new to both processing and programming) but I'm having some issues:
This is my code so far:
ArrayList words;
void setup() {
size(600, 600);
words= new ArrayList();
BufferedReader reader = createReader("Story.txt");
String currentLine;
String wholeStory;
//add all of the txt to one string
try {
while ((currentLine = reader.readLine()) != null) {
wholeStory+=currentLine;
}
}
catch (Exception e) {
e.printStackTrace();
}
//take out all punctuation using regex, make new word object containing a word,
//and add it to the array
//Pattern parserPat = Pattern.compile("([a-zA-Z]*?)");
//Matcher wordFinder = parserPat.matcher(wholeStory);
/*while (wordFinder.find()) {
Word word = new Word();
word.setWord("hi");
words.add(word);
}*/
Word word = new Word();
word.setWord("hi");
words.add(word);
System.out.println(words);
System.out.println("here");
//assign an x and y coordinate to each word object
int closestSquare=(int)(Math.ceil(Math.sqrt(words.size())));
//length of one side of board
int squareSide=width/closestSquare;
int count=0;
for (int i=0; i<closestSquare; i++){
for (int j=0; j<closestSquare; j++){
words.get(count).setCenterX((squareSide/2)+squareSide*j);
words.get(count).setCenterY((squareSide/2)+squareSide*i);
words.get(count).setRGB(int(random(255)));
count++;
}
}
//draw the squares
rectMode(CENTER);
for(int k=0; k<words.size(); k++){
fill=word.getRGB();
rect(word.getCenterX(), word.getCenterY(), squareSide, squareSide);
}
}
void Draw(){
rectMode(CENTER);
}
public class Word {
/*this class is the word object which contains variables for the value of the
word, the coordinates of its center, and its fill color.*/
String value;
int xCenter;
int yCenter;
int greyRGB;
public void setWord(String input){
input=value;
}
public String getWord(){
return word;
}
public void setCenterX (int x){
xCenter= x;
}
public int getCenterX (){
return centerX;
}
public void setCenterY (int y){
yCenter= y;
}
public int getCenterY (){
return centerX;
}
public void setColor (int rgb) {
greyRGB= rgb;
}
public int getColor(){
return greyRGB;
}
public String toString(){
return value;
}
}
and the issue is that not only is it not seeing internal methods like setCenterX(), it's not even printing my println, even when outside of the while-loop. Also, was fretting about my regex, and not compliling "ArrayList<Word>" words.
I'm thinking about ditching my internal class entirely and using hashmaps, but it seems like I'm missing some trivial beans.
1