button that calls repaint() doesn't stop calling it.
Greetings!
I've created an applet that displays a sequence of images depending on what button is pressed.
The paint() method displays the necessary images as desired. Currently, I have a button that, when pressed, calls the paint() method using repaint(). However, when the button is pressed the paint() method is run again and again.
The portion of the code I'm speaking is as follows:
// e is the ActionEvent, this is in the method ActionPerformed()
if (e.getSource() == SCAN){
text.append("Scanning\n");
repaint();
}
Here's the link to the applet (works in IE only):
http://ase.tufts.edu/chemistry/sykes/Applets/STMapplet/STMapplet.html
The whole code, including the paint method is below:
import java.awt.event.*;
import java.awt.*;
import java.net.URL;
import javax.swing.*;
/** 7.19.07
* To Be Done:
* - Make firefox compatible
* - Might have to modify the FRMRATE timing
*
*
* @author Darin Bellisario
*
*
*/
public class STMapplet extends JApplet implements ActionListener{
/**
* PUBLIC OBJECTS AND VARIABLES:
*/
//Creat Public Text Area object with 10 rows, 10 columns. This is the UI text output window.
JTextArea text = new JTextArea(10,10);
//Make image public object and set location in relation to (0,0) being in northwest corner.
Image picture;
int imageX = 175;
int imageY = 50;
//frame specs, # of frames, time between frames (ms)
int NUM_FRMS = 65;
int MAX_FRMS = 65;
long FRMRATE = 130;
//Declare public scanning condition variables. All values are false by default. Changed by button actions.
boolean highV = false;
boolean lowV = false;
boolean highI = false;
boolean lowI = false;
// Make Public image URLS, will be established later during paint method
// Use the same URLs for LILV and HIHV
URL hVhI;
URL hVlI;
URL lVhI;
//Border URLs and image objects
URL IMGBorderTop;
URL IMGBorderLeft;
URL IMGBorderRight;
URL IMGBorderBottom;
Image BORDERTOP;
Image BORDERLEFT;
Image BORDERRIGHT;
Image BORDERBOTTOM;
//Declare Public Buttons, set colors
JButton highVButton;
//highVButton.setForeground(color.Black);
JButton lowVButton;
JButton highIButton;
JButton lowIButton;
JButton SCAN;
/**
* constraint construction for button panel layout
*
* addGB adds a component to the panel that has a grid bag layout
*/
GridBagConstraints buttonConstraints = new GridBagConstraints();
void addGBB(JPanel panel, Component component, int x, int y){
buttonConstraints.gridx =x;
buttonConstraints.gridy = y;
panel.add(component, buttonConstraints);
}
/**
* constraint construction for left display panel layout
*
* addGB adds a component to the panel that has a grid bag layout
*
* there is technically no reason to have two sets of constraints
* (one for the buttons, one for the left panel), but if panel
* properties wish to modified individually, the constraint and
* addGB methods are seperated for good measure.
*/
GridBagConstraints constraints = new GridBagConstraints();
void addGBS(JPanel panel, Component component, int x, int y){
constraints.gridx =x;
constraints.gridy = y;
panel.add(component, constraints);
}
/**
* INITIALIZATION METHOD, CALLED AT START OF APPLET, ESTABLISHES THE REMAINDER OF THE UI
*
* exception: paint() method builds image border.
*/
public void init()
{
// Set up a panel with a Grid Bag layout on the left side of the pane.
JPanel leftPane = new JPanel(new GridBagLayout());
getContentPane().add("West", leftPane);
//Generate buttons for each of the scanning conditions
SCAN = new JButton("Begin Scanning");
highVButton = new JButton("1.00");
lowVButton = new JButton("0.07");
highIButton = new JButton("2.00");
lowIButton = new JButton("0.10");
//Color scanning condition buttons
lowIButton.setBackground(new Color(211,211,211));
highIButton.setBackground(new Color(211,211,211));
lowVButton.setBackground(new Color(211,211,211));
highVButton.setBackground(new Color(211,211,211));
SCAN.setBackground(new Color(211,211,211));
//add action detection to each of the buttons, this object is its own action listener
SCAN.addActionListener( this );
highVButton.addActionListener( this );
lowVButton.addActionListener( this );
highIButton.addActionListener( this );
lowIButton.addActionListener( this );
//create button panel with a grid bag layout
JPanel panel = new JPanel(new GridBagLayout());
//create pane for text and add to the left panel
addGBS(leftPane, new JScrollPane(text),0,1);
//add buttons and headers to the button panel
addGBB(panel, new JLabel("Voltage (V)"),0,1);
addGBB(panel, new JLabel("Current (nA)"),0,4);
addGBB(panel, highVButton,0,3);
addGBB(panel, lowVButton, 0,2);
addGBB(panel, highIButton, 0,6);
addGBB(panel, lowIButton,0,5);
addGBB(panel, SCAN, 0, 0);
//getContentPane().add("West" , panel);
// add button panel to the left panel
addGBS(leftPane, panel, 0,0);
//author
text.append("@Darin Bellisario\n");
}
/**
*
* DISPLAY IMAGES ACCORDING TO THE BUTTON PRESSED, CALLED WITH THE REPAINT() METHOD
*
*/
public void paint(Graphics g){
//Draw Border
//Get URLs
try { IMGBorderTop = new URL("http://ase.tufts.edu/chemistry/sykes/Applets/STMapplet/IMGBorderTop.jpg");} catch (Exception e){}
try { IMGBorderRight = new URL("http://ase.tufts.edu/chemistry/sykes/Applets/STMapplet/IMGBorderRight.jpg");} catch (Exception e){}
try { IMGBorderLeft = new URL("http://ase.tufts.edu/chemistry/sykes/Applets/STMapplet/IMGBorderLeft.jpg");} catch (Exception e){}
try { IMGBorderBottom = new URL("http://ase.tufts.edu/chemistry/sykes/Applets/STMapplet/IMGBorderBottom.jpg");} catch (Exception e){}
// Draw
BORDERTOP = getImage(IMGBorderTop, "IMGBorderTop.jpg");
BORDERRIGHT = getImage(IMGBorderRight, "IMGBorderRight.jpg");
BORDERLEFT = getImage(IMGBorderLeft, "IMGBorderleft.jpg");
BORDERBOTTOM = getImage(IMGBorderBottom, "IMGBorderBottom.jpg");
g.drawImage(BORDERTOP,imageX - 49, imageY - 20, this );
g.drawImage(BORDERRIGHT,imageX + 613, imageY, this );
g.drawImage(BORDERLEFT,imageX - 49, imageY, this );
g.drawImage(BORDERBOTTOM,imageX, imageY + 613, this );
//Display Appropriate image (HIHV & LILV same image)
if( (highV == true && highI == true) || (lowV == true && lowI == true) ){
for (int i = 1; i<= NUM_FRMS; i++){
//Display first part of pic
try { hVhI = new URL("http://ase.tufts.edu/chemistry/sykes/Applets/STMapplet/LILV&HIHV" + i + "of" + MAX_FRMS + "%20copy.jpg");
picture = getImage(hVhI, "LILV&HIHV" + i + "of" + MAX_FRMS + "%20copy.jpg");
g.drawImage(picture,imageX,imageY,this);
//wait
Thread.sleep(FRMRATE);
}catch (Exception e){System.out.println("the wait thing ****** up");}
}
} else if( (highV == true && lowI == true) ){
for (int i = 1; i<= NUM_FRMS; i++){
//Display first part of pic
try { hVlI = new URL("http://ase.tufts.edu/chemistry/sykes/Applets/STMapplet/LIHV" + i + "of" + MAX_FRMS + "%20copy.jpg");
picture = getImage(hVhI, "LIHV" + i + "of" + MAX_FRMS + "%20copy.jpg");
g.drawImage(picture,imageX,imageY,this);
//wait
Thread.sleep(FRMRATE);
}catch (Exception e){System.out.println("the wait thing ****** up");}
}
} else if( (lowV == true && highI == true) ){
for (int i = 1; i<= NUM_FRMS; i++){
//Display first part of pic
try { lVhI = new URL("http://ase.tufts.edu/chemistry/sykes/Applets/STMapplet/HILV" + i + "of" + MAX_FRMS + "%20copy.jpg");
picture = getImage(hVhI, "HILV" + i + "of" + NUM_FRMS + "%20copy.jpg");
g.drawImage(picture,imageX,imageY,this);
//wait
Thread.sleep(FRMRATE);
}catch (Exception e){System.out.println("the wait thing ****** up");}
}
}
//// Set background of content pane. Don't forget to accordingly change background of left panel!
//// for the time being, setting everything to white background
//getContentPane().setBackground(new Color(200,245,245));
}
//change image based on what buttons pressed. When an action is performed, the actionlistener (this) calls actionperformed
public void actionPerformed (ActionEvent e){
if (e.getSource() == highVButton){
highV = true;
lowV = false;
text.append("Voltage on High\n");
}
if (e.getSource() == lowVButton){
lowV = true;
highV = false;
text.append("Voltage on Low\n");
}
if (e.getSource() == highIButton){
lowI = false;
highI = true;
text.append("Current on High\n");
}
if (e.getSource() == lowIButton){
lowI = true;
highI = false;
text.append("Current on Low\n");
}
if (e.getSource() == SCAN){
text.append("Scanning\n");
repaint();
}
}
}
Thanks for any help!
/Darum

