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 - SecurityAPPLETS 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
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
Programming using applets and oracle - AppletProgramming 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 JavaApplet 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 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.
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 AppletApplets 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
<APPLET> Tag Parameters: The <PARAM> 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 basicsJava basics Differences between applications and
applets java - Java BeginnersJava
applets programs coding Java
applets programs coding and example.Thanks in Advance
home pagehome page Develop home page using
applets and swings
Java appletJava applet How do you set security in
applets Java appletJava applet How are the differences between
Applets and Applications
Java appletJava applet What is the sequence for calling the methods by AWT for
applets java appletjava applet create
applets to accept the personal details and sales details in the database. create jdbc application to connect the
applets with the database