Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-08-2008, 04:11 PM
Member
 
Join Date: Feb 2008
Posts: 16
Deathmonger is on a distinguished road
Help using Buttons/Actions with Drawings
Hi, I'm sure this is a simple fix, but I don't know what to use to fix it. I am playing around with a program that allows the user to interact with an object. The program I'm starting allows the user to choose a direction, and move a square 5 pixels. However, I can't seem to get it to work. When you click on the "Up" button, the square is supposed to move 5 pixels up, and when you click on "Left," the square moves 5 pixels left, and so on...

Here is the code I created so far, and when I click on one of these buttons, the square doesn't do anything. I'm sure it's a simple fix, I just don't know what it is.

Any help is greatly appreciated. Thanks

Code:
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class InteractionX { static JFrame frame; static Container contain; static JButton up; static JButton down; static JButton left; static JButton right; static Panel controls; static Panel canvasArea; static int x = 75; static int y = 75; static class InteractXCanvas extends Canvas { public void paint(Graphics g) { g.fillRect(x,y,10,10); } } static class InteractXMovement implements ActionListener { InteractXCanvas IXC = new InteractXCanvas(); public void actionPerformed(ActionEvent ae) { String whichButton; whichButton = ae.getActionCommand(); int x2 = 0; int y2 = 0; if(whichButton.equals("Up")) { y = y + 5; } if(whichButton.equals("Down")) { y = y-5; } if(whichButton.equals("Left")) { x = x-5; } if(whichButton.equals("Right")) { x = x+5; } IXC.repaint(); } } public static void main(String[] args) { InteractXCanvas IXC = new InteractXCanvas(); InteractXMovement IXM = new InteractXMovement(); frame = new JFrame("Interaction World"); contain = new Container(); up = new JButton("Up"); down = new JButton("Down"); left = new JButton("Left"); right = new JButton("Right"); controls = new Panel(); canvasArea = new Panel(); up.addActionListener(IXM); down.addActionListener(IXM); left.addActionListener(IXM); right.addActionListener(IXM); frame.setSize(500,500); IXC.setSize(100,100); contain = frame.getContentPane(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); contain.setLayout(new GridLayout(2,3)); controls.add(up); controls.add(down); controls.add(left); controls.add(right); canvasArea.add(IXC); frame.setVisible(true); } }
__________________
Jai guru deva om, Nothing's gonna change my world
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
GUI buttons question... glenrowan New To Java 0 01-16-2008 06:49 PM
Actions alley AWT / Swing 2 01-16-2008 03:52 PM
Bean related actions in JSP Java Tip Java Tips 0 12-24-2007 11:04 AM
Next, Finish Buttons !!! pele SWT / JFace 1 07-14-2007 06:22 PM
Maximum size permited in the parameters in actions and servlets Eric Java Servlet 2 07-03-2007 05:09 PM


All times are GMT +3. The time now is 12:48 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org