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

2020/4/10

need help with my file dialog

1
2
3
4
Cats123 Cats123

2020/4/10

#
need to get birds on the screen using a separate file tis is what i have for it. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.FileDialog; import java.io.*; import java.util.Scanner; public class MyWorld extends World { /** * Constructor for objects of class MyWorld. * */ public MyWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(800, 600, 1); prepare(); } public void prepare() { CircleMeter circlemeter = new CircleMeter(50,90); addObject(circlemeter,20,580); RectangleMeter rectanglemeter = new RectangleMeter(100); addObject(rectanglemeter, 10,500); } /** * act - do whatever a MyWorld is supposed to do. (needs a better comment) */ public void act() { if (Greenfoot.isKeyDown("l")==true) { FileDialog fd = null; fd = new FileDialog (fd , "Open bird file" , FileDialog.LOAD); //fd.setVisable(true); String fname = fd.getDirectory() + fd.getFile(); File birdFile = new File (fname); Scanner birdReader = null; try { birdReader = new Scanner(birdFile); } catch (IOException ioe) { System.out.println("no file found:" + fname); } } } }
danpost danpost

2020/4/10

#
I would suggest using the following format for reading in a file as (1) awt is not supported on the greenfoot site; and (2) it is always best to minimize the amount of time a file is open. Move or copy the text file into your scenario folder, if not there already, and set filename to a simple format like filename.txt.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/** read in text file */
java.util.List<String> lines = new java.util.ArrayList(); // creates a list object for the lines of text read
BufferedReader br = null; // sets up a local field for the BufferedReader object
// attempt to open an input stream to the file given
try
{
    InputStream input = getClass().getClassLoader().getResourceAsStream(filename); // open stream
    br = new BufferedReader(new InputStreamReader(input)); // wrap it within a BufferedReader object
}
catch (Exception e) { System.out.println("Scenario file missing"); return; } // for failure to open stream
// attempt to read in the lines of text
try
{
    String line = null; // sets up a local field for each line of text
    while ((line = br.readLine()) != null)  lines.add(line); // read each line and add them to the 'lines' list
    br.close(); // close the BufferedReader object
}
catch (Exception e) { try { br.close(); } catch (Exception f) {} } // close file if read error occurred
Each line in he lines array (containing the file data) can then be parsed and used individually.
Cats123 Cats123

2020/4/11

#
so i got them to appear on my screen but the movement is a little weird. They just go to points instead of a fluid movement. Any way you could help with that?? bird class import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Bird extends Actor { private int speed; private double xSpeed; private double ySpeed; private double xLoc; private double yLoc; /** * returns the current speed in the x direction of this Bird * @return the current x speed */ public Bird( int speed, double xVel, double yVel) { this.speed = speed; redraw(); xSpeed = xVel; ySpeed = yVel; } public void addedToWorld(World shootBirds) { xLoc = getX(); yLoc = getY(); } public double getHorizontalSpeed() { // this needs fixing !!! return 0; } // private method - so na Javadoc comment. // redraws the image for this bird. private void redraw() { // get an "empty" bird image setImage("bird.png"); GreenfootImage pallette = getImage(); // if bird is moving left, "flip" image so head and tail of bird // appear in proper orientation if (getHorizontalSpeed()<0) { pallette.mirrorHorizontally(); } // if the bird is carrying a viral Load ... if (getInfectionCount() > 0) { // add red viral load count in text to the right of the bird image pallette.setColor(Color.RED); pallette.drawString(""+ getInfectionCount(), 60, 25); } // add life left count to the left of bird image in blue. pallette.setColor(Color.BLUE); pallette.drawString(""+getLifeLeft() , 2, 25); } /** * returns this Bird's current viral load count * @return the viral load count */ public int getInfectionCount() { // you'll want to fix this, as it shouldn't always be 1. return 1; } /** * returns this Bird's amount of life left * @return the amount of life left */ public int getLifeLeft() { // you'll want to fix this, as it shouldn't always be 1. return 1; } /** * Act - do whatever the Bird wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if(getX() <= 0 || getX() >= getWorld().getWidth()-1) xSpeed = -xSpeed; if(getY() <= 0 || getY() >= getWorld().getHeight()-1) ySpeed = -ySpeed; xLoc += xSpeed; yLoc += ySpeed; setLocation( (int) xLoc, (int) yLoc); } } world class import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.FileDialog; import java.io.*; import java.util.Scanner; public class MyWorld extends World { /** * Constructor for objects of class MyWorld. * */ public MyWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(800, 600, 1); prepare(); } public void prepare() { CircleMeter circlemeter = new CircleMeter(50,90); addObject(circlemeter,20,580); RectangleMeter rectanglemeter = new RectangleMeter(100); addObject(rectanglemeter, 10,500); } /** * act - do whatever a MyWorld is supposed to do. (needs a better comment) */ public void act() { if (Greenfoot.isKeyDown("l")) { FileDialog fd = null; fd = new FileDialog (fd , "Open bird file" , FileDialog.LOAD); fd.setVisible(true); String fname = fd.getDirectory() + fd.getFile(); File birdFile = new File (fname); Scanner birdReader = null; try { birdReader = new Scanner(birdFile); } catch (IOException ioe) { System.out.println("no file found:" + fname); return; } while(birdReader.hasNext()) { String type = birdReader.next(); if (type.equalsIgnoreCase("bird")) { int speed = birdReader.nextInt(); int intX = birdReader.nextInt(); int intY = birdReader.nextInt(); Bird bigBird = new Bird(speed, intX, intY); addObject(bigBird, intX, intY); } } } } }
danpost danpost

2020/4/11

#
Cats123 wrote...
so i got them to appear on my screen but the movement is a little weird. They just go to points instead of a fluid movement. << Codes Omitted >>
What exactly do you mean by "go to points"? Describe the little weird, non-fluid movement?
Cats123 Cats123

2020/4/12

#
so when i run it they all go to the location from the file and then to the bottom left corner and the top left corner
danpost danpost

2020/4/12

#
Cats123 wrote...
so when i run it they all go to the location from the file and then to the bottom left corner and the top left corner
Interesting -- because when I ran your Bird class, the birds just casually bounced (normally) off the edges (even out of corners). The image was not mirroring, however.
Cats123 Cats123

2020/4/12

#
i need to have them only go side to side like in a straight like and when the hit the sides they should go the other way
danpost danpost

2020/4/12

#
Cats123 wrote...
i need to have them only go side to side like in a straight like and when the hit the sides they should go the other way
Then the 3rd parameter should arguably be zero: new Bird( ?, ?, 0) // ?'s being non-zero values
Cats123 Cats123

2020/4/12

#
this is what its supposed to be like That is, the word bird (in a case insensitive manner), followed by three integers. The first integer specifies the initial speed of the bird in the x direction (initially, the bird will not move in the y direction), the second specifies the initial x location of the bird, and the third specifies the y location of the bird. A corresponding Bird object should be added to the world. and when i did that they al stayed at the top here is an example file bird 3 400 500 BIRD 1 400 350 Bird -3 400 450
danpost danpost

2020/4/12

#
But in your Bird constructor, the 2nd and 3rd parameters are being set to the horizontal and vertical speeds. Apparently, you do not need xSpeed and ySpeed -- you only need speed, which would only require the 1st parameter. You don't need xLoc and yLoc either. Those values can be gotten using getX() and getY(). In other words your Bird class was way overcomplicated. The following would have done:
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
import greenfoot.*;
 
public class Bird extends Actor
{
    int speed;
    GreenfootImage baseImage;
     
    public Bird(int speed)
    {
        this.speed = speed;
        baseImg = getImage();
    }
     
    public void act()
    {
        move(speed);
        if (getX() == 0 || getX() == getWorld().getWidth()-1) turnAround();
    }
     
    private void turnAround()
    {
        speed = -speed;
        GreenfootImage image = new GreenfootImage(baseImage);
        if (speed < 0) image.mirrorHorizontally();
        setImage(image);
    }
}
I did not add any code for lives or infections as they were not yet implemented.
Cats123 Cats123

2020/4/12

#
thank you!! that worked for the movement but all the birds are staying at the top do you know why that might be??
danpost danpost

2020/4/12

#
Cats123 wrote...
thank you!! that worked for the movement but all the birds are staying at the top do you know why that might be??
Previously:
Cats123 wrote...
i need to have them only go side to side like in a straight like and when the hit the sides they should go the other way
This is what you said and what my code does. So, now you say, that is not enough. Do you want them to start at different heights and only move horizontally?
Cats123 Cats123

2020/4/12

#
start at different heights on the screen but still move side to side sorry for the confusion!!!
Cats123 Cats123

2020/4/12

#
bird 3 400 500 BIRD 1 400 350 Bird -3 400 450 like this is the example file the two integers at the end are the initial x and y location
danpost danpost

2020/4/12

#
Cats123 wrote...
bird 3 400 500 BIRD 1 400 350 Bird -3 400 450 like this is the example file the two integers at the end are the initial x and y location
Please show your adjusted MyWorld class codes.
There are more replies on the next page.
1
2
3
4