Applets

Applets

this is one of the code i am working on. I am trying to get the end points of the line when i click on the the line but it is giving the end points as the same point where the line is clicked

class DrawPanel extends Panel implements MouseListener, MouseMotionListener { public static final int LINES = 0; public static final int POINTS = 1; int mode = LINES; @SuppressWarnings("rawtypes") static Vector lines = new Vector(); @SuppressWarnings("rawtypes") static Vector colors = new Vector(); int x1,y1; int x2,y2; int mx; int my; int xn, yn; public boolean isLineSelected = false; public boolean mousePressed=false; public boolean mouseReleased=false; public boolean mouseDragged=false; public Point2D lineStart; public Point2D lineEnd; public Rectangle r; public Point2D pointSelected; public Line2D line; public boolean is;

// boolean isMouseDraggingBox = false;

public DrawPanel() {
setBackground(Color.white);
addMouseMotionListener(this);
addMouseListener(this);
}

public void setDrawMode(int mode) {
switch (mode) {
  case LINES:
  case POINTS:
    this.mode = mode;
    break;
  default:
    throw new IllegalArgumentException();
}
}

public void deleteLine() { x1=y1=x2=y2=0;

repaint();

}

@SuppressWarnings("unchecked")
public void mouseDragged(MouseEvent e) {
    e.consume();

    switch (mode) {
        case LINES:

            x2 = e.getX();
            y2 = e.getY();

           // System.out.println(x2+"in dragging of LINES");
           // System.out.println(y2+"in dragging of LINES");
          //  System.out.println(" dragging LINES over");
            break;
        case POINTS:
        default:

            colors.addElement(getForeground());
            lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY()));
            x1 = e.getX();
            y1 = e.getY();

           // System.out.println(x1+"in dragging of LINES");
          //  System.out.println(y1+"in dragging of LINES");
         //   System.out.println(" dragging default over");

            break;
    }
    repaint();
}

public void mouseMoved(MouseEvent e) {
}

@SuppressWarnings("unchecked")
public void mousePressed(MouseEvent e) {

    e.consume();
    System.out.println("Mouse Pressed");
    mousePressed=true;
    switch (mode) {
        case LINES:
            //System.out.println("Mouse Pressed LINES case "+isLineSelected);

            x1 = e.getX();
            y1 = e.getY();
            x2 = -1;

           // System.out.println(x1+"in pressed of LINES");
           // System.out.println(y1+"in pressed of LINES");
          //  System.out.println(" Pressed LINES over");
            break;
        case POINTS:
        default:
        //  System.out.println("Mouse Pressed default case "+isLineSelected);

            colors.addElement(getForeground());
            lines.addElement(new Rectangle(e.getX(), e.getY(), -1, -1));
            x1 = e.getX();
            y1 = e.getY();


               repaint();

           // System.out.println(x1+"in pressed of Default");
          //  System.out.println(y1+"in pressed of Default");
          //  System.out.println(" Pressed default over");
           break;
}


}

