Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

big favour - convert applet to app

  • 03-05-2006 2:49pm
    #1
    Closed Accounts Posts: 32


    hey guys,

    i attached an applet that i've created from two seperate applets. Basically it is whiteboard that you can write and draw on. I have an another program that this is goin into through a JPanel.

    I've followed instructions I found on the net for changing an Applet into an App, but whatever way it does it. It wont let me put an instance of the app in my program. this is what i tried to do: http://www.rgagnon.com/javadetails/java-0305.html

    if any knows a quick simple way to do this, i would be very grateful?

    thnx
    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.*;
    
    public class SimpleWhiteboard extends Applet {
      protected int lastX=0, lastY=0;
      protected FontMetrics fm;
    
      public void init() {
        setBackground(Color.white);
        setForeground(Color.black);
        addMouseListener(new PositionRecorder());
        addMouseMotionListener(new LineDrawer());
        Font font = new Font("Serif", Font.BOLD, 20);
    	setFont(font);
    	fm = getFontMetrics(font);
        addKeyListener(new CharDrawer());
      }
    
      protected void record(int x, int y) {
        lastX = x;
        lastY = y;
      }
    
      // Record position that mouse entered window or
      // where user pressed mouse button.
    
      private class PositionRecorder extends MouseAdapter {
        public void mouseEntered(MouseEvent event) {
          requestFocus(); // Plan ahead for typing
          record(event.getX(), event.getY());
        }
    
        public void mousePressed(MouseEvent event) {
          record(event.getX(), event.getY());
        }
      }
    
      // As user drags mouse, connect subsequent positions
      // with short line segments.
    
      private class LineDrawer extends MouseMotionAdapter {
        public void mouseDragged(MouseEvent event) {
          int x = event.getX();
          int y = event.getY();
          Graphics g = getGraphics();
          g.drawLine(lastX, lastY, x, y);
          record(x, y);
        }
      }
    
      private class CharDrawer extends KeyAdapter {
          // When user types a printable character,
          // draw it and shift position rightwards.
    
          public void keyTyped(KeyEvent event) {
            String s = String.valueOf(event.getKeyChar());
            getGraphics().drawString(s, lastX, lastY);
            record(lastX + fm.stringWidth(s), lastY);
          }
      }
    }
    
    


Comments

  • Registered Users, Registered Users 2 Posts: 40 dob99


    Just add the following main method to your class, then you can run it directly:
      public static void main(String args[])
      {
        Frame f = new Frame();
        f.addWindowListener(new java.awt.event.WindowAdapter() {
             public void windowClosing(java.awt.event.WindowEvent e) {
             System.exit(0);
             };
           });
    
        SimpleWhiteboard wb = new SimpleWhiteboard();
        wb.setSize(100,100);
        f.add(wb);
        f.pack();
        wb.init();
        f.setSize(100,100 + 20);
        f.setVisible(true);
      }
    

    You can play around with the sizes and set the title, menu, etc to the Frame object.

    If you want to use a JPanel, make sure you use an appropriate LayoutManager (FlowLayout is a bit limited - try BorderLayout).


  • Closed Accounts Posts: 32 VanStrummer


    i hate that... i feel like a non IT person..

    you try something for hours.. doesnt work. someone makes a suggestion and there you go if works for ya... yeah i have a JPanel with BorderLayout setup.

    The App when run directly works fine, drawing and writing, but when i run my GUI that has the JPanel and uses the whiteboard only the CharDrawer() method works. I cant seem to draw any lines.


  • Registered Users, Registered Users 2 Posts: 40 dob99


    That may have something to do with painting - Swing is a bit strange (and more complex than AWT) when it comes to these things.

    Even using the AWT code above, if you resize the Frame, everything disappears. This is because you're not actually saving the "work" in memory. To get around it, I would draw onto an in-memory Image and then draw that Image into the Applet's Graphics instance in the Applet's paint method (which you override from Container).

    (It's a while since I've done anything like this, but I think that's the recommended method.)


Advertisement