Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Java

Reply
 
LinkBack Thread Tools Display Modes
Old 03-16-2005, 02:57 PM   #1 (permalink)
davidmccabe
Registered User
 
Join Date: Mar 2004
Posts: 7
davidmccabe is on a distinguished road
drawing shapes in any diagonal direction in applet

i have a simple paint applet, which only allows drawing shapes form the top left to the bottom right, you cant drag it from the bottom right to the top left etc.

how would i go about changing this to allow any direction to be used

this is my applet so far

Code:
// import required modules 
import java.awt.*; 
import java.applet.*; 
import java.awt.event.*;
import javax.swing.*;

public class Drawing extends Applet 
{ 
	// declare required objects
	Panel palette, tools, bgcolour;
	Button line, rect, oval, arc;
	Checkbox filled;
	Button black, cyan, red, blue, green, yellow, pink, white,
	       bgblack, bgred, bgblue, bggreen, bgyellow, bgwhite;
	MyCanvas drawarea;
	TextField startText, sweepText;
	Label startLabel = new Label("Arc starting angle");
	Label sweepLabel = new Label("Arc sweep angle");
	Label linesLabel = new Label("Line Style");
	Label bgColourLabel = new Label("Background Colours");
	Label shapesLabel = new Label("Shapes");
	private JComboBox lineComboBox;
	
		
	// declare and initialise some variables
	int mx = 0; int my = 0; int mx1 = 0; int my1 = 0;
	int currentTool = 0; 
	Color currentColor = new Color(0,0,0);
	boolean wipe = false;
	
	private String message = "Waiting for events...";




		
	public void init()
	{ 
		setLayout(new BorderLayout()); 	// Define a new layout for the applet
		
		lineComboBox = new JComboBox();          
        lineComboBox.setMaximumRowCount(3);
		lineComboBox.addItem("Any Direction");
		lineComboBox.addItem("Horizontal");
		lineComboBox.addItem("Vertical");
		
		lineComboBox.addItemListener(
		new ItemListener() {  // anonymous inner class 
    
                // handle JComboBox event
                public void itemStateChanged( ItemEvent event )
                {
                   // determine whether it is selected
                   if ( event.getStateChange() == ItemEvent.SELECTED )
                         lineComboBox.getSelectedIndex();
                }
    
             }  // end anonymous inner class
                                                           
          ); // end call to addItemListener
	
	
		// Define and position the palette at the bottom of the screen
		palette = new Panel();
		palette.setLayout(new GridLayout(2,4,5,5));
		
		//set up palette buttons
		black = new Button(); 
		black.setBackground(Color.black);
		cyan = new Button();
		cyan.setBackground(Color.cyan);
		red = new Button();
		red.setBackground(Color.red);
		blue = new Button(); 
		blue.setBackground(Color.blue); 
		green = new Button();
		green.setBackground(Color.green);
		yellow = new Button(); 
		yellow.setBackground(Color.yellow);
		pink = new Button();
		pink.setBackground(Color.pink);
		white = new Button();
		white.setBackground(Color.white);
		
		// add palette buttons
		palette.add(black); 
		palette.add(cyan);
		palette.add(red);
		palette.add(blue);
		palette.add(green);
		palette.add(yellow);
		palette.add(pink);
		palette.add(white);
		add(palette,"South");
		
		// Define the position of the bg colour toolbar at the right of the screen
		bgcolour = new Panel();
		bgcolour.setLayout( new GridLayout( 7, 1, 4, 4 ));
		// set up background colour buttons
		bgblack = new Button(); 
		bgblack.setBackground(Color.black);
		bgred = new Button(); 
		bgred.setBackground(Color.red);
		bgblue = new Button(); 
		bgblue.setBackground(Color.blue);
		bggreen = new Button(); 
		bggreen.setBackground(Color.green);
		bgyellow = new Button(); 
		bgyellow.setBackground(Color.yellow);
		bgwhite = new Button(); 
		bgwhite.setBackground(Color.white);
		
		bgcolour.add(bgColourLabel);
		// add background colour buttons
		bgcolour.add(bgblack); 
		bgcolour.add(bgred);
		bgcolour.add(bgblue);
		bgcolour.add(bggreen);
		bgcolour.add(bgyellow);
		bgcolour.add(bgwhite);
		add(bgcolour,"East");
	
		// Define and position the tool bar at the left of the screen 
		tools = new Panel();
		tools.setLayout(new GridLayout(12,1,5,5)); 
		line = new Button("Line");
		rect = new Button("Rectangle");
		oval = new Button("Oval");
		arc = new Button("Arc");
		filled = new Checkbox("Filled",false); 
		startText = new TextField(10);
		sweepText = new TextField(10);
		startText.setText("0");
		sweepText.setText("270");
		tools.add(shapesLabel);
		tools.add(line);
		tools.add(rect);
		tools.add(oval);
		tools.add(arc);
		tools.add(filled);
		tools.add(startLabel);
		tools.add(startText);
		tools.add(sweepLabel);
		tools.add(sweepText);
		tools.add(linesLabel);
		tools.add(lineComboBox);
		add(tools,"West");
		
		drawarea = new MyCanvas();
		add(drawarea,"Center"); // add drawing canvas to the middle of the screen
		validate();
		
	}// end method init
	
	// If the user clicks a button on the toolbar, then change the value of "currentTool" and call "filled.setEnable" appropriately
	public boolean action(Event evt, Object obj) // define event handler
	{
		if (evt.target == line)
		{
			currentTool = 0;
			filled.setEnabled(false);
		}
		else if (evt.target == rect)
		{
			currentTool = 1;
			filled.setEnabled(true);
        } 
		else if (evt.target == oval)
		{
			currentTool = 2;
 			filled.setEnabled(true);
 		}
 		else if (evt.target == arc)
 		{
			currentTool = 3;
 			filled.setEnabled(true);
 		}  
		
						
		// Change the value of currentColor to the appropraite color clicked by user
		else if (evt.target == black) currentColor = new Color(0,0,0);
 		else if (evt.target == cyan) currentColor = new Color(0,255,255);
 		else if (evt.target == red) currentColor = new Color(255,0,0);
 		else if (evt.target == blue) currentColor = new Color(0,0,255);
 		else if (evt.target == green) currentColor = new Color(0,255,0);
 		else if (evt.target == yellow) currentColor = new Color(255,255,0);
		else if (evt.target == pink) currentColor = new Color(255,175,175);
		else if (evt.target == white) currentColor = new Color(255,255,255);
		
		// change the colour of the canvas according to which buttont he user clicks
		else if (evt.target == bgblack) drawarea.setBackground(Color.black);
		else if (evt.target == bgred) drawarea.setBackground(Color.red);
		else if (evt.target == bgblue) drawarea.setBackground(Color.blue);
		else if (evt.target == bggreen) drawarea.setBackground(Color.green);
		else if (evt.target == bgyellow) drawarea.setBackground(Color.yellow);
		else if (evt.target == bgwhite) drawarea.setBackground(Color.white);
 
		return (true);
		} 
	
	// Create a new class called "MyCanvas" which extends the standard class Canvas
	class MyCanvas extends Canvas implements MouseListener
	{
		
		// the classes constructor
		public MyCanvas() 
		{
			addMouseListener(this);
		} 
		
		// These actions are performed when the user is pressing the left mouse button 
		public void mousePressed(MouseEvent e) 
		{
			mx = e.getX();
			my = e.getY();
			mx1 = e.getX();
			my1 = e.getY();
		// return (true);
 		}// end method mousePressed
		
		// These actions are performed when the user is dragging the mouse
		public void mouseReleased(MouseEvent e)
	  	{
			mx1 = e.getX();
			my1 = e.getY();
			repaint();
		}// end method mouseReleased 
		