@SuppressWarnings("unchecked")
public void mouseReleased(MouseEvent e) {
    e.consume();
    System.out.println("Mouse Released");
    mouseReleased=true;
    xn=e.getX();
    yn=e.getY();
    switch (mode) {
        case LINES:
            //System.out.println("Mouse Released LINES case"+isLineSelected);

            colors.addElement(getForeground());
            lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY()));
            x2 = -1;

            //System.out.println(x1+"in released of LINES");
          //  System.out.println(y1+"in released of LINES");
          //  System.out.println(" Released LINES over");
            //isMouseDraggingBox = false;
            break;
        case POINTS:
        default:
            break;
    }
    repaint();
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public void mouseClicked(MouseEvent e) {
    e.consume();
    //Point2D lineStart;
    //Point2D lineEnd;
    //mouseReleased=false;
    //mousePressed=false;
      System.out.println("Mouse Clicked");
      mx=e.getX();
      my=e.getY();
      pointSelected=new Point2D.Double(mx,my);
     // System.out.println("Size of vector of lines is"+lines.size());
      for(int i=0;i<lines.size();i++)
      {

        r=(Rectangle)lines.elementAt(i);
        if(r.width!=-1)
        {
        x1=r.x;
        y1=r.y;
        x2=r.width;
        y2=r.height;
        lineStart = new Point2D.Double(x1, y1);
        lineEnd = new Point2D.Double(x2, y2);

       // pointSelected=new Point(mx,my);
        //System.out.println("linestart: "+lineStart);
        //System.out.println("slineend: "+lineEnd);
      /*  if(lineStart.equals(lineEnd))
        {
            i=0;
            continue;
        }*/

              line = new Line2D.Double(lineStart, lineEnd);

         if ( line.ptSegDist(mx, my)==0.0 || line.contains(pointSelected) ) {
            isLineSelected=true;

            break;
         }
        }

        }
         System.out.println("isLineselected: "+isLineSelected);

         if(isLineSelected){
             //deleteLine();
            //System.out.println("Start point "+ x1+ ", "+y1);
            //System.out.println("EndPoint "+x2+", "+y2);

         }
    }

    //Point lineStart = new Point(x1, y1);
    //Point lineEnd = new Point(x2, y2);






public void paint(Graphics g) {
int np = lines.size();

/* draw the current lines */
if(mousePressed){
    g.drawString("("+x1+","+y1+")",x1,y1);
}
if(mouseReleased)
{
    g.drawString("("+xn+","+yn+")",xn,yn);
}
g.setColor(getForeground());
for (int i=0; i < np; i++) {
    Rectangle p = (Rectangle)lines.elementAt(i);
    g.setColor((Color)colors.elementAt(i));
    if (p.width != -1) {
    g.drawLine(p.x, p.y, p.width, p.height);
    } else {
    g.drawLine(p.x, p.y, p.x, p.y);
    }
}
if (mode == LINES) {
    g.setColor(getForeground());
    if (x2 != -1) {
            g.drawLine(x1, y1, x2, y2);
    }
}
}

}

what should i do to get correct endpoints....

View Answers









