import javax.swing.*; import java.awt.*; public class MyFrame extends JFrame { public MyFrame() { super("Drawing a String"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); DrawingPanel dp = new DrawingPanel(); add(dp); setSize(300, 150); setVisible(true); } public static void main(String[] args) { new MyFrame(); } } class DrawingPanel extends JPanel { @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.drawString("Hello, World!", 50, 50); } }
import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; public class ImageExample { public static void main(String[] args) { BufferedImage image = new BufferedImage(200, 100, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); g.setColor(Color.WHITE); g.fillRect(0, 0, 200, 100); g.setColor(Color.BLACK); g.drawString("Java Graphics", 50, 50); try { ImageIO.write(image, "png", new File("image.png")); } catch (Exception e) { e.printStackTrace(); } } }In this example, we create a `BufferedImage` object and draw a string on it using the `drawString()` method. The resulting image is written to a file using the `ImageIO` class. The `java.awt.Graphics` package is part of the standard Java library.