473,390 Members | 1,449 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 software developers and data experts.

Problem with double buffering

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_SE LECTION);
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,th is);
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_CLOS E);
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;

}
});
}
}
Jul 17 '05 #1
1 4434
David wrote:
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.

Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Abhishek | last post by:
My objective is quite simple. I want to display a JPG image and then on it overlay a polygon. This polygon can be moved by a user. The even is trapped on the mouse down event of the control. I can...
3
by: Alex Glass | last post by:
I have read plenty about applying double buffering for animation and self drawn forms. Is there a way to apply it to a form with many standard controls on it (textboxes, labels etc) ?? I have...
2
by: MPowell | last post by:
Gents/Ladies, I'm doing (at least plan on ) lots of Reads and Writes across a communication channel. I'm told that for the 'receive side' it'd be prudent to implement a double buffering scheme to...
2
by: Dan Neely | last post by:
My dialog has groupboxes with slow to redraw controls, to improve the appearance I want to doublebuffer it. While I can use SetStyle() in the Dailogs constructor the setting change doesn't get...
0
by: Brian Henry | last post by:
I am trying to do a owner drawn list view in detail mode, when i inherited the list view into a new custom control then turned on double buffering all the sudden the selection rectangle is the...
1
by: TyBreaker | last post by:
I notice in VB 2005 that I can set a form to be double-buffered just by setting that option to true in the Form properties. I have a panel on that form but there doesn't appear to be a...
3
by: ssoffline | last post by:
hi i have an app in which i can drop objects onto a form and move them, it consists of graphics (lines), i am using double buffering to avoid filckering in the parent control which is a panel,but...
4
by: black(flashing vampire) | last post by:
Hi all, i'm trying to make my first windows tetris game but found a severe problem. that is, everytime i update(redraw) the window it may be filled with shaking lines. i checked some articles...
8
by: Olie | last post by:
I have a window drawing problem that I hope someone may be able to help with. I basically have a form and a control in the form which is docked to fill. I want to resize the form and get the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.