我正在尝试使用eclipse在java中绘制一些矩形,并且调用draw()
方法会给我一个错误。我在这里缺少什么吗?
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();
}
}
任何帮助都不仅仅是值得赞赏的。 谢谢。
答案 0 :(得分:5)
首先阅读:
Rectangle
是形状的表示,它本身没有如何绘画的概念,但可以通过适当的API绘制
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();
}
}
}
答案 1 :(得分:0)
这是绘制矩形的方法。在您发布问题之前谷歌有点:)
// 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);
}
}
编辑:Java The complete reference
Herbert Schildt
答案 2 :(得分:0)
你想要它画在哪里?查看你正在使用main的代码,它将在控制台中输出,实际上是main而不扩展特殊类,如JFrame
(这是你需要的,因为这是你可以绘制的地方,这是java中的一个窗口)
请参阅下面的代码,了解绘制矩形的其他方法。
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);
}
}
或使用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();
}
}
另请参阅this了解paint
和paintComponent
之间的差异,以供将来参考。