Link to home
Start Free TrialLog in
Avatar of BrianGEFF719
BrianGEFF719Flag for United States of America

asked on

Simple JavaCode

Can someone provide me with code that takes a simple "canvas" as a java applet and allows the user to click two places on that canvas and draws a line between those two click points?


Brian
Avatar of BrianGEFF719
BrianGEFF719
Flag of United States of America image

ASKER

This needs to be as an applet.

Brian
Avatar of DeanHorak
DeanHorak

This should be pretty close to what you're looking for

http://www.faqs.org/docs/javap/source/RubberBand.java
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
check this program modify according to your need


import java.applet.*;
import java.awt.*;
import java.awt.Event.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
public class MouseMotion  extends Applet implements MouseListener,MouseMotionListener

{

Point p;

public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}
      public void mouseClicked(MouseEvent arg0) {
            // TODO Auto-generated method stub
            
      }

      public void mousePressed(MouseEvent arg0) {
            p = arg0.getPoint();
            repaint();
            // TODO Auto-generated method stub
            
      }

      public void mouseReleased(MouseEvent arg0) {
            p = null;
            repaint();
            // TODO Auto-generated method stub
            
      }

      public void mouseEntered(MouseEvent arg0) {
            // TODO Auto-generated method stub
            
      }

      public void mouseExited(MouseEvent arg0) {
            // TODO Auto-generated method stub
            
      }

      public void mouseDragged(MouseEvent arg0) {
            // TODO Auto-generated method stub
            
      }

      public void mouseMoved(MouseEvent arg0) {
            // TODO Auto-generated method stub
            
      }
      public void paint(Graphics g)
      {
            if(p != null)
                  
            {
                  
                  Dimension d = getSize();
                  int xc = d.width/2;
                  int yc = d.height/2;
                  g.drawLine(xc,yc,p.x,p.y);
            }
      }
      

}


here is the tutorial


http://www.dgp.toronto.edu/~mjmcguff/learn/java/08-painting/

How do I use netbeans to compile this stuff? I tried to make a general applet, but I'm having a hard time. How can I create a new project an import some of that sample .java code.

Brian
doesn't it have an import option?
In Netbeans, goto File -> New Project -> General -> Java Project with existing sources
q. will be finalized later this week. thanks for patience.