Problem with double buffering

D

David

Hello I'm writting an apllication and i like to display and offscreen image.
However my code doesn't seem to work.
It compiles and runs properly but What i want is to associate the button of
the menu to switch an image (actually to add rectangels offscreen and bring
it to the front).
The following codes runs properly. You just need to create a file
images.properties and put some images in a folder called images.

By the way this is related to metabolic pathways is there is any biologist
outside that like to help.

thanks


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.ButtonGroup;
import javax.swing.JMenuBar;
import java.util.*;

//PathwayViewer itself is not a visible component.
public class PathwayViewer extends JComponent implements ItemListener,
ListSelectionListener {
private Vector imageNames; //Vector to call pictures
private JLabel picture;
private JList list; //List to call filenames
private JSplitPane splitPane; //A Splitpane


private String newline = "\n";


//Double buffer (for flipping images)
private Graphics offscreenGraphics; //A graphics object for the buffer
private Image offscreenImage = null; //The off screen image for double
buffering
private Dimension d = getSize();
private int w = d.width;
private int h = d.height;

Thread main;

Image imageBackground;


//Menu definitions
private JMenuBar menuBar;
private JMenu Genes,About;
private JMenuItem menuItem;
private JRadioButtonMenuItem rbMenuItem;
private JCheckBoxMenuItem cbMenuItem;

public PathwayViewer() {
ImageIcon imageBackground = createImageIcon("images/hsa00010.gif");



// Initialise double buffering
/// repaint();


//Read image names from a properties file.
ResourceBundle imageResource;
try {
imageResource = ResourceBundle.getBundle("imagenames");
String imageNamesString = imageResource.getString("images");
imageNames = parseList(imageNamesString);
} catch (MissingResourceException e) {
handleMissingResource(e);
}

//
//Create the list of images and put it in a scroll pane.
//
list = new JList(imageNames);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setSelectedIndex(0);
list.addListSelectionListener(this);
JScrollPane listScrollPane = new JScrollPane(list);

//
//Set up the picture label and put it in a scroll pane.
//
ImageIcon firstImage = createImageIcon("images/" +
(String)imageNames.firstElement());
if (firstImage != null) {
picture = new JLabel(firstImage);
} else {
picture = new JLabel((String)imageNames.firstElement());
}
JScrollPane pictureScrollPane = new JScrollPane(picture);

//
//Create a split pane with the two scroll panes in it.
//
splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
listScrollPane, pictureScrollPane);
splitPane.setOneTouchExpandable(true);
splitPane.setDividerLocation(150);

//
//Provide minimum sizes for the two components in the split pane.
//
Dimension minimumSize = new Dimension(100, 50);
listScrollPane.setMinimumSize(minimumSize);
pictureScrollPane.setMinimumSize(minimumSize);

//
//Provide a preferred size for the split pane.
//
splitPane.setPreferredSize(new Dimension(900, 600));
}

//Used by PathwayViewer2
//public JList getImageList() {
// return list;
//}

public JSplitPane getSplitPane() {
return splitPane;
}

public void valueChanged(ListSelectionEvent e) {

if (e.getValueIsAdjusting())
return;


//
// Read file with image filenames
//
JList theList = (JList)e.getSource();
if (theList.isSelectionEmpty()) {
picture.setIcon(null);
picture.setText(null);
} else {
int index = theList.getSelectedIndex();
ImageIcon newImage = createImageIcon("images/" +
(String)imageNames.elementAt(index));
picture.setIcon(newImage);
if (newImage != null) {
picture.setText(null);

} else {
picture.setText("Image not found: "
+ (String)imageNames.elementAt(index));
}
}
}

//
// Store image filenames in vector
//
protected static Vector parseList(String theStringList) {
Vector v = new Vector(10);
StringTokenizer tokenizer = new StringTokenizer(theStringList, " ");
while (tokenizer.hasMoreTokens()) {
String image = tokenizer.nextToken();
v.addElement(image);
}
return v;
}

/*
// Called when the image property file can't be found.
*/
private void handleMissingResource(MissingResourceException e) {
System.err.println();
System.err.println("Can't find the properties file " +
"that contains the image names.");
System.err.println("Its name should be imagenames.properties, " +
"and it should");
System.err.println("contain a single line that specifies " +
"one or more image");
System.err.println("files to be found in a directory " +
"named images. Example:");
System.err.println();
System.err.println(" images=Bird.gif Cat.gif Dog.gif");
System.err.println();
throw(e); //Used to be exit(1), but that causes the console to
//go away under Java Web Start; this way, you're
//more likely to see a relevant error message.
}

