?
Solved

URGENT - Java Applet Application

Posted on 2005-03-09
11
Medium Priority
?
455 Views
Last Modified: 2013-11-23
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
Comment
Question by:ellandrd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 13494510
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
 
LVL 16

Author Comment

by:ellandrd
ID: 13494700
still cant get it to work... it create a frame etc etc, but my lines dont get drawn
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 13494708
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 16

Author Comment

by:ellandrd
ID: 13494709
ok got it working but i cant close the frame?
how do i do that?
0
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 2000 total points
ID: 13494711
see my above code
Something like below


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

Expert Comment

by:sudhakar_koundinya
ID: 13494723
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
 
LVL 16

Author Comment

by:ellandrd
ID: 13494724
oh well since my last post, i got it working, but stil not closing?

well try your last code and see what happens
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 13494740
>> 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
 
LVL 16

Author Comment

by:ellandrd
ID: 13494753
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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 13494770
>>alots of points avaiable today!!

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

Regards
Sudhakar
0
 
LVL 16

Author Comment

by:ellandrd
ID: 13494805
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question