views:

1394

answers:

3

I created a new JApplet form in netbeans:

public class UI extends javax.swing.JApplet {
    //generated code...
}

And a jpanel in design mode named panou:

// Variables declaration - do not modify                     
private javax.swing.JPanel panou;

How do I get to draw a line on "panou"? I've been searching for this for 5 hours now so a code snippet and where to place it would be great. (using Graphics2D preferably)

Edit: its getting really frustrating, I tried this (big chunk of code) Edit2: thanks to Martijn I made it work:)

A: 

To do custom painting in a JPanel, one would need to make a subclass of a JPanel, and then overload the paintComponent method:

class MyPanel extends JPanel {
  public void paintComponent(Graphics g) {
     // Perform custom painting here.
  }
}

In the example above, the MyPanel class is a subclass of JPanel, which will perform whatever custom painting is written in the paintComponent method.

For more information on how to do custom painting in Swing components, Lesson: Performing Custom Painting from The Java Tutorials have some examples.


If one wants to do painting with Java2D (i.e. using Graphics2D) then one could do some painting on a BufferedImage first, then draw the contents of the BufferedImage onto the JPanel:

class MyPanel extends JPanel {
  BufferedImage image;

  public MyPanel() {
    Graphics2D g = image.createGraphics();
    // Do Java2D painting onto the BufferedImage.
  }

  public void paintComponent(Graphics g) {
     // Draw the contents of the BufferedImage onto the panel.
     g.drawImage(image, 0, 0, null);
  }
}

Further reading:

coobird
+1  A: 
  1. Go to design mode
  2. Right Click on the panel "panou"
  3. Click "Costumize code"
  4. In the dialog select in the first combobox "costum creation"
  5. add after = new javax.swing.JPanel() this, so you see this:

 

panou = new javax.swing.JPanel(){
    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g); // Do the original draw
        g.drawLine(10, 10, 60, 60); // Write here your coordinates
    }
};

Make sure you import java.awt.Graphics.

The line that you will see is always one pixel thick. You can make it more "line" by doing the following:

Create this method:

public static final void setAntiAliasing(Graphics g, boolean yesno)
{
    Object obj = yesno ? RenderingHints.VALUE_ANTIALIAS_ON
                 : RenderingHints.VALUE_ANTIALIAS_OFF;

    ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, obj);
}

And add after super.paintComponent(g); (in your costum creation) this:

setAntiAlias(g, true);

Edit

What you are doing wrong is: you paint the line once (by creating the frame). When you paint the line the frame is also invisible. The first draw is happening when the frame becomes visible. The frame will be REpainted, so everything from the previous paint will disapear.
Always you resize the frame, everything will be repainted. So you have to make sure each time the panel is painted, the line also is painted.

Martijn

Martijn Courteaux
thank you very much
csiz
A: 

I just want to know whether this method can work with netbean IDE ??

Prashant
yes it works, see Martijn's answer.
csiz