• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 457
  • Last Modified:

URGENT - Java Applet Application

I have an applet that draws lines on grid using bresenhams algorithm..

is there any way it can be re-coded so it doesnt use an applet?

if so, please tell me how with example code


import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Line extends Applet implements ActionListener {
    private BresLine bres;

    public void init () {
        setLayout (new BorderLayout());
        Panel p = new Panel();
        Button gridButton = new Button("Toggle Grid");
        p.add(gridButton);
        gridButton.addActionListener(this);
        Button lineButton = new Button("Toggle Line");
        p.add(lineButton);
        lineButton.addActionListener(this);
        Button plusButton = new Button("+ Res.");
        p.add(plusButton);
        plusButton.addActionListener(this);
        Button minusButton = new Button("- Res.");
        p.add(minusButton);
        minusButton.addActionListener(this);

        add (p, "South");
        bres = new BresLine();
        add (bres, "Center");

        bres.initBres();
    }

    public void actionPerformed (ActionEvent evt) {
        String arg = evt.getActionCommand();
        if (arg.equals("Toggle Grid") )
           bres.GridToggle();
        else if (arg.equals("Toggle Line") )
           bres.LineToggle();
        else if (arg.equals("+ Res.") )
           bres.IncreaseRes();
        else if (arg.equals("- Res.") )
           bres.DecreaseRes();

        bres.repaint();
    }
}

class BresLine extends Canvas {

    private int x_1, y_1, x_2, y_2;
    private int line_x1, line_y1, line_x2, line_y2;
    private int mypixelwidth, mypixelheight;
    private int clicks;
    private int windowheight, windowwidth;
    private boolean linedrawn;
    private boolean javaline, griddrawn;
    private Graphics g;

    public void paint (Graphics g) {
        Dimension d = getSize();
        windowwidth = d.width;
        windowheight = d.height;
      if (griddrawn)
           drawgrid();
      if (linedrawn)
         myDrawLine (x_1, y_1, x_2, y_2);
    }

    public void drawPixel (int x, int y) {
      g.fillRect (x*mypixelwidth, y*mypixelheight, mypixelwidth, mypixelwidth);
    }

    public void linePosShallow(int x_1, int y_1, int x_2, int y_2) {
      int dx, dy, p, twody, twodydx, x, y, xend;

      dx = Math.abs(x_1 - x_2);
      dy = Math.abs(y_1 - y_2);
      twody = 2 * dy;
      p = twody - dx;
      twodydx = 2 * (dy - dx);

      if (x_1 > x_2) {
         x = x_2;
         y = y_2;
         xend = x_1;
      } else {
         x = x_1;
         y = y_1;
         xend = x_2;
      }
      drawPixel (x, y);

      while (x < xend) {
         x++;
         if (p < 0)
            p += twody;
         else {
            y++;
            p += twodydx;
         }
         drawPixel (x, y);
      }
    }

    public void linePosSteep(int x_1, int y_1, int x_2, int y_2) {
      int dx, dy, p, twodx, twodxdy, x, y, yend;

      dx = Math.abs(x_1 - x_2);
      dy = Math.abs(y_1 - y_2);
      twodx = 2 * dx;
      p = twodx - dy;
      twodxdy = 2 * (dx - dy);
      y = y_1;            // Dummy initialization
      yend = y_2;            // Ditto
      x = x_1;

      if (x_1 == x_2)            // Vertical line
      {
         if (y_2 > y_1) {
            yend = y_2;
            y = y_1;
         } else {
            yend = y_1;
            y = y_2;
         }
         for (; y <= yend; y++)
            drawPixel(x_1, y);
      }
      if (x_1 > x_2) {
         x = x_2;
         y = y_2;
         yend = y_1;
      }
      if (x_1 < x_2) {
         x = x_1;
         y = y_1;
         yend = y_2;
      }
      if (x_1 != x_2)
      {
         drawPixel (x_1, y_1);
         while (y < yend) {
            y++;
            if (p < 0)
               p += twodx;
            else {
               x++;
               p += twodxdy;
            }
            drawPixel (x, y);
         }
      }
    }

