add javascript into html page

Discussion in 'JavaScript' started by money4leads, Jan 18, 2013.

  1. #1
    hello
    I need to use a javascript for drawing, I got the code but I don't know how to call the script in a html page, I need someone to show me how

    here the code:
    
    
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Insets;
    import java.awt.Point;
    import java.awt.event.MouseEvent;
    
    import javax.swing.BorderFactory;
    import javax.swing.BoxLayout;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.event.MouseInputListener;
    
    public class CoordinatesDemo {
      private JLabel label;
    
      private Point clickPoint, cursorPoint;
    
      private void buildUI(Container container) {
        container.setLayout(new BoxLayout(container, BoxLayout.PAGE_AXIS));
    
        CoordinateArea coordinateArea = new CoordinateArea(this);
        container.add(coordinateArea);
    
        label = new JLabel();
        resetLabel();
        container.add(label);
    
        coordinateArea.setAlignmentX(Component.LEFT_ALIGNMENT);
        label.setAlignmentX(Component.LEFT_ALIGNMENT); // redundant
      }
    
      public void updateCursorLocation(int x, int y) {
        if (x < 0 || y < 0) {
          cursorPoint = null;
          updateLabel();
          return;
        }
    
        if (cursorPoint == null) {
          cursorPoint = new Point();
        }
    
        cursorPoint.x = x;
        cursorPoint.y = y;
        updateLabel();
      }
    
      public void updateClickPoint(Point p) {
        clickPoint = p;
        updateLabel();
      }
    
      public void resetLabel() {
        cursorPoint = null;
        updateLabel();
      }
    
      protected void updateLabel() {
        String text = "";
    
        if ((clickPoint == null) && (cursorPoint == null)) {
          text = "Click or move the cursor within the framed area.";
        } else {
    
          if (clickPoint != null) {
            text += "The last click was at (" + clickPoint.x + ", " + clickPoint.y + "). ";
          }
    
          if (cursorPoint != null) {
            text += "The cursor is at (" + cursorPoint.x + ", " + cursorPoint.y + "). ";
          }
        }
    
        label.setText(text);
      }
    
      public static void main(String[] args) {
        JFrame frame = new JFrame("CoordinatesDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        CoordinatesDemo controller = new CoordinatesDemo();
        controller.buildUI(frame.getContentPane());
    
        frame.pack();
        frame.setVisible(true);
      }
    
      public static class CoordinateArea extends JComponent implements MouseInputListener {
        Point point = null;
    
        CoordinatesDemo controller;
    
        Dimension preferredSize = new Dimension(400, 75);
    
        Color gridColor;
    
        public CoordinateArea(CoordinatesDemo controller) {
          this.controller = controller;
    
          // Add a border of 5 pixels at the left and bottom,
          // and 1 pixel at the top and right.
          setBorder(BorderFactory.createMatteBorder(1, 5, 5, 1, Color.RED));
    
          addMouseListener(this);
          addMouseMotionListener(this);
          setBackground(Color.WHITE);
          setOpaque(true);
        }
    
        public Dimension getPreferredSize() {
          return preferredSize;
        }
    
        protected void paintComponent(Graphics g) {
          // Paint background if we're opaque.
          if (isOpaque()) {
            g.setColor(getBackground());
            g.fillRect(0, 0, getWidth(), getHeight());
          }
    
          // Paint 20x20 grid.
          g.setColor(Color.GRAY);
          drawGrid(g, 20);
    
          // If user has chosen a point, paint a small dot on top.
          if (point != null) {
            g.setColor(getForeground());
            g.fillRect(point.x - 3, point.y - 3, 7, 7);
          }
        }
    
        // Draws a 20x20 grid using the current color.
        private void drawGrid(Graphics g, int gridSpace) {
          Insets insets = getInsets();
          int firstX = insets.left;
          int firstY = insets.top;
          int lastX = getWidth() - insets.right;
          int lastY = getHeight() - insets.bottom;
    
          // Draw vertical lines.
          int x = firstX;
          while (x < lastX) {
            g.drawLine(x, firstY, x, lastY);
            x += gridSpace;
          }
    
          // Draw horizontal lines.
          int y = firstY;
          while (y < lastY) {
            g.drawLine(firstX, y, lastX, y);
            y += gridSpace;
          }
        }
    
        // Methods required by the MouseInputListener interface.
        public void mouseClicked(MouseEvent e) {
          int x = e.getX();
          int y = e.getY();
          if (point == null) {
            point = new Point(x, y);
          } else {
            point.x = x;
            point.y = y;
          }
          controller.updateClickPoint(point);
          repaint();
        }
    
        public void mouseMoved(MouseEvent e) {
          controller.updateCursorLocation(e.getX(), e.getY());
        }
    
        public void mouseExited(MouseEvent e) {
          controller.resetLabel();
        }
    
        public void mouseReleased(MouseEvent e) {
        }
    
        public void mouseEntered(MouseEvent e) {
        }
    
        public void mousePressed(MouseEvent e) {
        }
    
        public void mouseDragged(MouseEvent e) {
        }
      }
    }
    
    
    
    Code (markup):
     
    money4leads, Jan 18, 2013 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    That's not Javascript, it's Java. Post one directory back - in Programming. (Most people who can do Javascript know nothing about Java applets.)
     
    Rukbat, Jan 18, 2013 IP
  3. money4leads

    money4leads Well-Known Member

    Messages:
    117
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #3
    thanks, ok then how to create my html page with this?
     
    money4leads, Jan 19, 2013 IP
  4. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #4
    I don't do Java applets, so I can't help you. (I was reading this forum because I do Javascript.)
     
    Rukbat, Jan 19, 2013 IP
  5. dixcoder

    dixcoder Member

    Messages:
    71
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    26
    #5
    If you have JAVA installed then you compile this code and generate applet ... then after that it will give you class file once you have class file then you can add in the html
    from here you can learn how to add applet in your html
    http://www.w3schools.com/tags/tag_applet.asp
     
    dixcoder, Jan 20, 2013 IP
  6. temp2

    temp2 Well-Known Member

    Messages:
    1,231
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    150
    Digital Goods:
    2
    #6
    what you're wanting to draw? HTML5 + CSS3 is best option this moment
     
    temp2, Jan 20, 2013 IP
  7. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #7
    HTML5 is the worst option regardless of what you're trying to do, but especially if you're trying to develop a website. It's the worst of 1997. Onward to the past.
     
    Rukbat, Jan 20, 2013 IP
  8. manisha mishra

    manisha mishra Greenhorn

    Messages:
    29
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    8
    #8
    I think we can add this code on HTML. But before that you have to create a function in the head part. And after that you have to write that code on the body of HTML. As we are writing java script you have to write Document.write then code for displaying something. For more info you can go through different sites to know more.
     
    manisha mishra, Jan 21, 2013 IP