You want it to draw where? See your code you are using main which will output in console, and actually main without extending special class like JFrame
(this is what you need because this is where you can draw ,this is a window in java)
see below code, for other way drawing your rectangle.
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
class UsingRectangle extends JComponent {
public void paint(Graphics g) {
g.drawRect (10, 10, 200, 200);
}
}
public class DrawRect {
public static void main(String[] a) {
JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(30, 30, 300, 300);
window.getContentPane().add(new UsingRectangle());
window.setVisible(true);
}
}
or using paintComponent
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class UsingRectangle extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawRectangle (10, 10, 200, 200);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(300, 200);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Container contentPane = frame.getContentPane();
contentPane.add(new UsingRectangle());
frame.show();
}
}
also see this for what the difference of paint
and paintComponent
for future reference.