    public void lineNegShallow (int x_1, int y_1, int x_2, int y_2) {
      int dx, dy, p, twody, twodydx, x, y, xend;

      dx = Math.abs(x_2 - x_1);
      dy = Math.abs(y_2 - y_1);
      p = 2 * dy - dx;
      twody = 2 * dy;
      twodydx = 2 * (dy - dx);

      if (x_1 > x_2) {
         x = x_2;
         y = y_2;
         xend = x_1;
      } else {
         x = x_1;
         y = y_1;
         xend = x_2;
      }
      drawPixel (x, y);

      while (x < xend) {
         x++;
         if (p < 0)
            p += twody;
         else {
            y--;
            p += twodydx;
         }
         drawPixel (x, y);
      }
    }

    public void lineNegSteep (int x_1, int y_1, int x_2, int y_2) {
      int dx, dy, p, twodx, twodxdy, x, y, yend;

      dx = Math.abs (x_2 - x_1);
      dy = Math.abs (y_2 - y_1);
      p = 2 * dx - dy;
      twodx = 2 * dx;
      twodxdy = 2 * (dx - dy);

      if (x_1 > x_2) {
         x = x_2;
         y = y_2;
         yend = y_1;
      } else {
         x = x_1;
         y = y_1;
         yend = y_2;
      }
      drawPixel (x, y);

      while (y > yend) {
         y--;
         if (p < 0)
            p += twodx;
         else {
            x++;
            p += twodxdy;
         }
         drawPixel (x, y);
      }
    }

    public void myDrawLine (int x_1, int y_1, int x_2, int y_2) {
      double deltax, deltay;                  // For Bresenham's
      double slope;                        // Ditto

      deltax = x_2 - x_1;            // Change in x
      deltay = y_2 - y_1;            // Change in y

      if (deltax == 0)            // Need to avoid div by zero
         linePosSteep(x_1, y_1, x_2, y_2);

      else {
         slope = deltay / deltax;

         if (slope > 0.0) {
            if (slope > 1.0)
               linePosSteep(x_1, y_1, x_2, y_2);
            else
               linePosShallow(x_1, y_1, x_2, y_2);
         } else {
            if (slope > -1.0)
               lineNegShallow(x_1, y_1, x_2, y_2);
            else
               lineNegSteep(x_1, y_1, x_2, y_2);
         }
      }
    }

    public void initBres() {
      int i;

      this.setBackground (Color.white);
      g = this.getGraphics();
      g.setColor (Color.black);
      mypixelwidth = mypixelheight = 11;
      clicks = 0;
      linedrawn = false;
      griddrawn = javaline = true;
    }

    public boolean mouseDown (Event e, int x, int y) {
      if (clicks == 0) {

         if (linedrawn) {
              g.setColor (Color.white);
              g.fillRect(0, 0, windowwidth, windowheight);
              g.setColor (Color.black);
           }
         line_x1 = x;
         x_1 = x / mypixelwidth;
         line_y1 = y;
         y_1 = y / mypixelheight;
         drawPixel(x_1, y_1);
           if (griddrawn) drawgrid();
      }
      else {
         line_x2 = x;
         x_2 = x / mypixelwidth;
         line_y2 = y;
         y_2 = y / mypixelheight;
         drawPixel (x_1, y_1);            // XOR it out
         myDrawLine (x_1, y_1, x_2, y_2);
           if (javaline) {
              g.setColor(Color.red);
              g.drawLine (line_x1, line_y1, line_x2, line_y2);
                         // Draw line in "Java code"
              g.setColor(Color.black);
           }
         linedrawn = true;
      }
      clicks = (clicks + 1) % 2;
      return true;
    }

    public void GridToggle() {
        griddrawn = !griddrawn;
    }

    public void LineToggle() {
        javaline = !javaline;
    }

    public void IncreaseRes() {
        if (mypixelheight > 3) { // Can't get better otherwise
           mypixelheight -= 2;
           mypixelwidth -= 2;
        }
    }