Related Tutorials/Questions & Answers:
Applets?
Applets?  applets are using now a days   Please visit the following link: Applet Tutorials
APPLETS - Security
APPLETS  In java applets sun.misc is not working can any body help me for the alternate solution. while using new sun.misc.BASE64Encoder().encode(enc); i got runtime error... java.security.AccessControlException
Advertisements
applets
Applets
Applets
Applets
Applications and Applets
Applications and Applets       Now a days, Java is widely used for applications and applets. The code... the user's browser. Applets can be very useful, user friendly programs or can
Applications and Applets
Applications and Applets       Now a days, Java is widely used for applications and applets. The code... the user's browser. Applets can be very useful, user friendly programs or can
Servlet v/s Applets - Servlet Interview Questions
Servlet v/s Applets  what is difference between servlets & applets
Programming using applets and oracle - Applet
Programming using applets and oracle  Develop an Online Examination software in java using applets as front end and oracle as back end. Software should handle at least ten users at a time. There are Multiple Choice questions
Free Java Applets
Free Java Applets       There are many free java applets available on the web that you can use in your website. These applets includes menu, clock, slide show, image animation
Applets in Java
Applet is a Java program embedded within HTML pages. Java applets is compatible with almost all the web browsers like Mozilla Firefox, Google Chrome, Internet explorer, Netscape navigator and others that are java enabled. Applets
Should we go for javascript or applets for online exam project using java
Should we go for javascript or applets for online exam project using java  Hi, I am new to java and i am working on online examination project.As soon as the candidate login successfully a window pop up should appear
java applets - Applet
Jav Applets - Applet
Animation in Applets - Applet
Applets - IDE Questions
java applets - Java Beginners
regarding applets - Java Beginners
Role of Java Applets in Web Application Development
In this article, we are discussing about the role of Java Applets in web application development. As we all know that an Applet is a simple Java program, which is written in Java programming language that extends the java.applet.
Problem with display of images in applets - Applet
I am new to java applets. When i run this code on applet viewer status is displayed that applet not initialized
I am new to java applets. When i run this code on applet viewer status is displayed that applet not initialized  import java.awt.*; import java.awt.event.*; import java.applet.*; public class Textfields extends Applet implements
What is Applet in Java?
computer. Applets are used to make the web site more dynamic and entertaining. The main difference between Java based applications and applets... of Applet? Applets are cross platform and can run on Windows, Mac OS and Linux
Java Applet
Applets are Java programs that run on java enabled web browser. Java applets can run on client servers with some restrictions. Applets are embedded into HTML..._TO_REPLACE_1 There are various advantages of using Java Applets: They can
Applet versus Application
;    Applets as previously described, are the small programs while applications are larger programs. Applets don't have the main method while in an application execution starts with the main method. Applets can run in our
Free Java stuffs
Java apps for cell phonesADS_TO_REPLACE_2 Free Java Applets
&lt;APPLET&gt; Tag Parameters: The &lt;PARAM&gt; Tag
directly to a Java applet. Parameters are to applets what command-line... to applets are: First, add the PARAM tag (with values) to the HTML source file... the Applet These parameter values are retrieved by Java applets using
Java JAR Files
Java JAR Files       A JAR file is a collection of  class files and auxiliary resources associated with applets and applications. The Java Archive (JAR) file format enables
Java Update
Update 2 provides improved behavior for applets in Safari and increased stability... run applets and Web Start applications without waiting for the whole JRE... the requirements of most Java applets and applications and any additional libraries
Java hello world
Java hello world       Java is a high-level object-oriented programming language developed by the Sun Microsystems and is available in different form like JSP, Java Applets, J2EE
Applet
; Introduction Applet is java program that can be embedded into HTML pages. Java applets... restrictions on it. Applet can't access system resources on the local computer. Applets... of Applet:ADS_TO_REPLACE_1 Applets are cross platform and can run on Windows
The Java Applet Viewer
; Applet viewer is a command line program to run Java applets... that inherits the Applet. We generally use web browsers to run applets. Its not always... that acts as a test bed for Java applets. The working of Applet viewer is a bit
The Java Applet Viewer
is a command line program to run Java applets. It is included in the SDK. It helps you... generally use web browsers to run applets. Its not always mandatory to open a Web... as a test bed for Java applets. The working of Applet viewer is a bit different from
Using Applet in JSP
be embedded into HTML pages. Java applets runs on the java enables web browsers... resources on the local computer. Applets are used to make the web site more dynamic
The Java Applet Viewer
; Applet viewer is a command line program to run Java applets... the Applet. We generally use web browsers to run applets. Its not always... that acts as a test bed for Java applets. The working of Applet viewer is a bit
Applet in Java
the java.applet.Applet class Applets are also useful for a small Internet and intranet... are viewed through Java-enabled web browsers. Advantages of Applet: Applets can run on Windows, Linux and Mac OS Applets are secure Almost all web
Java applet
Java applet  what are types of applets
Java basics
Java basics  Differences between applications and applets
java - Java Beginners
Java applets programs coding  Java applets programs coding and example.Thanks in Advance
<APPLET> Tag Parameters: The <PARAM> Tag
Swing In Java
Package in Applet
Package in Applet  How do I use this technique for applets
home page
home page  Develop home page using applets and swings
Java applet
Java applet  How do you set security in applets
Java applet
Java applet   How are the differences between Applets and Applications
Java applet
Java applet  What is the sequence for calling the methods by AWT for applets
java applet
java applet  create applets to accept the personal details and sales details in the database. create jdbc application to connect the applets with the database
What is an Applet - Java Applet Tutorial
Object-Oriented Language: Java / APIs, Java OOPs
does any one have sudoku in java - Java Beginners
does any one have sudoku in java   i need soduko in java using applets

Ads