Results 1 to 5 of 5
  1. #1
    Deathmonger is offline Member
    Join Date
    Feb 2008
    Posts
    16
    Rep Power
    0

    Default 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:

    Java 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

  2. #2
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    Try this website Linky
    My IP address is 127.0.0.1

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java 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 [i]getPreferredSize[/i] 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
            // [i]pack[/i] or [i]setVisible[/i] is called.
            // Better to override [i]getPreferredSize[/i] 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();
        }
    }

  4. #4
    Deathmonger is offline Member
    Join Date
    Feb 2008
    Posts
    16
    Rep Power
    0

    Default

    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

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

Similar Threads

  1. drawing window
    By BlitzA in forum New To Java
    Replies: 1
    Last Post: 01-15-2009, 01:55 PM
  2. Drawing on aJPanel
    By Djangolo in forum AWT / Swing
    Replies: 1
    Last Post: 02-17-2008, 02:01 AM
  3. drawing window
    By BlitzA in forum Advanced Java
    Replies: 0
    Last Post: 12-30-2007, 06:39 PM
  4. Drawing outside paintComponent()
    By DarkSide1 in forum Java 2D
    Replies: 2
    Last Post: 11-08-2007, 11:36 PM
  5. Help with Drawing a line
    By Rgfirefly24 in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 09:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •