Results 1 to 10 of 10
  1. #1
    bmw4l1f3 is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Default Java Applet Issue

    Ok, so I have this rudimentary code, basically just to test if Netbeans 6.0 is working correctly. Well when I run the program (draws a 800 x 500 rectangle) the rectangle gets cut off

    Java Code:
    import java.applet.*;
    import java.awt.BorderLayout;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Frame;
    import java.awt.Rectangle;
    import java.awt.TextField;
    import java.awt.event.TextEvent;
    import java.awt.event.TextListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import java.io.*;
    
     
    public class MyApplet extends Applet {
        // Values
        private Rectangle box;
      
        public MyApplet() {
            box = new Rectangle(300,10, 10, 10);
           
    
        }
            
        public void paint(Graphics g) {
           
            Graphics2D g2 = (Graphics2D)g;
            setSize(new java.awt.Dimension(1000, 1000));
            g2.drawRect(0,0, 800, 500);
           
          
          
        }
    }

    I have the picture, I can send it VIA PM if you would like

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I think that you set the size of an Applet in the HTML that holds it (I may be wrong as I'm not an expert in this). Also, I'd advise you against calling setSize from within the paint method as it smells funny in that location. The paint method should be used for painting and nothing but painting.

  3. #3
    bmw4l1f3 is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Default

    Where should I put the setSize then?? If I put it anywhere else it doesn't work

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    How about (as I stated above) in your HTML code? Do not put it in the paint method. It may work somewhat in the init method, but I still think it's better in the html code that holds and displays the applet. again, i'm no expert on this. Ask me about Swing :)
    Last edited by Fubarable; 11-13-2008 at 04:47 AM.

  5. #5
    bmw4l1f3 is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Default

    This isn't going to be run in a browser, this is to be a standalone program. Sorry if I didn't state this in the beginning.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    If a standalone, why create an Applet? From what I know/believe, I think that you'd be far better off using Swing and not AWT and creating a JFrame.

  7. #7
    bmw4l1f3 is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Default

    Thanks! That is exactly what I am going to do. I remember thinking about doing it via swing, but when I sat down to program that idea got thrown out of the window.. Thank you for not only your help, but the quickness you answered my question

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You're welcome. One thing I noticed just now about your code is that your MyApplet class has a constructor. Please note that applet extending classes usually don't have their constructors called, but rather the init() method is called instead.

    Good luck with your JFrame and please come back if you have a question.

  9. #9
    bmw4l1f3 is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Default

    Yeah, I'm a little rusty (well more than a little) when it comes to Java. Hell the last time I programmed in Java, projects weren't used.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Just to get you started, here's an example similar to what you were trying to create above, but using Swing:
    Java Code:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.geom.Rectangle2D;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class RectExample extends JPanel
    {
      private Rectangle2D rect2D = new Rectangle2D.Double(10, 10, 500, 400);
      
      public RectExample()
      {
        setPreferredSize(new Dimension(700, 500));
      }
      
      @Override
      protected void paintComponent(Graphics g)
      {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setColor(Color.blue);
        g2.fill(rect2D);
      }
      private static void createAndShowUI()
      {
        JFrame frame = new JFrame("RectExample");
        frame.getContentPane().add(new RectExample());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args)
      {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
          public void run()
          {
            createAndShowUI();
          }
        });
      }
    }
    the Sun graphics tutorials are great, BTW, and you'd do well to study them.

    Again, much luck.

Similar Threads

  1. Replies: 4
    Last Post: 03-21-2009, 11:43 PM
  2. MySQL + Java - auto incrementation issue
    By int80 in forum Advanced Java
    Replies: 6
    Last Post: 07-29-2008, 06:04 PM
  3. Replies: 3
    Last Post: 07-23-2008, 08:07 AM
  4. how to issue the command of Ctrl-C (copy) in Java
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-18-2007, 03:14 PM
  5. Applet tabbing issue
    By pegitha in forum Java Applets
    Replies: 2
    Last Post: 04-28-2007, 10:41 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
  •