OBCT
asked on
AWT Fullscreen SlideShow
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.BufferStrat egy;
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:\\pict ures\\pic1 .JPG");
public ImageView(GraphicsDevice device)
{
GraphicsConfiguration gc = device.getDefaultConfigura tion();
mainFrame = new Frame(gc);
mainFrame.setLayout(new GridLayout(5,5));
mainFrame.setUndecorated(t rue);
mainFrame.setIgnoreRepaint (true);
device.setFullScreenWindow (mainFrame );
addComponents();
if (device.isDisplayChangeSup ported())
{
chooseBestDisplayMode(devi ce);
}
Rectangle bounds = mainFrame.getBounds();
mainFrame.createBufferStra tegy(1);
BufferStrategy bufferStrategy = mainFrame.getBufferStrateg y();
Graphics g = bufferStrategy.getDrawGrap hics();
g.setColor(BLACK);
g.fillRect(0,0,bounds.widt h, bounds.height);
bufferStrategy.show();
}
private static DisplayMode getBestDisplayMode(Graphic sDevice 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].getW idth() &&
modes[i].getHeight() == BEST_DISPLAY_MODES[x].getH eight() &&
modes[i].getBitDepth() == BEST_DISPLAY_MODES[x].getB itDepth())
{
return BEST_DISPLAY_MODES[x];
}
}
}
return null;
}
public static void chooseBestDisplayMode(Grap hicsDevice 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(ActionEven t e)
{
System.exit(0);
}
});
mainFrame.add(exit);
}
public static void main(String[] args)
{
GraphicsEnvironment env = GraphicsEnvironment.getLoc alGraphics Environmen t();
GraphicsDevice device = env.getDefaultScreenDevice ();
ImageView test = new ImageView(device);
Graphics graph = mainFrame.getBufferStrateg y().getDra wGraphics( );
}
}
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
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.BufferStrat
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(
public ImageView(GraphicsDevice device)
{
GraphicsConfiguration gc = device.getDefaultConfigura
mainFrame = new Frame(gc);
mainFrame.setLayout(new GridLayout(5,5));
mainFrame.setUndecorated(t
mainFrame.setIgnoreRepaint
device.setFullScreenWindow
addComponents();
if (device.isDisplayChangeSup
{
chooseBestDisplayMode(devi
}
Rectangle bounds = mainFrame.getBounds();
mainFrame.createBufferStra
BufferStrategy bufferStrategy = mainFrame.getBufferStrateg
Graphics g = bufferStrategy.getDrawGrap
g.setColor(BLACK);
g.fillRect(0,0,bounds.widt
bufferStrategy.show();
}
private static DisplayMode getBestDisplayMode(Graphic
{
for (int x = 0; x < BEST_DISPLAY_MODES.length;
{
DisplayMode[] modes = device.getDisplayModes();
for (int i = 0; i < modes.length; i++)
{
if (modes[i].getWidth() == BEST_DISPLAY_MODES[x].getW
modes[i].getHeight() == BEST_DISPLAY_MODES[x].getH
modes[i].getBitDepth() == BEST_DISPLAY_MODES[x].getB
{
return BEST_DISPLAY_MODES[x];
}
}
}
return null;
}
public static void chooseBestDisplayMode(Grap
{
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
{
public void actionPerformed(ActionEven
{
System.exit(0);
}
});
mainFrame.add(exit);
}
public static void main(String[] args)
{
GraphicsEnvironment env = GraphicsEnvironment.getLoc
GraphicsDevice device = env.getDefaultScreenDevice
ImageView test = new ImageView(device);
Graphics graph = mainFrame.getBufferStrateg
}
}
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
the java.awt.Frame class implements ImageObserver:
g.drawImage(image,x,y,main Frame);
g.drawImage(image,x,y,main
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks GrandSchtroumpf!!!!
Your always a good help. :)
Cheers
-OBCT
Your always a good help. :)
Cheers
-OBCT
:°)
When you call the drawImage method, you can set the ImageObserver parameter to null : g.drawImage(image,x,y,null