2D图形画不出来

乾K大挪移 2008-06-08 10:43:44

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画不出来???
...全文
125 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
magichao4141 2008-06-08
  • 打赏
  • 举报
回复
呵呵~ 我也正在看这本书
xray2005 2008-06-08
  • 打赏
  • 举报
回复
这个是《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);
}

}
乾K大挪移 2008-06-08
  • 打赏
  • 举报
回复
只显示一个空白的Frame,里面什么也没有。
乾K大挪移 2008-06-08
  • 打赏
  • 举报
回复
楼上的说的很有道理!!!谢了!
witcheryne 2008-06-08
  • 打赏
  • 举报
回复
晕~~ 代码中加不成颜色 - -|
"[color=#FF0000] "没有这句 代码...
witcheryne 2008-06-08
  • 打赏
  • 举报
回复
绘图操作 必须在 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);
}
}


乾K大挪移 2008-06-08
  • 打赏
  • 举报
回复
我知道是这本书的,但是为什么出不来???
我代码哪里写错了?
我对照了半天没找出不一样的地方

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