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

2013/7/22

Fullscreen mode for Greenfoot

1
2
3
Gevater_Tod4711 Gevater_Tod4711

2013/7/22

#
Hi all, I know there were already discussions about a fullscreen mode for greenfoot but I think a fullscreen mode would be realy great for creating good games with greenfoot. So I tried a view things and came to a result: I think the easyest way for good fullscreen games would be to create a fullscreen frame (not the greenfoot frame), then take the image from the greenfoot panel (the world), scale it the the right screensize using Toolkit and print the image onto the fullscreen frame. After some search I found the a java tutorial for the fullscreen exclusive mode which would be the best way for doing the fullscreen frame I think. Also I used an undecorated frame so the frame itselves covers the whole screen just like in real computergames. Now I just got the problem that for some reason I can't get the image from the world and copy it onto the fullscreen frame. But maybe the greenfoot team can introduce this in one of the next greenfoot versions. That would be realy great. If anyone knows what I am doing wrong with my try for fullscreening please tell me so I can try to fix it. Here's the code I'm using: The world class:
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import greenfoot.core.WorldHandler;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
 
/**
 * Write a description of class FullScreenWorld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class FullScreenWorld extends World
{
    private JPanel panel;
     
    private FullScreenWindow fullScreenWindow = new FullScreenWindow();
     
    public FullScreenWorld() {
        super(600, 400, 1);
        panel = WorldHandler.getInstance().getWorldCanvas();
        Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        //panel.getParent().getParent().getParent().getParent().getParent().getParent().getParent().getParent().getParent().setVisible(false);
    }
     
    public void act() {
        Graphics2D graphics = (Graphics2D) panel.getGraphics();
        Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        graphics.scale(screenSize.getWidth()-4, screenSize.getHeight()-10);
        //fullScreenWindow.getContentPane().print(graphics);
    }
}
And the fullscreen frame:
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
import java.awt.BorderLayout;
 
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
 
 
public class FullScreenWindow extends JFrame {
     
    private static final long serialVersionUID = 1L;
     
    private JPanel contentPane;
     
    public FullScreenWindow() {
        setUndecorated(true);
        //setAlwaysOnTop(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
        if (getGraphicsConfiguration().getDevice().isFullScreenSupported()) {
            getGraphicsConfiguration().getDevice().setFullScreenWindow(this);
        }
    }
     
    public JPanel getContentPane() {
        return contentPane;
    }
}
bourne bourne

2013/7/22

#
I used WorldHandler with my Color Palette class to obtain the Color of any pixel found in the World (with Actors drawn). I used WorldHandler.getInstance().getSnapShot() to obtain a BufferedImage
darkmist255 darkmist255

2013/7/22

#
Mmm, interesting topic. I'm eager to see how well this works, it could be very useful for dealing with different monitor sizes.
Gevater_Tod4711 Gevater_Tod4711

2013/7/23

#
@bourne I also tried to use WorldHandler but I didn't try getSnspShot(). Is this a method in the WorldHandler class? Because I can't find it in any api. However I'll try to use this. Thanks for the advice.
Gevater_Tod4711 Gevater_Tod4711

2013/7/23

#
@bourne Well that also doesn't seem to work. I think the problem is that I can't get the Graphics or BufferedImage object from the world panel. The object I get seems to be empty and so nothing is drawn onto the FullScreenPanel. Has anyone an idea how I can get the Graphics from the world panel?
davmac davmac

2013/7/23

#
Has anyone an idea how I can get the Graphics from the world panel?
Graphics objects are used for drawing; they are not images. Do you really want the Graphics for the world panel? That will let you draw on the world panel, but I don't think that's what you want. If you want a BufferedImage that corresponds to what the world looks like, you could use "WorldHandler.getInstance().getSnapShot()" as suggested by Bourne above, bearing in mind that it's not an official part of the API (and so it might not work in future versions etc).
Gevater_Tod4711 Gevater_Tod4711

2013/7/23

#
Oh ok then I need to use a BufferedImage. But I also tried to use WorldHandler.getInstance().getSnapShot(). It also seems to return an empty BufferedImage.
davmac davmac

2013/7/23

#
It shouldn't do that; why do you think that the BufferedImage is empty?
Gevater_Tod4711 Gevater_Tod4711

2013/7/23

#
I'm sorry I mixed up something. I still was using the Graphics which was the empty object. Now it seams to work. Thanks for the advices.
MatheMagician MatheMagician

2013/7/24

#
I have actually posted a scenario for using full-screen mode in greenfoot here. I use
1
(int)Toolkit.getDefaultToolkit().getScreenSize().getWidth()
and my version allows for scaling of the actors as well.
Kartoffelbrot Kartoffelbrot

2013/7/24

#
Where did you all get to know these classes?
Gevater_Tod4711 Gevater_Tod4711

2013/7/26

#
Well I think I know most of them through references in the APIs or from examples. You need some of this classes when you start programming little applications without greenfoot and the better your programms get the more classes you will have to know.
Kartoffelbrot Kartoffelbrot

2013/7/26

#
like in Greenfoot :)
MatheMagician MatheMagician

2013/7/27

#
I learn pretty much everything from stackoverfow:)
Gevater_Tod4711 Gevater_Tod4711

2013/7/27

#
Hi all, I now got another problem concerning the fullscreen mode. The scaling and drawing of the greenfoot world now works fine but if I use scenario speeds greater than 50 there are so many imaes per second that my computer just doesn't seem to be able to draw the current image until the next image comes up. To fix this problem I used multibuffering. If I use a scenario speed of 50 I use 5 buffers (JPanels where I draw the images onto) and everything works fine. But if I want to use a scenario speed of about 75 I need nearly 200 buffers and I think that can't be right. If I want a scenario speed of 100 I would have to use that many buffers that I get a OutOfMemoryError. But the greenfoot world is also drawed with high speeds and so it must be possible. I just don't know how. Has anyone got an idea how to draw that many images in this short time? My current code for the multy buffering is this:
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
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
 
 
public class FullScreenWindow extends JFrame {
     
    private static final long serialVersionUID = 1L;
     
    private final int bufferPanelNumber;
     
    private int bufferNumber;
     
    private FullScreenBufferPanel[] bufferPanels;
     
    public FullScreenWindow() {
        this(5);
    }
     
    public FullScreenWindow(int buffer) {
        bufferPanels = new FullScreenBufferPanel[buffer];
        bufferNumber = 0;
        bufferPanelNumber = buffer;
        setUndecorated(true);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        for (int i = 0; i < bufferPanelNumber; i++) {
            bufferPanels[i] = new FullScreenBufferPanel(i);
            bufferPanels[i].setBorder(new EmptyBorder(5, 5, 5, 5));
            bufferPanels[i].setLayout(new BorderLayout(0, 0));
        }
        setContentPane(bufferPanels[0]);
        if (getGraphicsConfiguration().getDevice().isFullScreenSupported()) {
            getGraphicsConfiguration().getDevice().setFullScreenWindow(this);
        }
    }
     
    public void setImage(BufferedImage img) {
        int prebuffer = bufferNumber;
        bufferNumber++;
        if (bufferNumber >= bufferPanelNumber) {
            bufferNumber = 0;
        }
        setContentPane(bufferPanels[bufferNumber]);
        bufferPanels[prebuffer].setImage(img);
        bufferPanels[prebuffer].repaint();
    }
}
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.image.BufferedImage;
import java.awt.Graphics;
import javax.swing.JPanel;
 
/**
 * Write a description of class FullScreenBufferPane here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class FullScreenBufferPanel extends JPanel
{
     
    private BufferedImage contentImage;
     
    private int bufferPanelNumber;
     
    public FullScreenBufferPanel(int bufferPanelNumber) {
        this.bufferPanelNumber = bufferPanelNumber;
    }
     
    public void setImage(BufferedImage img) {
        this.contentImage = img;
    }
     
    public void paint(Graphics g) {
        super.paint(g);
        g.drawImage(contentImage, 0, 0, null);
    }
}
I also tried to run all the buffers in different threads but that also didn't help. Any advices how to fix this problem would be great.
There are more replies on the next page.
1
2
3