Results 1 to 2 of 2
  1. #1
    Pucho is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default How to paint on a Netbean 6.5 generated applet?

    Hello everyone:

    I wish to create an applet using Netbeans 6.5 and paint on it with instructions as, for example,

    gr.setColor(Color.YELLOW);
    gr.fillRect(10, 10, 100, 100);
    etc.

    I created an applet according to Netbeans help instructions, that is:

    File/New project/Java/Java class library…
    Later:
    I right clicked over the project icon /New/Other/Swing GUI forms/JApplet form.

    I have added controls etc.. but I’m not able to paint anything on the applet.
    I tried many ways…,

    I have created my own panel class extended from JPanel and overrode the paint() method…,
    I have obtained the graphics context by getGraphics()…,
    I have added Internal frame to my main class (extended from JApplet) etc.

    but always unsuccessfully.

    Source code compiles fine, no errors reported, but I only see the controls added, never what I tried to paint.

    Could you please let me know what should I do to paint something on the applet?

    Sorry, I have been studying Java only since two weeks ago.

    Thanks a lot in advance
    Pucho

    Note: I can paint on a simple applet (not using Swing) but losing all of the Swing advantages.

  2. #2
    Pucho is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Unhappy

    I read that I should use a JPanel, override its paintComponent() method, having as the first line a call to super.paintComponent(g); and then add the JPanel to the JApplet's contentPane.

    However:
    1) If I visually drag a JPanel to the JApplet’s content pane I don’t know how to override the paintComponent() method.

    2) On the other hand, if I create a new class “MyPanel” extended from JPanel and add it (not visually but using the code) to the JApplet’s content pane, I can override the paintComponent() method but I cannot see MyPanel in the Design view so I cannot drag and drop controls . In addition I cannot see neither what I tried to paint , see applet’s code below.

    //------------------------------------------------------
    package AP001Package;
    import java.awt.*;
    import javax.swing.BorderFactory;

    public class AP001class extends javax.swing.JApplet {
    @Override
    public void init() {
    try {
    java.awt.EventQueue.invokeAndWait(new Runnable() {
    public void run() {
    add(new MyPanel());
    initComponents();

    }
    });
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">

    }

    class MyPanel extends javax.swing.JPanel
    {
    public MyPanel() {
    setBorder(BorderFactory.createLineBorder(Color.bla ck));
    }

    @Override
    public Dimension getPreferredSize() {
    return new Dimension(250,200);
    }

    @Override
    public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.setColor(Color.YELLOW);
    g.fillRect(10, 10, 100, 100);
    }
    }

Similar Threads

  1. auto generated keys
    By abhi_iips in forum New To Java
    Replies: 3
    Last Post: 03-13-2009, 07:14 AM
  2. How to delete pre-generated code?
    By Terentius in forum NetBeans
    Replies: 0
    Last Post: 08-19-2008, 04:02 PM
  3. Replies: 5
    Last Post: 08-16-2008, 04:33 PM
  4. getting dynamically generated valus
    By abhiN in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 03-29-2008, 10:58 AM
  5. Generated servlet error
    By tommy in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-05-2007, 10:46 AM

Posting Permissions

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