LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-26-2006, 02:15 PM   #1
AquamaN
Member
 
Registered: Oct 2002
Location: Ohio, USA
Distribution: OS X 10.4.8, Ubuntu 6.10
Posts: 146

Rep: Reputation: 15
Java - Questions


Ok, I'm strayed from Obj-C, C/C++ and am trying out Java now. I have run into some trouble with my KeyListener as my simple program is not changing my modes (from polygon to circle).


Draw.java
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
    
    
public class Draw extends JApplet implements KeyListener
{  
	Display canvas;
	
	public void init()
	{
		canvas = new Display();
		setContentPane(canvas);
	}
	   
	public char mode = 'c';
	   
	public void keyTyped(KeyEvent evt) 
	{
		char ch = evt.getKeyChar();
		
		if (ch == 'p' || ch == 'P')
			mode = 'p';
		else if (ch == 'c' || ch == 'C')
			mode = 'c';
		else if (ch == 'b' || ch == 'B')
			mode = 'b';
	}
	   
	public void keyPressed(KeyEvent evt) {}
	public void keyReleased(KeyEvent evt) {}
	
    class Display extends JPanel implements MouseListener
	{
            
          /* Variables for implementing polygon input. */

		private int[] xCoord, yCoord;  // Arrays containing the points of 
                                         //   the polygon.  Up to 500 points 
                                         //   are allowed.

		private int pointCt;  // The number of points that have been input.

		private Color polygonColor = Color.red;
                               // Color that is used to draw the polygons. 
		  
		private Color theColor = Color.red;
		

		public Display() 
		{
			setBackground(Color.white);
			addMouseListener(this);
			xCoord = new int[500];
			yCoord = new int[500];
			pointCt = 0;
		}


		public void paintComponent(Graphics g) 
		{

			super.paintComponent(g); // Fills with background color, white.
			g.setColor(Color.black);
			g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
			g.drawRect(1, 1, getWidth() - 3, getHeight() - 3);
			g.drawLine(35, 0, 35, 250);
			 
			g.setColor(Color.black);
			g.fillRect(0, 0, 35, 50);
			
			g.setColor(Color.blue);
			g.fillRect(0, 50, 35, 100);
			 
			g.setColor(Color.red);
			g.fillRect(0, 100, 35, 150);
			 
			g.setColor(Color.yellow);
			g.fillRect(0, 150, 35, 200);
			
			g.setColor(Color.green);
			g.fillRect(0, 200, 35, 250);
		}


		private void putLine(int x1, int y1, int x2, int y2) 
		{
			Graphics g = getGraphics();
			g.drawLine(x1,y1,x2,y2);
			g.dispose();
		}
		  
		private void drawACircle (int x1, int y1, int x2, int y2)
		{
			Graphics g = getGraphics();
			int dx, dy;
			
			dx = Math.abs(x1 - x2);
			dy = Math.abs(y1 - y2);
			double radius = Math.sqrt((dx * dx) + (dy * dy));
		
			g.setColor(theColor);
			g.fillOval(x1, y1, (int)radius, (int)radius);
			g.dispose();
		}


		private void putPolygon()
		{
			if (pointCt < 2)
				return;
			Graphics g = getGraphics();
			if (pointCt == 2) 
			{
				g.drawLine(xCoord[0], yCoord[0], xCoord[1], yCoord[1]);
			}
			else 
			{
				g.setColor(theColor);
				g.fillPolygon(xCoord, yCoord, pointCt);
				g.setColor(Color.black);
				g.drawPolygon(xCoord, yCoord, pointCt);
			}
				g.dispose();
		}


		public void mousePressed(MouseEvent evt) 
		{ 
			if (evt.isShiftDown()) 
			{
				pointCt = 0;
				repaint();
			}
			else if ( pointCt > 0 && (Math.abs(xCoord[0] - evt.getX())<=2) && (Math.abs(yCoord[0] - evt.getY())<=2) && mode == 'p' ) 
			{
				putPolygon();
				pointCt = 0;
			}
			else if (evt.isMetaDown() || pointCt == 500)
			{
				putPolygon();
				pointCt = 0;
			}
			else 
			{
				if (mode == 'p') 
				{
				
					xCoord[pointCt] = evt.getX();
					yCoord[pointCt] = evt.getY();
						//Get color to be used
					if (evt.getX() <= 25) 
					{
						if (evt.getY() <= 50)
							theColor = Color.black;
						else if (evt.getY() <= 100)
							theColor = Color.blue;
						else if (evt.getY() <= 150)
							theColor = Color.red;
						else if (evt.getY() <= 200)
							theColor = Color.yellow;
						else if (evt.getY() <= 250)
							theColor = Color.green;
				
							pointCt--;
					} 
				
					pointCt++;
				
					if (pointCt >= 2 && evt.getX() > 25) 
					{
						putLine(xCoord[pointCt-2], yCoord[pointCt-2], xCoord[pointCt-1], yCoord[pointCt-1]); 
					}
				}// end if 'p'
		 
				if (mode == 'c') //Circle Mode
				{
					xCoord[pointCt] = evt.getX();
					yCoord[pointCt] = evt.getY();
				
					if (evt.getX() <= 25)
					{
							if (evt.getY() <= 50)
								theColor = Color.black;
							else if (evt.getY() <= 100)
								theColor = Color.blue;
							else if (evt.getY() <= 150)
								theColor = Color.red;
							else if (evt.getY() <= 200)
								theColor = Color.yellow;
							else if (evt.getY() <= 250)
								theColor = Color.green;
					}
					
					pointCt++;
				
					if (pointCt >= 2 && evt.getX() > 25) 
					{
						drawACircle(xCoord[pointCt-1], yCoord[pointCt-1], xCoord[pointCt], yCoord[pointCt]); 
						pointCt = 0;
					}
				}//end mode 'c'
			} //end else
			canvas.requestFocus();
		} // end mousePressed()

		public void mouseReleased(MouseEvent evt) { }
		public void mouseClicked(MouseEvent evt) { }
		public void mouseEntered(MouseEvent evt) { }
		public void mouseExited(MouseEvent evt) { }
          
	} // end nested class Display
}  // end class Draw
Draw.html
Code:
<html>
<head>
<TITLE>Simple Paint Program</TITLE>
</head>
<body>

<p align=center>
<applet code="Draw.class" height=250 width=300>
</applet>
</p>

</body>
</html>
Any help as to why it's not working properly would be great. Thanks guys.
 
Old 03-26-2006, 02:32 PM   #2
AquamaN
Member
 
Registered: Oct 2002
Location: Ohio, USA
Distribution: OS X 10.4.8, Ubuntu 6.10
Posts: 146

Original Poster
Rep: Reputation: 15
Ok, figured it out. Needed "canvas.addKeyListener(this);" in my init().
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Java questions microsoft/linux Programming 10 12-31-2005 08:13 PM
More Java Questions Matir Programming 4 03-07-2005 04:42 PM
Questions in regards to learning C, C++ and Java... eBopBob Programming 8 12-21-2004 10:09 AM
2 Questions: java calling system commands? PERL vs Java? randomx Programming 28 11-28-2003 08:24 PM
Questions about Java wh33t Programming 2 11-03-2003 04:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration