| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 498 人关注过本帖
标题:为何框架窗口中没有显示图形....
只看楼主 加入收藏
geqmoon
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2004-10-16
收藏
 问题点数:0 回复次数:2 
为何框架窗口中没有显示图形....


// 我设计了一个CAR类,再JFRAME类来显示CAR,怎么运行时只见框架,不见车呢?




import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import javax.swing.JFrame;
import javax.swing.JComponent;

class CarComponent extends JComponent
{
public void PrintCarComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;

Car car1 = new Car(0,0);
Car car2 = new Car(getWidth() - 50,getHeight() - 30);

car1.draw(g2);
car2.draw(g2);
}
}

class Car
{
public Car(int x,int y)
{
X1 = x;
Y1 = y;
}
public void draw(Graphics2D g2)
{
Point2D.Double fronttop = new Point2D.Double(X1 + 20,Y1);
Point2D.Double frontlow = new Point2D.Double(X1 + 10,Y1 + 10);
Point2D.Double backtop = new Point2D.Double(X1 + 30,Y1);
Point2D.Double backlow = new Point2D.Double(X1 + 40,Y1 + 10);

Line2D.Double cartop = new Line2D.Double(fronttop,backtop);
Line2D.Double frontglass = new Line2D.Double(fronttop,frontlow);
Line2D.Double backglass = new Line2D.Double(backtop,backlow);

Rectangle carbody = new Rectangle(X1,Y1+10,50,10);

Ellipse2D.Double frontwheel = new Ellipse2D.Double(X1 + 10,Y1 + 20,10,10);
Ellipse2D.Double backwheel = new Ellipse2D.Double(X1 + 30,Y1 + 20,10,10);

g2.draw(cartop);
g2.draw(frontglass);
g2.draw(backglass);
g2.draw(carbody);
g2.draw(frontwheel);
g2.draw(backwheel);
}

private int X1;
private int Y1;
}


public class CarViewer
{
public static void main(String[] args)
{
JFrame Frame = new JFrame();
Frame.setSize(300,500);
Frame.setTitle("我的文档");

CarComponent Component = new CarComponent();
Frame.add(Component);

Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Frame.setVisible(true);
}
}



搜索更多相关主题的帖子: 图形 框架 窗口 
2007-06-18 13:08
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
public void PrintCarComponent(Graphics g)

把它改成

public void paintComponent(Grahpics g)

可惜不是你,陪我到最后
2007-06-18 13:09
geqmoon
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2004-10-16
收藏
得分:0 

豁然明朗...


2007-06-18 13:24
快速回复:为何框架窗口中没有显示图形....
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020242 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved