Results 1 to 6 of 6
  1. #1
    rebelstone is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Question covert code from a panel to an applet

    i have this code here and ive really tried to make it work in an applet but no success. pls help me out.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class DrawRectRx extends JPanel
    {
    private int pointCount =0;
    private Point points[] = new Point[100];
    private Point points2[] = new Point[100];
    private Point start = new Point();
    private Point end = new Point();
    Rectangle rect = new Rectangle();

    public DrawRectRx()
    {
    addMouseMotionListener( new MouseMotionAdapter()
    {
    public void mouseDragged(MouseEvent ev)
    {
    end = ev.getPoint();
    rect.setFrameFromDiagonal(start, end);
    repaint();
    }//end mouse drag
    });
    addMouseListener(new MouseAdapter()
    {
    public void mousePressed(MouseEvent e)
    {
    start = e.getPoint();
    }

    public void mouseReleased(MouseEvent ev)
    {
    points[pointCount] = start;
    points2[pointCount] = ev.getPoint();
    pointCount++;
    rect.setFrameFromDiagonal(start, start);
    repaint();
    }
    });
    }

    protected void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,
    RenderingHints.VALUE_ANTIALIAS_ON);
    // Draw line being dragged.
    g2.setPaint(Color.red);
    g2.draw(rect);
    // Draw lines between points in arrays.
    g2.setPaint(Color.blue);
    Rectangle r = new Rectangle();
    for (int i =0; i < pointCount; i++)
    {
    r.setFrameFromDiagonal(points[i], points2[i]);
    g2.fill(r);
    }
    }

    public static void main(String[] args)
    {
    DrawRectRx test = new DrawRectRx();
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(test);
    f.setSize(400,400);
    f.setLocation(200,200);
    f.setVisible(true);
    }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default

    ive really tried
    I don't see any usage of Applet in that code. What have you tried?
    Have you read about how Applets work?
    What class does Applet extend?
    Applet's do NOT use a main() method.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    // <applet code="DrawRectApplet" width="400" height="400"></applet>
    // at the prompt: >appletviewer DrawRectApplet.java
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class DrawRectApplet extends JApplet
    {
        public void init()
        {
            setLayout(new BorderLayout());
            add(new DrawRectRx());
        }
    
        /** Convenience method useful for development. */
        public static void main(String[] args)
        {
            DrawRectApplet applet = new DrawRectApplet();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(applet);
            f.setSize(400,400);
            f.setLocation(200,200);
            applet.init();
            f.setVisible(true);
        }
    }

  4. #4
    rebelstone is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Default

    pls give me the full code to this. i dint understand ur code hardwired

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    // <applet code="DrawRectApplet" width="400" height="400"></applet>
    // at the prompt: >appletviewer DrawRectApplet.java
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class DrawRectApplet extends JApplet
    {
        public void init()
        {
            setLayout(new BorderLayout());
            add(new DrawRectRx());
        }
    
        /** Convenience method useful for development. */
        public static void main(String[] args)
        {
            DrawRectApplet applet = new DrawRectApplet();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(applet);
            f.setSize(400,400);
            f.setLocation(200,200);
            applet.init();
            f.setVisible(true);
        }
    }
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class DrawRectRx extends JPanel
    {
        private int pointCount =0;
        private Point points[]  = new Point[100];
        private Point points2[] = new Point[100];
        private Point start = new Point();
        private Point end   = new Point();
        Rectangle rect = new Rectangle();
    
        public DrawRectRx()
        {
            addMouseMotionListener( new MouseMotionAdapter()
            {
                public void mouseDragged(MouseEvent ev)
                {
                    end = ev.getPoint();
                    rect.setFrameFromDiagonal(start, end);
                    repaint();
                }//end mouse drag
            });
            addMouseListener(new MouseAdapter()
            {
                public void mousePressed(MouseEvent e)
                {
                    start = e.getPoint();
                }
    
                public void mouseReleased(MouseEvent ev)
                {
                    points[pointCount] = start;
                    points2[pointCount] = ev.getPoint();
                    pointCount++;
                    rect.setFrameFromDiagonal(start, start);
                    repaint();
                }
            });
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            // Draw line being dragged.
            g2.setPaint(Color.red);
            g2.draw(rect);
            // Draw lines between points in arrays.
            g2.setPaint(Color.blue);
            Rectangle r = new Rectangle();
            for (int i =0; i < pointCount; i++) {
                r.setFrameFromDiagonal(points[i], points2[i]);
                g2.fill(r);
            }
        }
    	
        public static void main(String[] args) {
            DrawRectRx test = new DrawRectRx();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test);
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

  6. #6
    rebelstone is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Default

    hardwired pls. i need the full code of the program so that i can run the program on an applet n not a panel

Similar Threads

  1. add image on panel
    By samiksha.goel in forum AWT / Swing
    Replies: 4
    Last Post: 08-02-2008, 07:38 PM
  2. Undoable Drawing Panel
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 07:52 PM
  3. Problem in getting table on panel
    By adeeb in forum AWT / Swing
    Replies: 0
    Last Post: 06-09-2008, 08:23 PM
  4. How to place panel into frame
    By vivek_9912 in forum AWT / Swing
    Replies: 2
    Last Post: 11-19-2007, 11:21 PM
  5. Help with drag from panel
    By fernando in forum AWT / Swing
    Replies: 2
    Last Post: 08-07-2007, 10:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •