Results 1 to 7 of 7
  1. #1
    jmanes92 is offline Member
    Join Date
    Apr 2012
    Location
    Warrensburg, MO
    Posts
    4
    Rep Power
    0

    Default Etch A Sketch Like Program Using Arrow Keys

    Hello everyone. This is my first post here. I've never really need help with Java until now, so I hope everything goes ok.

    I am supposed to draw lines using the graphics class by mapping directions to arrow keys. My code is as follows:

    Java Code:
    // Import Neded Utilites
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class DrawLine extends JFrame {
    
    	// Constructor
    	public DrawLine() {
    
    		// Add CreateLine Panel
    		add(new CreateLine());
    
    	}
    
    	// Main Method
    	public static void main(String[] args) {
    
    		// Create GUI Settings
    		DrawLine frame = new DrawLine();
    		frame.setTitle("Exercise 16.9");
    		frame.setSize(400, 400);	
    		frame.setLocationRelativeTo(null);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    	}
    
    
    	// Inner Class For Receiving Input
    	static class CreateLine extends JPanel {
    
    		// Declare Variables
    		private int x1 = 200;
    		private int y1 = 200;
    		private int x2 = 200;
    		private int y2 = 200;
    
    		// Constructor
    		public CreateLine() {
    
    			// Set Focus
    			setFocusable(true);
    
    			// Add Key Listener
    			addKeyListener(new KeyAdapter() {
    
    				// Key Direction Method
    				public void keyPressed(KeyEvent e) {
    					switch (e.getKeyCode()) {
    						case KeyEvent.VK_UP:	y1 -= 10; break;
    						case KeyEvent.VK_DOWN:	y2 += 10; break;
    						case KeyEvent.VK_LEFT:	x1 -= 10; break;
    						case KeyEvent.VK_RIGHT:	x2 += 10; break;
    					}
    
    					// Paint Lines
    					repaint();
    				}
    			});
    		}
    
    		// Graphics Method
    		public void paint(Graphics g) {
    
    			// Draw Lines
    			super.paint(g);
    				g.drawLine(x1, y1, x2, y2);
    		}
    	}
    }
    When I compile and run the code, the line starts in the center as it should, but the arrow keys make the line do odd things. I understand why it makes the line act differently, but I'm not sure how to fix it. Any help?

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

    Default Re: Etch A Sketch Like Program Using Arrow Keys

    Your paint method is written to draw one line and one line only, which is not what I think that you want to do. Perhaps you want to use your arrow keys to place points in an ArrayList and call repaint() on the drawing JPanel, and then in its paint, iterate through the ArrayList drawing your multiple connected lines.

    Note that all drawing should be done in the JPanel's paintComponent method, not its paint method.

  3. #3
    jmanes92 is offline Member
    Join Date
    Apr 2012
    Location
    Warrensburg, MO
    Posts
    4
    Rep Power
    0

    Default Re: Etch A Sketch Like Program Using Arrow Keys

    Some of my friends that have completed the project claim they did not have to use arrays at all, but it is worth a shot. I'll try it out and report back.

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

    Default Re: Etch A Sketch Like Program Using Arrow Keys

    Quote Originally Posted by jmanes92 View Post
    Some of my friends that have completed the project claim they did not have to use arrays at all,
    And I'm not recommending that you do this either, but rather a List<Point> such as an ArrayList<Point>. Another way to accomplish this is to draw onto a BufferedImage and then display the image in your paintComponent method (again, *not* your paint method).

    but it is worth a shot. I'll try it out and report back.
    It's not only worth a shot, it works. I've already modified your code with a few lines and have gotten it to work great.

  5. #5
    jmanes92 is offline Member
    Join Date
    Apr 2012
    Location
    Warrensburg, MO
    Posts
    4
    Rep Power
    0

    Default Re: Etch A Sketch Like Program Using Arrow Keys

    Got it! ;)

    Finished Code.

    Java Code:
    // Inner Class For Receiving Input
    	static class CreateLine extends JPanel {
    
    		// Declare Variables
    		private int x1 = 200;
    		private int y1 = 200;
    		private int x2 = 200;
    		private int y2 = 200;
    
    		// Constructor
    		public CreateLine() {
    
    			// Set Focus
    			setFocusable(true);
    
    			// Add Key Listener
    			addKeyListener(new KeyAdapter() {
    
    				// Key Direction Method
    				public void keyPressed(KeyEvent e) {
    					switch (e.getKeyCode()) {
    						case KeyEvent.VK_UP:
    							x1 = x2; y2 -= 10; break;
    						case KeyEvent.VK_DOWN:
    							x1 = x2; y2 += 10; break;
    						case KeyEvent.VK_LEFT:
    							y1 = y2; x2 -= 10; break;
    						case KeyEvent.VK_RIGHT:
    							y1 = y2; x2 += 10; break;
    					}
    
    					// Paint Lines
    					repaint();
    				}
    			});
    		}
    
    		// Graphics Method
    		public void paintComponent(Graphics g) {
    
    			// Draw Lines
    			g.drawLine(x1, y1, x2, y2);
    		}
    	}
    }
    Thanks for showing me your way of doing it though! The help was appreciated.

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

    Default Re: Etch A Sketch Like Program Using Arrow Keys

    Quote Originally Posted by jmanes92 View Post
    Got it! ;)
    Um, no you didn't.

    Finished Code.

    Java Code:
    		// ....
    
    		// Graphics Method
    		public void paintComponent(Graphics g) {
    
    			// Draw Lines
    			g.drawLine(x1, y1, x2, y2);
    		}
    	}
    }
    Thanks for showing me your way of doing it though! The help was appreciated.
    Sorry, but my way of doing will work, but yours above won't. Try to draw on your screen, and then minimize and restore your GUI to see what I mean. What happens to your drawing then?

    Again, I urge you to do as I suggested if you want to do this correctly and robustly.

  7. #7
    jmanes92 is offline Member
    Join Date
    Apr 2012
    Location
    Warrensburg, MO
    Posts
    4
    Rep Power
    0

    Default Re: Etch A Sketch Like Program Using Arrow Keys

    Quote Originally Posted by Fubarable View Post
    Um, no you didn't.



    Sorry, but my way of doing will work, but yours above won't. Try to draw on your screen, and then minimize and restore your GUI to see what I mean. What happens to your drawing then?

    Again, I urge you to do as I suggested if you want to do this correctly and robustly.
    We have not learned about using ArrayList Points and I honestly could not follow what you said entirely. I got the just, but did not know how to implement it. I tried researching it and I could not find out exactly what to do. So I did it this way and showed my professor. It was ok by him. The assignment is only to test our knowledge of mapping keys and the graphics class, so I will get full credit.

Similar Threads

  1. Replies: 4
    Last Post: 12-03-2011, 05:38 PM
  2. how to move a rectangle with arrow keys,
    By jmu2101 in forum AWT / Swing
    Replies: 3
    Last Post: 09-27-2011, 02:13 AM
  3. Arrow Keys?
    By Alerhau in forum New To Java
    Replies: 10
    Last Post: 06-17-2011, 08:45 PM
  4. Moving a ball with Arrow Keys
    By kekcklemen in forum Java Applets
    Replies: 5
    Last Post: 02-25-2011, 11:15 PM
  5. Replies: 0
    Last Post: 07-11-2008, 04:07 PM

Tags for this Thread

Posting Permissions

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