Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

AWT Fullscreen SlideShow

Posted on 2004-04-06
5
Medium Priority
?
806 Views
Last Modified: 2013-12-29
I'm attempting to create a basic slide show using awt.
So far I've managed to throw togeather a full screen buffer but now I'm having trouble adding an image.

Here's what I've got....


import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferStrategy;

public class ImageView implements DisplayModes //the interface contains an array of display modes
{
    private static Color BLACK = Color.black;
    private static Frame mainFrame;
    private        Image image = Toolkit.getDefaultToolkit().getImage("C:\\pictures\\pic1.JPG");

    public ImageView(GraphicsDevice device)
    {
        GraphicsConfiguration gc = device.getDefaultConfiguration();
        mainFrame = new Frame(gc);

        mainFrame.setLayout(new GridLayout(5,5));
        mainFrame.setUndecorated(true);
        mainFrame.setIgnoreRepaint(true);
        device.setFullScreenWindow(mainFrame);

        addComponents();


        if (device.isDisplayChangeSupported())
        {
            chooseBestDisplayMode(device);
        }

        Rectangle bounds = mainFrame.getBounds();
        mainFrame.createBufferStrategy(1);
        BufferStrategy bufferStrategy = mainFrame.getBufferStrategy();

        Graphics g = bufferStrategy.getDrawGraphics();
                 g.setColor(BLACK);
                 g.fillRect(0,0,bounds.width, bounds.height);

        bufferStrategy.show();
    }

    private static DisplayMode getBestDisplayMode(GraphicsDevice device)
    {
        for (int x = 0; x < BEST_DISPLAY_MODES.length; x++)
        {
            DisplayMode[] modes = device.getDisplayModes();
            for (int i = 0; i < modes.length; i++)
            {
                if (modes[i].getWidth()    == BEST_DISPLAY_MODES[x].getWidth()  &&
                    modes[i].getHeight()   == BEST_DISPLAY_MODES[x].getHeight() &&
                    modes[i].getBitDepth() == BEST_DISPLAY_MODES[x].getBitDepth())
                {
                    return BEST_DISPLAY_MODES[x];
                }
            }
        }
        return null;
    }

    public static void chooseBestDisplayMode(GraphicsDevice device)
    {
        DisplayMode best = getBestDisplayMode(device);
        if (best != null)
        {
            device.setDisplayMode(best);
        }
    }

    private void addComponents()
    {
        Button exit = new Button("Exit");

        exit.setBounds(0,0,50,50);

        exit.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }
        });

        mainFrame.add(exit);
    }

    public static void main(String[] args)
    {
        GraphicsEnvironment env    = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice      device = env.getDefaultScreenDevice();
        ImageView           test   = new ImageView(device);
        Graphics            graph  = mainFrame.getBufferStrategy().getDrawGraphics();
    }
}

I've tried to add an image using Graphics.drawImage(), however I don't understand how to implement ImageObserver and I'm not even sure if that's the best way to do it.
I still have to write a thread to show each image for however long but I want to take it one step at a time.

Any help, suggestions, tips etc would be greatly appriciated.

Cheers

-OBCT
0
Comment
Question by:OBCT
  • 3
5 Comments
 
LVL 13

Expert Comment

by:Webstorm
ID: 10768570
Hi OBCT,

When you call the drawImage method, you can set the ImageObserver parameter to null : g.drawImage(image,x,y,null);

0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 10769128
the java.awt.Frame class implements ImageObserver:
g.drawImage(image,x,y,mainFrame);
0
 
LVL 30

Accepted Solution

by:
GrandSchtroumpf earned 2000 total points
ID: 10769410
here is a working version of your code.
i modified a few things here and there, to make it easier for me:


import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferStrategy;

interface DisplayModes {
  DisplayMode[] BEST_DISPLAY_MODES = {
    new DisplayMode(1280, 1024, 32, 85),
    new DisplayMode(1152, 864, 32, 85),
    new DisplayMode(1024, 768, 32, 85),
    new DisplayMode(800, 600, 32, 85),
  };
}

public class ImageView extends Panel implements DisplayModes, ActionListener
{
    private static Color BLACK = Color.black;

    private Frame mainFrame;
    private Image image;
    private Button exit = new Button("Exit");

    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();
        if (source == exit)
        {
          System.exit(0);
        }
    }

    public ImageView(GraphicsDevice device)
    {
        super();

        image = this.getToolkit().createImage("starwars.jpg");
        MediaTracker tracker = new MediaTracker(this);
        tracker.addImage(image, 0);
        try {
            tracker.waitForID(0);
            if (tracker.isErrorAny()) {
                System.err.println("Error loading image");
            }
        } catch (Exception ex) { ex.printStackTrace();
        }

        GraphicsConfiguration gc = device.getDefaultConfiguration();
        mainFrame = new Frame(gc);

        mainFrame.setLayout(new BorderLayout());
        mainFrame.add(exit, BorderLayout.NORTH);
        mainFrame.add(this, BorderLayout.CENTER);

        mainFrame.setUndecorated(true);
        device.setFullScreenWindow(mainFrame);

        exit.addActionListener(this);

        if (device.isDisplayChangeSupported())
        {
            chooseBestDisplayMode(device);
        }
    }

    private static DisplayMode getBestDisplayMode(GraphicsDevice device)
    {
        for (int x = 0; x < BEST_DISPLAY_MODES.length; x++)
        {
            DisplayMode[] modes = device.getDisplayModes();
            for (int i = 0; i < modes.length; i++)
            {
                if (modes[i].getWidth()    == BEST_DISPLAY_MODES[x].getWidth()  &&
                    modes[i].getHeight()   == BEST_DISPLAY_MODES[x].getHeight() &&
                    modes[i].getBitDepth() == BEST_DISPLAY_MODES[x].getBitDepth())
                {
                    return BEST_DISPLAY_MODES[x];
                }
            }
        }
        return null;
    }

    public void paint(Graphics g) {
        g.drawImage(image,10,10,mainFrame);
    }

    public static void chooseBestDisplayMode(GraphicsDevice device)
    {
        DisplayMode best = getBestDisplayMode(device);
        if (best != null)
        {
            device.setDisplayMode(best);
        }
    }

    public static void main(String[] args)
    {
        GraphicsEnvironment env    = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice      device = env.getDefaultScreenDevice();
        ImageView           test   = new ImageView(device);
    }
}
0
 
LVL 9

Author Comment

by:OBCT
ID: 10771085
Thanks GrandSchtroumpf!!!!
Your always a good help. :)

Cheers

-OBCT
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 10772589
:°)
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question