import java.awt.*; import javax.swing.*; public class MyDrawPanel extends JPanel { public void paintComponent(Graphics g) { g.drawRect(10,10,50,50); } } public class MyGUI { public static void main(String[] args) { JFrame frame = new JFrame(); MyDrawPanel panel = new MyDrawPanel(); frame.getContentPane().add(panel); frame.setSize(200,200); frame.setVisible(true); } }
import java.awt.*; import java.awt.image.BufferedImage; import javax.swing.*; public class MyImage { public static void main(String[] args) { BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = img.createGraphics(); g2.setColor(Color.white); g2.fillRect(0, 0, 100, 100); g2.setColor(Color.red); g2.fillOval(25, 25, 50, 50); g2.dispose(); JFrame frame = new JFrame(); JLabel label = new JLabel(new ImageIcon(img)); frame.getContentPane().add(label); frame.setSize(200, 200); frame.setVisible(true); } }This example creates a BufferedImage and uses Graphics2D to draw a white background and a red oval. It then displays the image in a JFrame using a JLabel with an ImageIcon. Package library: java.awt, javax.swing