fb55 fb55 - 16 days ago 6
Java Question

Paint a graph in Java using AWT

I'm searching a possibility to paint a graph in Java using AWT. My knowledge of Java is still basic, so this is mainly for getting used to AWT, even if it's already "old".

-Felix

Answer
JFrame f = new JFrame();
JPanel p = new JPanel () {
     public void paint(Graphics g){
         Graphics2D g2d = printImg.createGraphics();
         g2d.drawString("123", 10, 10);        
     }
 }
 f.add(p);
 f.setSize(100,100);
 f.setVisible(true);

Small example of how to paint in Swing components, the same in AWT. The paint method is the one where you can paint. To see what you can paint (Rectangles, circles, ...) you can look at the Java API of the "Graphics" or "Graphics2D" class.