    public void DecreaseRes() {
        if (mypixelheight < 100) { // Gotta be a limit
           mypixelheight += 2;
           mypixelwidth += 2;
        }
    }

    public void drawgrid () {
        int i;

        g.setColor (Color.black);
        for (i=mypixelwidth; i < windowwidth; i+=mypixelwidth)
          g.drawLine (i, 0, i, windowheight);
        for (i=mypixelheight; i < windowheight; i+=mypixelheight)
          g.drawLine (0, i, windowwidth, i);
    }
}
0
ellandrd
Asked:
ellandrd
  • 6
  • 5
1 Solution
 
sudhakar_koundinyaCommented:
yes

change this line to
public class Line extends Applet implements ActionListener {

public class Line extends Frame implements ActionListener {

change this line to
  public void init () {

public Line();

introduce main method

public static void main(String s[])
{
  Line line=new Line();
    line.setSize(100,100);
   line.setVisible(true);
}
0
 
ellandrdAuthor Commented:
still cant get it to work... it create a frame etc etc, but my lines dont get drawn
0
 
sudhakar_koundinyaCommented:
Ok

here is another idea

Regards
Sudhakar

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Line extends Applet implements ActionListener {
    private BresLine bres;

    public void init () {
        setLayout (new BorderLayout());
        Panel p = new Panel();
        Button gridButton = new Button("Toggle Grid");
        p.add(gridButton);
        gridButton.addActionListener(this);
        Button lineButton = new Button("Toggle Line");
        p.add(lineButton);
        lineButton.addActionListener(this);
        Button plusButton = new Button("+ Res.");
        p.add(plusButton);
        plusButton.addActionListener(this);
        Button minusButton = new Button("- Res.");
        p.add(minusButton);
        minusButton.addActionListener(this);

        add (p, "South");
        bres = new BresLine();
        add (bres, "Center");

        bres.initBres();
    }

    public void actionPerformed (ActionEvent evt) {
        String arg = evt.getActionCommand();
        if (arg.equals("Toggle Grid") )
           bres.GridToggle();
        else if (arg.equals("Toggle Line") )
           bres.LineToggle();
        else if (arg.equals("+ Res.") )
           bres.IncreaseRes();
        else if (arg.equals("- Res.") )
           bres.DecreaseRes();

        bres.repaint();
    }
}

class BresLine extends Canvas {

    private int x_1, y_1, x_2, y_2;
    private int line_x1, line_y1, line_x2, line_y2;
    private int mypixelwidth, mypixelheight;
    private int clicks;
    private int windowheight, windowwidth;
    private boolean linedrawn;
    private boolean javaline, griddrawn;
    private Graphics g;

    public void paint (Graphics g) {
            this.g=g;
        Dimension d = getSize();
        windowwidth = d.width;
        windowheight = d.height;
     if (griddrawn)
           drawgrid();
     if (linedrawn)
        myDrawLine (x_1, y_1, x_2, y_2);
    }

    public void drawPixel (int x, int y) {
     g.fillRect (x*mypixelwidth, y*mypixelheight, mypixelwidth, mypixelwidth);
    }

    public void linePosShallow(int x_1, int y_1, int x_2, int y_2) {
     int dx, dy, p, twody, twodydx, x, y, xend;

     dx = Math.abs(x_1 - x_2);
     dy = Math.abs(y_1 - y_2);
     twody = 2 * dy;
     p = twody - dx;
     twodydx = 2 * (dy - dx);

     if (x_1 > x_2) {
        x = x_2;
        y = y_2;
        xend = x_1;
     } else {
        x = x_1;
        y = y_1;
        xend = x_2;
     }
     drawPixel (x, y);

     while (x < xend) {
        x++;
        if (p < 0)
          p += twody;
        else {
          y++;
          p += twodydx;
        }
        drawPixel (x, y);
     }
    }

    public void linePosSteep(int x_1, int y_1, int x_2, int y_2) {
     int dx, dy, p, twodx, twodxdy, x, y, yend;

     dx = Math.abs(x_1 - x_2);
     dy = Math.abs(y_1 - y_2);
     twodx = 2 * dx;
     p = twodx - dy;
     twodxdy = 2 * (dx - dy);
     y = y_1;          // Dummy initialization
     yend = y_2;          // Ditto
     x = x_1;

     if (x_1 == x_2)          // Vertical line
     {
        if (y_2 > y_1) {
          yend = y_2;
          y = y_1;
        } else {
          yend = y_1;
          y = y_2;
        }
        for (; y <= yend; y++)
          drawPixel(x_1, y);
     }
     if (x_1 > x_2) {
        x = x_2;
        y = y_2;
        yend = y_1;
     }
     if (x_1 < x_2) {
        x = x_1;
        y = y_1;
        yend = y_2;
     }
     if (x_1 != x_2)
     {
        drawPixel (x_1, y_1);
        while (y < yend) {
          y++;
          if (p < 0)
             p += twodx;
          else {
             x++;
             p += twodxdy;
          }
          drawPixel (x, y);
        }
     }
    }

    public void lineNegShallow (int x_1, int y_1, int x_2, int y_2) {
     int dx, dy, p, twody, twodydx, x, y, xend;

     dx = Math.abs(x_2 - x_1);
     dy = Math.abs(y_2 - y_1);
     p = 2 * dy - dx;
     twody = 2 * dy;
     twodydx = 2 * (dy - dx);

     if (x_1 > x_2) {
        x = x_2;
        y = y_2;
        xend = x_1;
     } else {
        x = x_1;
        y = y_1;
        xend = x_2;
     }
     drawPixel (x, y);

     while (x < xend) {
        x++;
        if (p < 0)
          p += twody;
        else {
          y--;
          p += twodydx;
        }
        drawPixel (x, y);
     }
    }

    public void lineNegSteep (int x_1, int y_1, int x_2, int y_2) {
     int dx, dy, p, twodx, twodxdy, x, y, yend;

     dx = Math.abs (x_2 - x_1);
     dy = Math.abs (y_2 - y_1);
     p = 2 * dx - dy;
     twodx = 2 * dx;
     twodxdy = 2 * (dx - dy);

     if (x_1 > x_2) {
        x = x_2;
        y = y_2;
        yend = y_1;
     } else {
        x = x_1;
        y = y_1;
        yend = y_2;
     }
     drawPixel (x, y);

     while (y > yend) {
        y--;
        if (p < 0)
          p += twodx;
        else {
          x++;
          p += twodxdy;
        }
        drawPixel (x, y);
     }
    }

    public void myDrawLine (int x_1, int y_1, int x_2, int y_2) {
     double deltax, deltay;               // For Bresenham's
     double slope;                    // Ditto

     deltax = x_2 - x_1;          // Change in x
     deltay = y_2 - y_1;          // Change in y

     if (deltax == 0)          // Need to avoid div by zero
        linePosSteep(x_1, y_1, x_2, y_2);

     else {
        slope = deltay / deltax;

        if (slope > 0.0) {
          if (slope > 1.0)
             linePosSteep(x_1, y_1, x_2, y_2);
          else
             linePosShallow(x_1, y_1, x_2, y_2);
        } else {
          if (slope > -1.0)
             lineNegShallow(x_1, y_1, x_2, y_2);
          else
             lineNegSteep(x_1, y_1, x_2, y_2);
        }
     }
    }

    public void initBres() {
     int i;

     this.setBackground (Color.white);
     g = this.getGraphics();
       if(g!=null)
            {
     g.setColor (Color.black);
            }
     mypixelwidth = mypixelheight = 11;
     clicks = 0;
     linedrawn = false;
     griddrawn = javaline = true;
    }

    public boolean mouseDown (Event e, int x, int y) {
     if (clicks == 0) {

        if (linedrawn) {
              g.setColor (Color.white);
              g.fillRect(0, 0, windowwidth, windowheight);
              g.setColor (Color.black);
           }
        line_x1 = x;
        x_1 = x / mypixelwidth;
        line_y1 = y;
        y_1 = y / mypixelheight;
        drawPixel(x_1, y_1);
           if (griddrawn) drawgrid();
                      repaint();
     }
     else {
        line_x2 = x;
        x_2 = x / mypixelwidth;
        line_y2 = y;
        y_2 = y / mypixelheight;
        drawPixel (x_1, y_1);          // XOR it out
        myDrawLine (x_1, y_1, x_2, y_2);
           if (javaline) {
              g.setColor(Color.red);
              g.drawLine (line_x1, line_y1, line_x2, line_y2);
                           repaint();
                         // Draw line in "Java code"
              g.setColor(Color.black);
           }
        linedrawn = true;

     }
     clicks = (clicks + 1) % 2;


     return true;
    }

    public void GridToggle() {
        griddrawn = !griddrawn;
    }

    public void LineToggle() {
        javaline = !javaline;
    }

    public void IncreaseRes() {
        if (mypixelheight > 3) { // Can't get better otherwise
           mypixelheight -= 2;
           mypixelwidth -= 2;
        }
    }

    public void DecreaseRes() {
        if (mypixelheight < 100) { // Gotta be a limit
           mypixelheight += 2;
           mypixelwidth += 2;
        }
    }

    public void drawgrid () {
        int i;

        g.setColor (Color.black);
        for (i=mypixelwidth; i < windowwidth; i+=mypixelwidth)
         g.drawLine (i, 0, i, windowheight);
        for (i=mypixelheight; i < windowheight; i+=mypixelheight)
         g.drawLine (0, i, windowwidth, i);
    }
}

 class AppletFrame {
  public static void main(String[] args) {
    Line myApplet = new Line(); // define applet of interest
    Frame myFrame = new Frame("Applet Holder"); // create frame with title


    // Call applet's init method (since Java App does not
    // call it as a browser automatically does)
    myApplet.init();      

    // add applet to the frame
    myFrame.add(myApplet, BorderLayout.CENTER);
    myFrame.setSize(500,500);
    myFrame.setVisible(true); // usual step to make frame visible

      myFrame.addWindowListener (
                  new WindowAdapter()
    {
      public void windowClosing (
                   WindowEvent event)
      {
        System.exit(0);
      }
    });

  } // end main
} // end class
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ellandrdAuthor Commented:
ok got it working but i cant close the frame?
how do i do that?
0
 
sudhakar_koundinyaCommented:
see my above code
Something like below


myFrame.addWindowListener (
                  new WindowAdapter()
    {
      public void windowClosing (
                   WindowEvent event)
      {
        System.exit(0);
      }
    });
0
 
sudhakar_koundinyaCommented:
basically the second idea is I am adding applet itself to the container.

Using this model, with out changing the Applet code, you can create a standalone application for applet

Regards
Sudhakar
0
 
ellandrdAuthor Commented:
oh well since my last post, i got it working, but stil not closing?

well try your last code and see what happens
0
 
sudhakar_koundinyaCommented:
>> i got it working, but stil not closing?

As said earlier in your main method

you should add this code

line.addWindowListener (
                  new WindowAdapter()
    {
      public void windowClosing (
                   WindowEvent event)
      {
        System.exit(0);
      }
    });


0
 
ellandrdAuthor Commented:
ok, i'll bring you up to date on what i am trying to do?

as you can see, im trying to drawlines on draw using bresenham alpgorithm...

what the code above is, an example becasue i cant get my lines to draw on grid my own application.

so i went searching google for examples, and got the above one.

maybe yopu can help me get my own application working?

im only learning java and another guys on EE didnt really help me much.

if you wnat i will award you the points for this and open another one to you to help with me else i'll keep posting here?

wnat you think?

alots of points avaiable today!!
0
 
sudhakar_koundinyaCommented:
>>alots of points avaiable today!!

It is your wish. But I try to help always, if I can :-)

Regards
Sudhakar
0
 
ellandrdAuthor Commented:
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now