	public void mouseClicked(MouseEvent e)
	{
		//empty method
	}// end method mouseClicked
	

	public void mouseEntered(MouseEvent e)
	{
		//empty method
	}// end method mousePressed

	
	public void mouseExited(MouseEvent e)
	{
		//empty method
	}// end method mousePressed			
		
		public void update(Graphics g)
		{
			paint(g);
		}// end method update
		
		
		public void paint(Graphics g) // When the user paints on the canvcas, then do the following 
		{
			// add header text
			g.drawString( "S-ID: 12129103 - David McCabe", 0, 15 );
			
			String s = startText.getText();
	        int start = Integer.parseInt(s);
	        s = sweepText.getText();
	        int sweep = Integer.parseInt(s);
			
			g.setColor(currentColor); // Set the drawing color to that selected
	
			   // Determine shape selected and if it is to be filled with current colour
			   switch ( currentTool )
			   {                                                                   
                case 0:  // line selected
				   g.drawLine( mx, my, mx1, my1 );
				   /*
				   if ( lineNum == 0 )
				      g.drawLine( mx, my, mx1, my1 ); 
					  else if ( lineNum == 1)
					     g.drawLine( mx, my, mx1, my );
						 else if ( lineNum == 2)
						    g.drawLine( mx, my, mx, my1); 
					*/
					
                   break;  // done processing case                      
                                                                        
                case 1:  // rectangle selected                            
                   if ( filled.getState() == true )
                      g.fillRect( mx, my, mx1-mx, my1-my );
                   else g.drawRect( mx, my, mx1-mx, my1-my );                    
                   break;  // done processing case                      
                                                                        
                case 2:  // oval selected                                
                   if (filled.getState() == true)
                      g.fillOval( mx, my, mx1-mx, my1-my );
			       else g.drawOval( mx, my, mx1-mx, my1-my );                       
                   break;  // done processing case
                
                case 3:  // arc selected                                
                   if (filled.getState() == true)
                      g.fillArc( mx, my, mx1-mx, my1-my, start, sweep );
			       else g.drawArc( mx, my, mx1-mx, my1-my, start, sweep );                       
                   break;  // done processing case 
                                                                   
                default:                                 
                                                                        
                } // end switch                                            
			  
		}// end method paint
	
	}// end class MyCanvas
	
}// end class Drawing
davidmccabe is offline   Reply With Quote
Old 03-16-2005, 03:05 PM   #2 (permalink)
webcomplete.com
Registered User
 
Join Date: Mar 2005
Posts: 14
webcomplete.com is on a distinguished road
hello davidmccabe

do you have a link to this online so i dont have to compile it and create main or a webpage for it? i need to see how it works currently...

thanks
webcomplete.com is offline   Reply With Quote
Old 03-17-2005, 03:47 AM   #3 (permalink)
davidmccabe
Registered User
 
Join Date: Mar 2004
Posts: 7
davidmccabe is on a distinguished road
link to applet online

yes it is online at the link below:

link to applet online

thanks for your help
davidmccabe is offline   Reply With Quote
Old 03-17-2005, 03:53 AM   #4 (permalink)
davidmccabe
Registered User
 
Join Date: Mar 2004
Posts: 7
davidmccabe is on a distinguished road
more information about applet

sorry to ask so much of you, but could you tell me how to:
- add text to the applet at mouse position
- implement snap to grid function
- control the width of the line

and the combo box is supposed to control the direction the line is drawn, only im not sure how to add a listener to do this

if you could help with any of these sometime today before 3pm it would be a great help.
davidmccabe is offline   Reply With Quote
Reply

Bookmarks

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

BB 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
Change line width in drawing applet/ allow text entry davidmccabe Java 0 03-16-2005 07:45 AM


All times are GMT -8. The time now is 01:24 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting