对Swing的一点理解(一)


   最近几天学习Java_Swing的时候,用Java2D画图的时候发现了一点问题,如果直接在JFrame中直接画图的话和容易就会出现下面的问题:

(因为我这里帖不上图片,所以把图片放在附件了大有需要看的,可以下载附件)
但是如果 在JFrame中加入一个JPanel 问题就没有了,这个问题我想有很多的初学者和我一样遇到过。加入JPanel以后的图:

(因为我这里帖不上图片,所以把图片放在附件了大有需要看的,可以下载附件)
<o:p></o:p>

这是他们的源代码:
java 代码
  1. //这是第一幅图的代码
  2. import javax.swing.*;
  3. import java.awt.*;
  4. public class Circle1 extends JFrame{
  5. public static void main(String[] args) {
  6. Circle1 Cir=new Circle1();
  7. }
  8. public Circle1(){
  9. super("Draw circle");
  10. setSize(500,500);
  11. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  12. setVisible(true);
  13. }
  14. public void paint(Graphics g)
  15. {
  16. int circlePointX=200;
  17. int circlePointY=200;
  18. int radius=200;
  19. int x = circlePointX;
  20. int y = circlePointY + radius;
  21. int d = 5 - 4 * radius;
  22. int deltaE = 12;
  23. int deltaSE = 20 - 8 * radius;//initialization.
  24. g.drawRect(x,y,1,1);
  25. g.drawRect(y,x,1,1);
  26. g.drawRect(-y,x,1,1);
  27. g.drawRect(-x,y,1,1);
  28. g.drawRect(y,-x,1,1);
  29. g.drawRect(x,-y,1,1);
  30. g.drawRect(-x,-y,1,1);
  31. g.drawRect(-y,-x,1,1);
  32. while ( y > x)
  33. {
  34. if (d <= 0)
  35. {
  36. d += deltaE;
  37. deltaSE += 8;
  38. }
  39. else
  40. {
  41. d += deltaSE;
  42. deltaSE += 16;
  43. y--;
  44. }
  45. deltaE += 8;
  46. x++;
  47. g.drawRect(x,y,1,1);
  48. g.drawRect(y,x,1,1);
  49. g.drawRect(-y,x,1,1);
  50. g.drawRect(-x,y,1,1);
  51. g.drawRect(y,-x,1,1);
  52. g.drawRect(x,-y,1,1);
  53. g.drawRect(-x,-y,1,1);
  54. g.drawRect(-y,-x,1,1);;
  55. }
  56. }
  57. }

java 代码
  1. //这是第二幅图的代码:
  2. import javax.swing.*;
  3. import java.awt.*;
  4. public class Circle {
  5. public static void main(String[] args) {
  6. TestCircle Cir=new TestCircle();
  7. Cir.setVisible(true);
  8. Cir.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  9. }
  10. }
  11. class TestCircle extends JFrame{
  12. public TestCircle(){
  13. setTitle("Draw circle");
  14. setSize(500,500);
  15. DrawCirclePanel panel = new DrawCirclePanel();
  16. add(panel);
  17. }
  18. }
  19. class DrawCirclePanel extends JPanel{
  20. public void paint(Graphics g){
  21. int circlePointX=200;
  22. int circlePointY=200;
  23. int radius=200;
  24. int x = circlePointX;
  25. int y = circlePointY + radius;
  26. int d = 5 - 4 * radius;
  27. int deltaE = 12;
  28. int deltaSE = 20 - 8 * radius;//initialization.
  29. g.drawRect(x,y,1,1);
  30. g.drawRect(y,x,1,1);
  31. g.drawRect(-y,x,1,1);
  32. g.drawRect(-x,y,1,1);
  33. g.drawRect(y,-x,1,1);
  34. g.drawRect(x,-y,1,1);
  35. g.drawRect(-x,-y,1,1);
  36. g.drawRect(-y,-x,1,1);
  37. while ( y > x)
  38. {
  39. if (d <= 0)
  40. {
  41. d += deltaE;
  42. deltaSE += 8;
  43. }
  44. else
  45. {
  46. d += deltaSE;
  47. deltaSE += 16;
  48. y--;
  49. }
  50. deltaE += 8;
  51. x++;
  52. g.drawRect(x,y,1,1);
  53. g.drawRect(y,x,1,1);
  54. g.drawRect(-y,x,1,1);
  55. g.drawRect(-x,y,1,1);
  56. g.drawRect(y,-x,1,1);
  57. g.drawRect(x,-y,1,1);
  58. g.drawRect(-x,-y,1,1);
  59. g.drawRect(-y,-x,1,1);;
  60. }
  61. }
  62. }

你可能感兴趣的:(swing)