/*
* Returns an ImageIcon, or null if the path was invalid.
*/
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = PathwayViewer.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}


// Called to redraw image offscreen
public void update(Graphics g){
paint(g);
}

// Called to redraw image offscreen
public void paint(Graphics g) {

Rectangle box = g.getClipRect();
//create the offscreenImage buffer
if (offscreenImage == null)
{
System.out.println("Creating double buffering");
// set up double buffer
offscreenImage = createImage( w, h);
offscreenGraphics = offscreenImage.getGraphics();
}

offscreenGraphics.drawImage(imageBackground,0,0,this);
offscreenGraphics.setColor(getBackground());
// clear the exposed area
offscreenGraphics.setColor(getBackground());
offscreenGraphics.fillRect(0, 0, box.width, box.height);
offscreenGraphics.setColor(getForeground());
// do normal redraw

paint(offscreenGraphics);
// Copy offscreen buffer to screen
g.drawImage(offscreenImage ,10 ,10, this );


//offscreenGraphics.setColor(getBackground());
//offscreenGraphics.setColor(getForeground());

}


/**
* Create the GUI and show it
*/
private static void createAndShowGUI() {

JMenuBar menuBar;
JMenu Genes,About;
JMenuItem menuItem;
JRadioButtonMenuItem rbMenuItem;
JCheckBoxMenuItem cbMenuItem;

//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);

JFrame frame = new JFrame("PathwayViewer 1.0");

//Implements Listener
PathwayViewer listener = new PathwayViewer();

//Create the menu bar
menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);

//Menu Genes
Genes = new JMenu("Genes");
Genes.setMnemonic(KeyEvent.VK_G);
menuBar.add(Genes);

//Menu About
About = new JMenu("About");
About.setMnemonic(KeyEvent.VK_A);
menuBar.add(About);

//Buttons in Genes
ButtonGroup group = new ButtonGroup();

//Button UP regulated
rbMenuItem = new JRadioButtonMenuItem("Show UP regulated",new
ImageIcon("images/up.gif"));
rbMenuItem.setSelected(false);
rbMenuItem.setMnemonic(KeyEvent.VK_U);
group.add(rbMenuItem);
Genes.add(rbMenuItem);

rbMenuItem.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED) {
System.out.println("UP regulated");


}
}
});

//Button Down regulated
rbMenuItem = new JRadioButtonMenuItem("Show DOWN regulated");
rbMenuItem.setSelected(false);
rbMenuItem.setMnemonic(KeyEvent.VK_D);
group.add(rbMenuItem);
rbMenuItem.addItemListener(listener);
rbMenuItem.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED) {
System.out.println("DOWN regulated");
}
}
});Genes.add(rbMenuItem);


//Button UP AND DOWN regulated
rbMenuItem = new JRadioButtonMenuItem("Show UP and DOWN regulated");
rbMenuItem.setSelected(true);
rbMenuItem.setMnemonic(KeyEvent.VK_A);
group.add(rbMenuItem);
rbMenuItem.addItemListener(listener);
Genes.add(rbMenuItem);
rbMenuItem.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED) {
System.out.println("BOTH regulated");
//Graphics g = getGraphics();
//update(g);
}
}
});

//Display the window.
frame.pack();
frame.setVisible(true);


//Create and set up the window.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
PathwayViewer pathway = new PathwayViewer();
frame.getContentPane().add(pathway.getSplitPane());

//Display the window.
frame.pack();
frame.setVisible(true);
}


public void itemStateChanged(ItemEvent e) {} //override method


// Returns just the class name -- no package info.
protected String getClassName(Object o) {
String classString = o.getClass().getName();
int dotIndex = classString.lastIndexOf(".");
return classString.substring(dotIndex+1);
}

public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
PathwayViewer path = new PathwayViewer();
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {

createAndShowGUI();
// //Image imageBackground;

}
});
}
}
 
A

Alex Hunsley

David said:
Hello I'm writting an apllication and i like to display and offscreen image.
However my code doesn't seem to work.
It compiles and runs properly but What i want is to associate the button of
the menu to switch an image (actually to add rectangels offscreen and bring
it to the front).

I've got your code to work but you really need to be more clear what
you're looking for.
The button of the menu? What button? The radio button items?
You want them to switch the image when pressed? And what exactly do you
mean by "actually to add rectangels offscreen and bring it to the front"?
Programming is a precise business and you need to be precise about what
you're trying to do, then I can help more.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top