import java.awt.*;
import java.awt.geom.Rectangle2D;
import javax.swing.*;public class DrawTest
{
public static void main(String[] args)
{
DrawFrame frame=new DrawFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}class DrawFrame extends JFrame
{
public DrawFrame()
{
setSize(Width,Height);
setTitle("DrawTest");
setLocation(500, 300);

DrawPanel panel=new DrawPanel();
add(panel);
}

public static final int Width=400;
public static final int Height=500;}class DrawPanel extends JPanel
{
public void paintComponet(Graphics g)
{ super.paintComponent(g);
Graphics2D g2=(Graphics2D)g;

//画一个方块
Rectangle2D rect=new Rectangle2D.Double(100,100,200,150);
g2.draw(rect);
}
}
为什么我的rect画不出来???

解决方案 »

  1.   

    只显示一个空白的Frame,里面什么也没有。
      

  2.   

    这个是《Java核心技术第一卷,基础知识》书上的例子,我昨天刚看完这章哦。我的按照书上的代码敲,都可以画出来啊。我还加其颜色,写字等等。
    代码和你一样呢!
    import java.awt.*;
    import java.awt.color.*;
    import java.awt.geom.*;
    import java.awt.geom.Ellipse2D.Double;
    import java.awt.font.*;
    import javax.swing.*;class DrawPanel extends JPanel {
    public void paintComponent(Graphics g) { super.paintComponent(g);
    // this.setBackground(Color.RED); Graphics2D g2 = (Graphics2D) g;
    double leftx = 100;
    double topy = 100;
    double width = 200;
    double height = 150;
    Rectangle2D rect = new Rectangle2D.Double(leftx, topy, width, height);
    // g2.setColor(Color.red);
    g2.draw(rect);
    Ellipse2D ellipse = new Ellipse2D.Double();
    ellipse.setFrame(rect);
    g2.draw(ellipse);
    g2.draw(new Line2D.Double(leftx, topy, width + leftx, height + topy));
    double ceterX = rect.getCenterX();
    double ceterY = rect.getCenterY();
    double radius = 150;
    Ellipse2D circle = new Ellipse2D.Double();
    circle.setFrameFromCenter(ceterX, ceterY, ceterX + radius, radius
    + ceterY);
    g2.setColor(Color.blue);
    g2.draw(circle);
    // g2.setPaint(Color.RED);
    String message = "老婆,我爱你!";
    Font sansbold114 = new Font("SansSerif", Font.BOLD, 14);
    g2.setFont(sansbold114);
    FontRenderContext context = g2.getFontRenderContext();
    Rectangle2D bouds = sansbold114.getStringBounds(message, context);
    double x = (getWidth() - bouds.getWidth()) / 2;
    double y = (getHeight() - bouds.getHeight()) / 2;
    double ascent = -bouds.getY();
    double baseY = y + ascent;
    g2.drawString(message, (int) x, (int) baseY);
    // g2.fill(circle);
    g2.dispose();
    }
    }class DrawFrame extends JFrame {
    public static final int DEFAULT_WIDTH = 400;
    public static final int DEFALT_HEGIHT = 400; public DrawFrame() {
    this.setTitle("DrawTest");
    this.setSize(DEFAULT_WIDTH, DEFALT_HEGIHT); DrawPanel panel = new DrawPanel();
    this.add(panel);
    }}public class DrawTest { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    DrawFrame frame = new DrawFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    }}
      

  3.   

    绘图操作 必须在 Component 的 paint(Graphics g) 方法内进行...
    当产生重绘情况的时候 系统将自动调用paint()方法对容器进行重绘把把你的代码修改了一下...import java.awt.*;
    import java.awt.geom.Rectangle2D;
    import javax.swing.*;public class DrawTest
    {
        public static void main(String[] args)
        {
            DrawFrame frame=new DrawFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    }class DrawFrame extends JFrame
    {
        public DrawFrame()
        {
            setSize(Width,Height);
            setTitle("DrawTest");
            setLocation(500, 300);
            
            DrawPanel panel=new DrawPanel();
            this.add(panel, java.awt.BorderLayout.CENTER);//使用了布局管理器    }
        
        public static final int Width=400;
        public static final int Height=500;}class DrawPanel extends JPanel
    {
        //绘图必须在这里进行
        public void paint(Graphics g) 
        {
    paintComponet(g);
        }


        public void paintComponet(Graphics g)
        {        super.paintComponent(g);
            Graphics2D g2=(Graphics2D)g;
            
            //画一个方块
            Rectangle2D rect=new Rectangle2D.Double(100,100,200,150);
            g2.draw(rect);
        }
    }
      

  4.   

    晕~~  代码中加不成颜色  - -|
      "[color=#FF0000]  "没有这句 代码...