2007-11-20
对Swing的一点理解(一)
关键字: Java 最近几天学习Java_Swing的时候,用Java的2D画图的时候发现了一点问题,如果直接在JFrame中直接画图的话和容易就会出现下面的问题:
(因为我这里帖不上图片,所以把图片放在附件了大有需要看的,可以下载附件)
但是如果 在JFrame中加入一个JPanel 问题就没有了,这个问题我想有很多的初学者和我一样遇到过。加入JPanel以后的图:
(因为我这里帖不上图片,所以把图片放在附件了大有需要看的,可以下载附件)
java 代码
- //这是第一幅图的代码
- import javax.swing.*;
- import java.awt.*;
- public class Circle1 extends JFrame{
- public static void main(String[] args) {
- Circle1 Cir=new Circle1();
- }
- public Circle1(){
- super("Draw circle");
- setSize(500,500);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setVisible(true);
- }
- public void paint(Graphics g)
- {
- int circlePointX=200;
- int circlePointY=200;
- int radius=200;
- int x = circlePointX;
- int y = circlePointY + radius;
- int d = 5 - 4 * radius;
- int deltaE = 12;
- int deltaSE = 20 - 8 * radius;//initialization.
- g.drawRect(x,y,1,1);
- g.drawRect(y,x,1,1);
- g.drawRect(-y,x,1,1);
- g.drawRect(-x,y,1,1);
- g.drawRect(y,-x,1,1);
- g.drawRect(x,-y,1,1);
- g.drawRect(-x,-y,1,1);
- g.drawRect(-y,-x,1,1);
- while ( y > x)
- {
- if (d <= 0)
- {
- d += deltaE;
- deltaSE += 8;
- }
- else
- {
- d += deltaSE;
- deltaSE += 16;
- y--;
- }
- deltaE += 8;
- x++;
- g.drawRect(x,y,1,1);
- g.drawRect(y,x,1,1);
- g.drawRect(-y,x,1,1);
- g.drawRect(-x,y,1,1);
- g.drawRect(y,-x,1,1);
- g.drawRect(x,-y,1,1);
- g.drawRect(-x,-y,1,1);
- g.drawRect(-y,-x,1,1);;
- }
- }
- }
java 代码
- //这是第二幅图的代码:
- import javax.swing.*;
- import java.awt.*;
- public class Circle {
- public static void main(String[] args) {
- TestCircle Cir=new TestCircle();
- Cir.setVisible(true);
- Cir.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- }
- class TestCircle extends JFrame{
- public TestCircle(){
- setTitle("Draw circle");
- setSize(500,500);
- DrawCirclePanel panel = new DrawCirclePanel();
- add(panel);
- }
- }
- class DrawCirclePanel extends JPanel{
- public void paint(Graphics g){
- int circlePointX=200;
- int circlePointY=200;
- int radius=200;
- int x = circlePointX;
- int y = circlePointY + radius;
- int d = 5 - 4 * radius;
- int deltaE = 12;
- int deltaSE = 20 - 8 * radius;//initialization.
- g.drawRect(x,y,1,1);
- g.drawRect(y,x,1,1);
- g.drawRect(-y,x,1,1);
- g.drawRect(-x,y,1,1);
- g.drawRect(y,-x,1,1);
- g.drawRect(x,-y,1,1);
- g.drawRect(-x,-y,1,1);
- g.drawRect(-y,-x,1,1);
- while ( y > x)
- {
- if (d <= 0)
- {
- d += deltaE;
- deltaSE += 8;
- }
- else
- {
- d += deltaSE;
- deltaSE += 16;
- y--;
- }
- deltaE += 8;
- x++;
- g.drawRect(x,y,1,1);
- g.drawRect(y,x,1,1);
- g.drawRect(-y,x,1,1);
- g.drawRect(-x,y,1,1);
- g.drawRect(y,-x,1,1);
- g.drawRect(x,-y,1,1);
- g.drawRect(-x,-y,1,1);
- g.drawRect(-y,-x,1,1);;
- }
- }
- }
发表评论
- 浏览: 4274 次
- 性别:

- 来自: 福建

- 详细资料
搜索本博客
我的相册
linuxkerel.jpg
共 5 张
共 5 张
最新评论
-
NetBeans vs Eclipse 最新 ...
qubic 写道leirdal 写道唉,netbeans和eclipse的比较, ...
-- by leirdal -
NetBeans vs Eclipse 最新 ...
K,这种话题6天12页,我真服了。。。
-- by 冉翔 -
NetBeans vs Eclipse 最新 ...
喜欢楼上这样的话,自己亲自去用某样东西,才有资格去评价它,否则,只能是人云亦云了
-- by lbfhappy -
NetBeans vs Eclipse 最新 ...
今天用了一下netbean 还有UI设计功能,很好很强大,毫无疑问第一感觉UI响 ...
-- by XMLDB -
NetBeans vs Eclipse 最新 ...
n个人说了VS不占内存速度快之后,证明了有个人就会信口开河.
-- by ray_linn






评论排行榜