Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
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 06-17-2008, 11:08 PM
Member
 
Join Date: Feb 2008
Posts: 16
Deathmonger is on a distinguished road
Help with 2-D Drawing
Hi,

I'm trying to create a simple program that allows the user to control the movement of a square. When the user presses up, the square moves up and so on. However, I can't seem to get the square to move. I think I'm missing the concept of calling the correct method that allows me to repaint the square at the new location. I'm sure there is more to it, and I've been trying to figure this out for a couple of months now (on and off.) I don't know what to do, and if someone knows how to get this to work, some advice would be greatly appreciated. Can someone please help me figure out how to get this square to move?

Thanks in advance

Here is the code I'm using:

Code:
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class InteractionX { // Declare all Buttons, Panels, and labels used for testing 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 Panel test; static Panel holdsBtns; static JLabel yTest; static JLabel xTest; // These are the initial coordinates for the square static int x = 75; static int y = 75; // Class: InteractXCanvas // Description: Used to draw the square and the borders for the canvas. static class InteractXCanvas extends Canvas { public void paint(Graphics g) { Dimension d = canvasArea.getSize(); int lengthCanvas = d.width; int heightCanvas = d.height; g.drawRect(0,0,lengthCanvas-1,heightCanvas-1); g.fillRect(x,y,10,10); } } // Class: InteractXMovement // Description: Holds the logic for controlling movement of the square. static class InteractXMovement implements ActionListener { public void actionPerformed(ActionEvent ae) { String whichButton; whichButton = ae.getActionCommand(); InteractXCanvas IXC = new InteractXCanvas(); 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; } yTest.setText("y: " + y); xTest.setText("x: " + x); } } public static void main(String[] args) { // Create constructors for the Movement class and Canvas class. InteractXMovement IXM = new InteractXMovement(); InteractXCanvas IXC = new InteractXCanvas(); // Create Buttons used for controlling the square and the labels used for testing. frame = new JFrame("Interaction World"); contain = new Container(); up = new JButton("Up"); down = new JButton("Down"); left = new JButton("Left"); right = new JButton("Right"); yTest = new JLabel("y: " + y); xTest = new JLabel("x: " + x); // Create Panels controls = new Panel(); canvasArea = new Panel(); test = new Panel(); holdsBtns = new Panel(); // Add the actionListener from class InteractXMovement up.addActionListener(IXM); down.addActionListener(IXM); left.addActionListener(IXM); right.addActionListener(IXM); // Set frame size and canvas size frame.setSize(500,500); IXC.setSize(200,200); contain = frame.getContentPane(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Set layout managers contain.setLayout(new BorderLayout()); controls.setLayout(new GridLayout(1,4)); test.setLayout(new GridLayout(1,2)); // Add buttons and test labels to their panels. The buttons are // attached to the controls panel, and the labels to the test panel controls.add(up); controls.add(down); controls.add(left); controls.add(right); test.add(yTest); test.add(xTest); holdsBtns.add(controls); holdsBtns.add(test); canvasArea.add(IXC); frame.add(holdsBtns, BorderLayout.NORTH); frame.add(canvasArea, BorderLayout.CENTER); frame.setVisible(true); } }
__________________
Jai guru deva om, Nothing's gonna change my world
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-18-2008, 12:35 AM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 386
Zosden is on a distinguished road
Try this website Linky
__________________
My IP address is 127.0.0.1
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-18-2008, 01:05 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class InteractionXRx { // Declare only what must be exposed as a member // variable, ie, whatever requires class scope. JLabel yTest; JLabel xTest; InteractXCanvas IXC; // These are the initial coordinates for the square int x = 75; int y = 75; // Class: InteractXCanvas // Description: Used to draw the square and the borders for the canvas. private class InteractXCanvas extends Canvas { public void paint(Graphics g) { Dimension d = //canvasArea.getSize(); getSize(); int lengthCanvas = d.width; int heightCanvas = d.height; g.drawRect(0,0,lengthCanvas-1,heightCanvas-1); g.fillRect(x,y,10,10); } /** Override getPreferredSize method */ public Dimension getPreferredSize() { return new Dimension(200,200); } } // Class: InteractXMovement // Description: Holds the logic for controlling movement of the square. private class InteractXMovement implements ActionListener { public void actionPerformed(ActionEvent ae) { String whichButton = ae.getActionCommand(); // This new instance of InteractXCanvas is not the // one you added to your gui. The x,y member variables // are used in the one you added to your gui. // InteractXCanvas IXC = new InteractXCanvas(); 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; } yTest.setText("y: " + y); xTest.setText("x: " + x); // Ask the view component to show the change: IXC.repaint(); } } private void showGUI() { // Create constructors for the Movement class and Canvas class. IXC = new InteractXCanvas(); // Create Buttons used for controlling the square and // the labels used for testing. JButton up = new JButton("Up"); JButton down = new JButton("Down"); JButton left = new JButton("Left"); JButton right = new JButton("Right"); yTest = new JLabel("y: " + y); xTest = new JLabel("x: " + x); // Create Panels Panel controls = new Panel(); Panel test = new Panel(); Panel holdsBtns = new Panel(); // Add the actionListener from class InteractXMovement InteractXMovement IXM = new InteractXMovement(); up.addActionListener(IXM); down.addActionListener(IXM); left.addActionListener(IXM); right.addActionListener(IXM); // Set frame size and canvas size JFrame frame = new JFrame("Interaction World"); frame.setSize(500,500); // Does no good before gui is realized, ie, after // pack or setVisible is called. // Better to override getPreferredSize in // the InteractXCanvas class. // IXC.setSize(200,200); Container contain = frame.getContentPane(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Set layout managers contain.setLayout(new BorderLayout()); controls.setLayout(new GridLayout(1,4)); test.setLayout(new GridLayout(1,2)); // Add buttons and test labels to their panels. The buttons are // attached to the controls panel, and the labels to the test panel controls.add(up); controls.add(down); controls.add(left); controls.add(right); test.add(yTest); test.add(xTest); holdsBtns.add(controls); holdsBtns.add(test); frame.add(holdsBtns, BorderLayout.NORTH); // You can show the view/graphic component (IXC) // in the center of another Panel: Panel canvasArea = new Panel(new GridBagLayout()); canvasArea.add(IXC, new GridBagConstraints()); frame.add(canvasArea, BorderLayout.CENTER); // or, eliminate the intermediate container (canvasArea) // and put IXC in the center section of your BorderLayout: // frame.add(IXC, BorderLayout.CENTER); frame.setVisible(true); } public static void main(String[] args) { InteractionXRx app = new InteractionXRx(); app.showGUI(); } }
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-18-2008, 01:12 AM
Member
 
Join Date: Feb 2008
Posts: 16
Deathmonger is on a distinguished road
Thanks both for your help. I can already see there is much more that I need to study.
__________________
Jai guru deva om, Nothing's gonna change my world
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-18-2008, 04:23 AM
Fubarable's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 898
Fubarable is on a distinguished road
You appear to be mixing Swing components (JButtons, and most anything else that starts with "J") and AWT components (Panel, etc...). I strongly advise you against doing this unless you really know what you are doing, in other words unless you are very strong in your Swing and AWT coding. Myself, I'm not yet strong enough. Also, if you haven't done so, you would be well served by going through the Sun Swing graphics tutorials.

Good luck.
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
Drawing on aJPanel Djangolo AWT / Swing 1 02-17-2008 03:01 AM
drawing window BlitzA Advanced Java 0 12-30-2007 07:39 PM
drawing window BlitzA New To Java 0 12-30-2007 06:45 PM
Drawing outside paintComponent() DarkSide1 Java 2D 2 11-09-2007 12:36 AM
Help with Drawing a line Rgfirefly24 New To Java 1 08-06-2007 10:40 AM


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


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