Reputation: 1
I am trying to draw some rectangles in java using eclipse and calling the draw()
method gives me an error. Is there something I am missing here?
import java.awt.Rectangle;
public class UsingRectangle {
public static void main(String[] args) {
Rectangle box1 = new Rectangle(0, 0, 20, 30);
//error "The method draw() is undefined for the type Rectangle"
box1.draw();
}
}
Any help would be more than appreciated. Thank you.
Upvotes: 0
Views: 3941
Reputation: 2528
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.
Upvotes: 0
Reputation: 347334
Start by having a read through:
Rectangle
is a representation of a shape, in of itself, it has no concept of how to paint, but can be painted through the appropriate APIs
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class PaintRectangle {
public static void main(String[] args) {
new PaintRectangle();
}
public PaintRectangle() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private Rectangle box1;
public TestPane() {
box1 = new Rectangle(0, 0, 20, 30);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.RED);
g2d.fill(box1);
g2d.setColor(Color.BLUE);
g2d.draw(box1);
g2d.dispose();
}
}
}
Upvotes: 5
Reputation: 1098
This is how you draw rectangles. Google a bit before you post questions :)
// Draw rectangles
import java.awt.*;
import java.applet.*;
public class Rectangles extends Applet {
public void paint(Graphics g) {
g.drawRect(10, 10, 60, 50);
g.fillRect(100, 10, 60, 50);
g.drawRoundRect(190, 10, 60, 50, 15, 15);
g.fillRoundRect(70, 90, 140, 100, 30, 40);
}
}
EDIT: Example taken from Java The complete reference
by Herbert Schildt
Upvotes: 0