Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a JApplet (swing based applet) with netbeans?
    primarykey
    data
    text
    <p>I am starting to learn Java a little after long time. And learning Netbeans 7.0.</p> <p>I just want to make sure I am doing this ok. </p> <p>I basically need to make an applet, but not having it AWT based, but instead Swing based. </p> <p>So I need to extend JApplet, not Applet. I understand in swing draws on a Jpanel instead of awt canvas (or Panel). And so I read on a web site that one needs to override PaintComponent() instead of overrriding paint() as the case with awt applet?</p> <p>I need to make a very simple applet, say with one button, when I click on it, I want to draw a graphics, say a line or circle, and have the output go to an area below the button.</p> <p>This is what I did </p> <ol> <li>File->New Project</li> <li>Select Java and from Projects, select "Java Application"</li> <li>make sure to Un-check the "create Main class", and click Finish</li> <li>File->New file</li> <li>Select "Swing GUI Forms" from under the catagories panel</li> <li>From the "File types", Select Japplet Form,Next and Finish</li> <li>From the palette, from Swing Controls, select Button and lay it on the from</li> <li>Now the tricky part. I need an area to draw on, right? So I from palette, I select, from Swing containers, a "Panel", and lay it on the form, resize it as needed. Do, now I have this:</li> </ol> <p><img src="https://i.stack.imgur.com/X0O3X.png" alt="enter image description here"></p> <p>Am I on the right track so far? Now I open the source file, and I see the init() method.</p> <p>Now is where I need little help. Not sure what the code I need to insert to just draw a line to the JPanel I just added. I know I need to insert it here:</p> <p><img src="https://i.stack.imgur.com/jN30h.png" alt="enter image description here"></p> <p>I tried the "insert Code" feature, and select override, but do not see PaintComponent()? </p> <p>I know how to do it in swt applet, just add a paint(Graphics g) method. But when I do this, the graphics do not draw inside the Jpanel area. Basically, how do I tell it to draw something to a specific JPanel area? </p> <p>If someone just tell me what code I need to insert there to draw a line or any graphics2D object to display on that JPanel I added below the bottom, that will great.</p> <p>thanks, --Nasser</p> <h2>EDIT 1:</h2> <p>Just a clarrification: If I add this function to paint on the Jpanel:</p> <p>public void paint(Graphics g)<br> {<br> super.paint(g);<br> g.drawString(....); } </p> <p>Then the output does show ok, but it over the main Japnel. And can hide the button and any other UI components are there.</p> <p>I need the paint output to go to a specific Jpanel which I added earlier below the button. To this one</p> <pre><code>private javax.swing.JPanel jPanel1; </code></pre> <p>So, my question is, how to draw/paint to the above object and not to the main Jpanel?</p> <h2>EDIT 2:</h2> <p>I tried just to change the JPanel background color, and it is not working. Here is the code. I also tried JpanelForm instead of JApplet Form. Can one use JFrame form to make an applet? Since that requires a main() it does not seem possible.</p> <pre><code>import javax.swing.*; import java.awt.*; import java.awt.Graphics; public class NewJApplet extends javax.swing.JApplet { /** Initializes the applet NewJApplet */ public void init() { jPanel1 = new JPanel(); try { java.awt.EventQueue.invokeAndWait(new Runnable() { public void run() { initComponents(); } }); } catch (Exception ex) { ex.printStackTrace(); } } private void initComponents() {...} //--------- ADDED THIS private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: Rectangle rect=new Rectangle(4,4); jPanel1.setBackground(Color.RED); } //--------------- // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JPanel jPanel1; // End of variables declaration } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload