This site requires JavaScript, please enable it in your browser!
Greenfoot back
Dom05
Dom05 wrote ...

2017/12/8

How to place objects in order as they are read from a text file

Dom05 Dom05

2017/12/8

#
How can i use an array so that when i choose a certain text file to read from to create a object those object created are placed from the left to right depending on the order they are read from the text file?
danpost danpost

2017/12/8

#
Which part are you having trouble with -- reading from a text file, using an array, or placing the objects? Things that may be required depending on your needs are: * format of text files * how the text files are read in * the type of array you are using * how you fill the array * how what is read in is translated into objects * how the objects are to be arranged in the world (more precisely -- spacing horizontally is missing, for one thing) * any and all related codes you may already have with regard to the issue
Dom05 Dom05

2017/12/8

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.Scanner;
import java.awt.FileDialog;
import java.io.*;
//import java.awt.Color;
import java.util.ArrayList;
 
/**
 * Write a description of class Load here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Load extends Button
{
  //  private Scanner reader;
    private Color col;
     
    public Load(){
        super("Load");
         
    }
     
    /**
     * The handle method will do whatever the Button is intended to do
     *   *** remember to write your own version of this method in a
     *   *** subclass !!!
     */
    public void handle(){
       FileDialog fd = null;
        fd = new FileDialog(fd, "Choose a file", FileDialog.LOAD); // titles the GUI choose a file
        fd.setVisible(true);
        String fname = fd.getDirectory() + fd.getFile();
        File myFile = new File(fname); // finds the files name and path and stores it as myFile
        Scanner reader=null; //scanner is empty
             
             
 
        try
         {
            reader = new Scanner(myFile);
          }
            catch (FileNotFoundException e)
            {
                System.out.println("I am sorry but " + fname + " is not a valid file!");
                return;
            }
         
        //ArrayList<String> list = new ArrayList<String>();
        while(reader.hasNext()){
                //list.add(reader.next());
                String a = reader.next(); // when text file is read sets the first word to string a
                if (a.equals("red")){ // is string a is exactly equal to the word
                    int width = reader.nextInt(); // sets the second piece of info to int xLoc
                    int height = reader.nextInt(); // sets the third piece of info on the same line
             
                    MyWorld w;
                    w = (MyWorld)getWorld();
                     
                    col = Color.RED;
                    Present gifts = new Present(width, height, col);
                    w.addObject(gifts, width, height);
                }
                 
                if (a.equals("blue")){ // is string a is exactly equal to the word
                    int width = reader.nextInt(); // sets the second piece of info to int xLoc
                    int height = reader.nextInt(); // sets the third piece of info on the same line
                     
                    MyWorld w;
                    w = (MyWorld)getWorld();
                     
                    col = Color.BLUE;
                    Present gifts = new Present(width, height, col);
                    w.addObject(gifts, width, height);
                }
                 
                if (a.equals("green")){ // is string a is exactly equal to the word
                    int width = reader.nextInt(); // sets the second piece of info to int xLoc
                    int height = reader.nextInt(); // sets the third piece of info on the same line
                    World w;
                    w = this.getWorld();
                    col = Color.GREEN;
                     
                    Present gifts = new Present(width, height, col);
                    w.addObject(gifts, width, height);
                }
            }
            reader.close(); // closes the scanner
        }  
    }
Dom05 Dom05

2017/12/8

#
so I have it adding the object to the world but they are not added from left to right in the order they are being read from in the text file
Dom05 Dom05

2017/12/8

#
blue 75 80 red 100 53 green 25 100 green 100 25 red 53 100 blue 100 53 red 50 50 green 50 50 blue 50 50 green 50 50 blue 25 100 this is the text files I am to use
Super_Hippo Super_Hippo

2017/12/8

#
You add them at 'width' and 'height'. If you want to add them in a horizontal line, you maybe want to use:
1
2
3
4
5
6
7
8
9
//line 48
int x = 20, y = 200; //location of the first present
 
//when adding
w.addObject(gifts, x, y);
 
 
//after line 86
x+=50; //next present 50 cells right from this one
Dom05 Dom05

2017/12/8

#
okay! so that works and they are in order and in a straight line but they are on lop of each other, how do I place each present right to the left of the one before it but so they don't overlap AT ALL? @Super_Hippo
Super_Hippo Super_Hippo

2017/12/8

#
Then you have to add 'width' instead of 50. So either move this line to each present or have 'int width' at the start so you can use it there.
Dom05 Dom05

2017/12/8

#
the first line read in must start at the left of the world. then the next line read in must be placed exactly to the right of that next present.
Super_Hippo Super_Hippo

2017/12/8

#
If you want more than one line, then you need to check if x reached a certain value and if it did, increase y and reset x.
Dom05 Dom05

2017/12/8

#
so what would the code look like if every present must only touch the one before its right side? I'm still kind of confused.
Dom05 Dom05

2017/12/8

#
so to be clear, sorry I didn't make much sense, but what would the pieces of code that would make them all have the same y value but no touch each other, and also what would it look like for if there was no more room to start a new line?
Super_Hippo Super_Hippo

2017/12/8

#
Either
1
2
3
4
5
6
7
//line 51
int width=0, height=0;
 
//remove 'int' from lines 54, 55, 66, 67, 78 and 79
 
//after line 86
x+=width;
Or instead of this x+=width line 86, put it after lines 62, 74 and 85. And to check if there is enough room:
1
2
3
4
5
6
7
if (x+width > getWidth())
//or if you don't want half of the present to be outside of the world
if (x + width/2 > getWidth())
{
    x = width/2; //maybe you also want to add the first present at half width, too
    y+=100; //need to use a fixed height here because not all presents have same height and then the next line might be in the middle of the other one if the last present was small
}
Dom05 Dom05

2017/12/9

#
does anyone know by chance say I have a class that must sort the present objects, using an array, by color? or by area? And sort I mean like it runs through the text file and adds the red presents first, then blue, then green.
Super_Hippo Super_Hippo

2017/12/9

#
I just threw this together. Not tested or anything. Hope it works and/or helps.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public void sortByColor(Color[] colorOrder)
{
    int x=0, y=200;
    for (int col=0; col<colorOrder.length; col++)
    {
         
         
        // -- start at the begin of the text file
         
        Color currentColor = colorOrder[col];
        while(reader.hasNext())
        {
            int width=0, height=0;
            String a = reader.next();
            String color = null;
            if (currentColor.equals(Color.RED)) color = "red";
            else if (currentColor.equals(Color.BLUE)) color = "blue";
            else if (currentColor.equals(Color.GREEN)) color = "green";
            else if (currentColor != null)
            {
                System.out.println("Color has to be red, blue or green");
                return;
            }
            else return;
         
            if (color.equals(a))
            {
                width = reader.nextInt();
                height = reader.nextInt();
                x += width/2;
                if (x + width/2 > getWorld().getWidth) {y+=100; x = width/2;}
                getWorld().addObject(new Present(width, height, currentColor), x, y);
                x += width/2;
            }
        }
    }
}
You need to login to post a reply.