import java.awt.*; public class CircleExample extends Frame { public static void main(String[] args) { new CircleExample().setVisible(true); // create a new frame and make it visible } public CircleExample() { setSize(300, 300); // set the size of the frame // create a new canvas and add it to the frame add(new Canvas() { public void paint(Graphics g) { g.setColor(Color.RED); // set the color to red g.translate(100, 100); // translate the origin to (100, 100) g.drawOval(0, 0, 50, 50); // draw a circle with radius 25 } }); } }
import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; public class ImageExample extends Frame { public static void main(String[] args) throws Exception { new ImageExample().setVisible(true); // create a new frame and make it visible } public ImageExample() throws Exception { setSize(300, 300); // set the size of the frame // load the image from the file BufferedImage image = ImageIO.read(new File("image.png")); // create a new canvas and add it to the frame add(new Canvas() { public void paint(Graphics g) { g.translate(100, 100); // translate the origin to (100, 100) g.drawImage(image, 0, 0, null); // draw the image } }); } }The java.awt Graphics translate method belongs to the java.awt package library.