Painting JComponent Objects : JComponent « Swing « Java Tutorial






Painting JComponent Objects
import java.awt.Graphics;

import javax.swing.JComponent;
import javax.swing.JFrame;

class MyComponent extends JComponent {
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    // Customize after calling super.paintComponent(g)

    g.drawString("string",20,20);
  }

}

public class PaintingJComponentObjects {
  public static void main(String[] args) {
    JFrame aWindow = new JFrame();
    aWindow.setBounds(30, 30, 300, 300); // Size
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    MyComponent com = new MyComponent();

    aWindow.add(com);

    aWindow.setVisible(true); // Display the window
  }
}








14.2.JComponent
14.2.1.JComponent
14.2.2.HTML formatting by setting the text on a labelHTML formatting by setting the text on a label
14.2.3.Listening to Inherited Events of a JComponent from Container and Component
14.2.4.Painting JComponent ObjectsPainting JComponent Objects
14.2.5.how an icon is adapted to a component
14.2.6.A Swing component that computes and displays a fractal image known as a 'Julia set'
14.2.7.Extends JComponent to create drawing pad
14.2.8.A panel that displays a paint sample.