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

2013/5/26

Opening windows

1
2
allekalle allekalle

2013/5/26

#
Hi! Is there a way in Greenfoot to open new windows? I intend to make a game with two players. The players have to take turns to use the computer. Each player has a control window for launching strikes in a main window where a battle takes place. Thanks! Karl
danpost danpost

2013/5/26

#
You can create a ControlPanel class whose instances will contain the objects used to control the battle. Then in your world constructor create and add two instances of that object into the world, one for each player. If everything is mouse-controlled in the control panels, it would make things easier (mouse actions are Actor-related, where keyboard input is not; there are ways to programmatically give focus to one at a time, however). The controls for the ControlPanel class should be created within the 'addedToWorld' method of the ControlPanel class. By creating the controls for the panel there, you only have to code it once instead of having to code the creation and addition of all the controls for both panels individually.
allekalle allekalle

2013/5/27

#
Hi & thanks! That sounds hopeful. However, I am not sure I know how to implement it, and I'm not sure if you understood what I wanted. I want to have three separate windows open at once. The two control windows and the battle window in the middle. The control windows should be outside the battle window as independently movable windows. I know it must be possible to create an actor area to act as a control panel inside the world when using only a single window, but I thought it would be neat to use separate windows. I saw somebody make an extra window somewhere on this forum but there was no code attached. (I asked a question there but no answer yet). Then I saw some comments about JFrame and JPanel, but no instructions on how to open new windows. Thanks!
danpost danpost

2013/5/27

#
The ControlPanel objects would act as separate windows. They can be made draggable. They will be able to keep its control objects in proper alignment with the window that it is made out to be. We just need to keep an 2d array of offsets from the center of the control panel for each control. The basics for the ControlPanel object would be as follows:
import greenfoot.*;

public class ControlPanel extends Actor
{
    private Actor[] controls = { new Control1(),
                                 new Control2(),
                                 /*  etc  */ };
    private int[][] locs = new int[controls.length][2];
    private boolean beingDragged;
    private int offCenterX, offCenterY; // used when being dragged

    public ControlPanel()
    {
        /* build window image */
        /* fill 'locs' array with offsets from center of control panel for each control */
        /* (as alternative, filling 'locs' could be done when it was declared) */
    }

    // adds the controls to this control panel object
    public void addedToWorld(World world)
    {
        for (int i=0; i<controls.length; i++) getWorld().addObject(controls[i], getX()+locs[i][0], getY());+locs[i][1]);
    }


    // keeps control on control panel object
    private void setControls()
    {
        for (int i=0; i<controls.length; i++) controls[i].setLocation(getX()+locs[i][0], getY()+locs[i][1]);
    }

    // control dragging of control panel object
    public void act()
    {
        if (!beingDragged && Greenfoot.mousePressed(this))
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            offCenterX = mouse.getX()-getX();
            offCenterY = mouse.getY()-getY();
            beingDragged = true;
        }
        if (beingDragged && Greenfoot.mouseMoved(null))
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            setLocation(mouse.getX()-offCenterX, mouse.getY()-offCenterY);
            setContols();
        }
        if (beingDragged && Greenfoot.mouseClicked(null)) beingDragged = false;
    }

    // the following are inner classes for the controls themselves
    public class Switch extends Actor
    {
        /* class code for switch  */
    }

    public class Button extends Actor
    {
        /*  class code for button  */
    }

    public class Slider extends Actor
    {
        /*  class code for slider  */
    }

    /*  any other controls you may want to add  */
}
danpost danpost

2013/5/27

#
Do not be thrown by 'new Control1()' and 'new Control2()' on lines 5 and 6. They are pseudo constructor calls, and should be replaced with actual constructor calls to classes you have declared.
danpost danpost

2013/5/27

#
Correction: on line 42 'mouseMoved(null)' should be replaced with 'mouseDragged(this)'.
danpost danpost

2013/5/27

#
Inserting the following code snippet at line 40 will, when mouse button is pressed while mouse is on a panel, make that panel draw on top of any others.
World world = getWorld();
world.removeObject(this);
world.addObject(this, mouse.getX()-offCenterX, mouse.getY()-offCenterY);
for (int i=0; i<controls.length; i++)
{
    world.removeObject(controls[i]);
    world.addObject(controls[i], 0, 0);
}
setControls();
danpost danpost

2013/5/27

#
Inserting the following code snippet at line 46 will, when panel is being dragged, not allow any part of the panel go outside the edges of the world:
int w = getImage().getWidth(), h = getImage().getHeight();
if (getX()<w/2) setLocation(w/2, getY());
if (getY()<h/2) setLocation(getX(), h/2);
int ww = getWorld().getWidth(), wh = getWorld().getHeight();
if (getX()>ww-w/2) setLocation(ww-w/2, getY());
if (getY()>wh-h/2) setLocation(getX(), wh-h/2);
allekalle allekalle

2013/5/28

#
Hi & thanks for all the code! I think it went a bit over my head again, though. When I after a while managed to compile it without errors (I made two Button classes for Control1 and Control2), nothing happened (no window was created). (I didn't try the last two additions). I suppose I have to write something in the constructor of the ControlPanel? Cheers, Karl
allekalle allekalle

2013/5/28

#
This one just got stackoverflow error (got the code from youtube, but it´s not for Greenfoot): import greenfoot.*; import javax.swing.JFrame; public class Window extends JFrame { public Window() { Window gui = new Window(); gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); gui.setSize(200,200); gui.setVisible(true); gui.setTitle("Test Window"); } }
allekalle allekalle

2013/5/28

#
I got the above code working in netbeans, though. (But I'm a bit stuck after that, would prefer Greenfoot).
davmac davmac

2013/5/28

#
You shouldn't really mix Swing (JFrame et al) with Greenfoot. It can be done theoretically, but doing it right in practice is very tricky - even if it "seems to" work there might be glitches due to threading issues.
allekalle allekalle

2013/5/29

#
I cannot find the code in danpost's reply that opens a window...(?)
danpost danpost

2013/5/29

#
A 'window' object will be created when you create a new ControlPanel object. You asked for an independently movable window, and I wrote a blueprint to create one. You can create the two control windows you need from this class. I can upload a sample scenario to show you how it would work (I will not at this point include the code as I am still working on creating some of the controls and functionality; basically, it is still a work in progress). You could then decide if it is something you could use or not.
danpost danpost

2013/5/29

#
You can find the sample in this scenario. Let me know if it might be something you could use or not.
There are more replies on the next page.
1
2