Results 1 to 2 of 2
- 03-31-2009, 03:26 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 3
- Rep Power
- 0
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.
- 03-31-2009, 05:04 PM #2
Member
- Join Date
- Mar 2009
- Posts
- 3
- Rep Power
- 0
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
-
auto generated keys
By abhi_iips in forum New To JavaReplies: 3Last Post: 03-13-2009, 07:14 AM -
How to delete pre-generated code?
By Terentius in forum NetBeansReplies: 0Last Post: 08-19-2008, 04:02 PM -
how to create a login system by using netbean 6?
By j007ha in forum NetBeansReplies: 5Last Post: 08-16-2008, 04:33 PM -
getting dynamically generated valus
By abhiN in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 03-29-2008, 10:58 AM -
Generated servlet error
By tommy in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 08-05-2007, 10:46 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks