The Artima Developer Community
Sponsored Link

Java Answers Forum
How to make a proper button listener ?

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Avin Sinanan

Posts: 28
Nickname: avin
Registered: Feb, 2002

How to make a proper button listener ? Posted: Mar 10, 2002 8:23 PM
Reply to this message Reply
Advertisement
Hello I've created some code to do the following:

1)It creates a frame and a panel.
2)A background image is added to the panel.
3)The panel is then added to the frame.
4)Then a image ( a jpg) moves from one point to
another on the panel. It move in a continuous motion from one point to another.

Ok the code below does the above. It should be noted that the code belows works perfectly. Examine to code and then I'll tell you what I trie dto extend the code to do and the problem I encounterd.

import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.awt.*;
import java.lang.Number.*;
 
class Test
{
	public static void main(String[] args)
	{
 
        Move move = new Move();
 
	}
}
 
 
class Move
{
	public JFrame frame ;
	public ImagePnl pane ;
	public JLabel label;
 
	public Move()
	{		
		frame = new JFrame();
		pane = new ImagePnl();
		pane.setLayout(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(800,770);
		frame.setVisible(true);
 
		
		JLabel van = new JLabel(new ImageIcon("VAN.jpg"));
 
		for(int i=219 ; i <=412 ; i++ )
		{
		Calculation cal = new Calculation();
		double x2minusx1 = 193 ;
		int y = cal.calculation(219,359,412,344,i,x2minusx1);
 
		van.setBounds(i,y,40,40);
		pane.add(van);
		frame.getContentPane().add(pane);
		pane.updateUI();
		}
    }
 
}
 
class ImagePnl extends JPanel
{
ImageIcon ic = new ImageIcon("GSMMAP2.jpg");
 
public ImagePnl()
{
}
 
public void paintComponent(Graphics g)
{
g.drawImage(ic.getImage() ,0,0,Color.red, null);
}
	
	
}
 


Ok note that was soon as the program is executed the small image ( VAN.jpg) starts to move in a smoothe motion (using the for loop) from one point to another.

However I do not want it to start right away. Instead I want to add a button to another frame and only when that button is pressed only then does the VAN.jpg image starts to move..only when that button is pressed. The code below is what I did to try to implement this. However when I ran the code below and the button was pressed the VAN.jpg picture moved to its final position and only then was it shown. That is - the user did not see the image move continously from one point to another. All the user sees is the image in its final position after the button was pressed.

Can someone tell me what am doing wrong. Thanks a million. Here is the code.


import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.awt.*;
import java.lang.Number.*;
 
class Test
{
	public static void main(String[] args)
	{
 
        JFrame frame = new JFrame();
		JPanel pane = new JPanel();
		JButton button = new JButton("Hello");
		
		button.addActionListener(new ActionListener(){
	    public void actionPerformed(ActionEvent e){
            
          		Move move = new Move();
         }
	});
 
        pane.add(button);
        frame.getContentPane().add(pane);
        frame.setSize(100,100);
        frame.setVisible(true);
 
	}
}
 
 
class Move
{
	public JFrame frame ;
	public ImagePnl pane ;
	public JLabel label;
 
	public Move()
	{		
		frame = new JFrame();
		pane = new ImagePnl();
		pane.setLayout(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(800,770);
		frame.setVisible(true);
 
		
		JLabel van = new JLabel(new ImageIcon("VAN.jpg"));
 
		for(int i=219 ; i <=412 ; i++ )
		{
		Calculation cal = new Calculation();
		double x2minusx1 = 193 ;
		int y = cal.calculation(219,359,412,344,i,x2minusx1);
 
		van.setBounds(i,y,40,40);
		pane.add(van);
		frame.getContentPane().add(pane);
		pane.updateUI();
		}
    }
 
}
 
class ImagePnl extends JPanel
{
ImageIcon ic = new ImageIcon("GSMMAP2.jpg");
 
public ImagePnl()
{
}
 
public void paintComponent(Graphics g)
{
g.drawImage(ic.getImage() ,0,0,Color.red, null);
}
	
	
}
 
 
 
 

Topic: Java Code Previous Topic   Next Topic Topic: reading menu items

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use