CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Java >  J2SE / 基础类

初学者一个简单程序的问题!有分

楼主secondflying(金乌贼)2005-07-29 10:34:33 在 Java / J2SE / 基础类 提问

左边是绘图面板,右边有一个滑杆用来控制角度!使一个点围绕另一个点画直线!怎么就有问题呢?!本人使初学者,这个问题可能比较弱,还望大家不要笑话:)  
  import   java.awt.*;  
  import   java.awt.event.*;  
  import   java.applet.*;  
  import   javax.swing.*;  
  import   javax.swing.event.*;  
   
  class   myPane  
          extends   JPanel   {  
      int   x1   =   300;  
      int   y1   =   300;  
      int   x2;  
      int   y2;  
      int   Angle;  
   
      public   myPane()   {  
          x2   =   (int)   (x1   +   200   *   Math.cos(this.getAngle()));  
          y2   =   (int)   (y1   +   200   *   Math.sin(this.getAngle()));  
      }  
   
      public   void   setAngle(int   a)   {  
          Angle   =   a;  
      }  
      public   int   getAngle()   {  
          return   Angle;  
      }  
   
      public   void   paintComponent(Graphics   g)   {  
          super.paintComponent(g);  
          g.drawLine(x1,   y1,   x2,   y2);  
      }  
   
  }  
   
  public   class   Applet1  
          extends   JApplet   {  
      private   boolean   isStandalone   =   false;  
      myPane   p   =   new   myPane();  
      JPanel   control   =   new   JPanel();  
      JSlider   slider   =   new   JSlider(0,   360,   30);  
      //Get   a   parameter   value  
      public   String   getParameter(String   key,   String   def)   {  
          return   isStandalone   ?   System.getProperty(key,   def)   :  
                  (getParameter(key)   !=   null   ?   getParameter(key)   :   def);  
      }  
   
      //Construct   the   applet  
      public   Applet1()   {  
      }  
   
      //Initialize   the   applet  
      public   void   init()   {  
          try   {  
              jbInit();  
          }  
          catch   (Exception   e)   {  
              e.printStackTrace();  
          }  
      }  
   
      //Component   initialization  
      private   void   jbInit()   throws   Exception   {  
          this.setSize(new   Dimension(800,   600));  
   
          Container   contentPane   =   getContentPane();  
          contentPane.setLayout(new   BorderLayout());  
          contentPane.add(p,   BorderLayout.CENTER);  
          contentPane.add(control,   BorderLayout.EAST);  
          control.add(slider);  
          slider.setMajorTickSpacing(90);  
          slider.setMinorTickSpacing(30);  
          slider.setPaintLabels(true);  
          slider.setPaintLabels(true);  
          slider.addChangeListener(new   ChangeListener()   {  
              public   void   stateChanged(ChangeEvent   e)   {  
                  p.setAngle(slider.getValue());  
                  p.repaint();  
              }  
          }  
          );  
      }  
   
      //Get   Applet   information  
      public   String   getAppletInfo()   {  
          return   "Applet   Information";  
      }  
   
      //Get   parameter   info  
      public   String[][]   getParameterInfo()   {  
          return   null;  
      }  
   
      //static   initializer   for   setting   look   &   feel  
      static   {  
          try   {  
              //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());  
              //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());  
          }  
          catch   (Exception   e)   {  
          }  
      }  
  } 问题点数:10、回复次数:7Top

1 楼aico(aico)回复于 2005-07-29 13:03:52 得分 0

1、x2,y2的计算应放在paintComponent方法中,而不是在构造方法中。  
  2、Math.sin和Math.cos的参数都是弧度而不是角度。因此你必须把slider上的数值换算成弧度。Top

2 楼aico(aico)回复于 2005-07-29 13:06:51 得分 0

3、myPane的构造方法中,指定自己的Size,否则不能显示。Top

3 楼aico(aico)回复于 2005-07-29 13:09:48 得分 0

4、弧度=角度×Math.PI/180Top

4 楼huangdeji(活着就是等死)回复于 2005-07-29 13:18:12 得分 0

鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎鼎                                                                           鼎鼎鼎  
                                                                                鼎鼎鼎  
                                                                                鼎鼎鼎Top

5 楼aico(aico)回复于 2005-07-29 13:20:45 得分 10

//修改后代码  
  import   java.awt.*;  
  import   java.awt.event.*;  
  import   java.applet.*;  
  import   javax.swing.*;  
  import   javax.swing.event.*;  
   
  class   myPane  
          extends   JPanel   {  
      int   x1   =   300;  
      int   y1   =   300;  
      int   x2;  
      int   y2;  
      int   Angle;  
   
      public   myPane()   {  
          setPreferredSize(new   Dimension(800,600));                                                             //此处设Size  
      }  
   
      public   void   setAngle(int   a)   {  
          Angle   =   a;  
      }  
      public   int   getAngle()   {  
          return   Angle;  
      }  
   
      public   void   paintComponent(Graphics   g)   {  
          super.paintComponent(g);  
          x2   =   (int)   (x1   +   200   *   Math.cos(this.getAngle()*Math.PI/180));                         //此处计算  
          y2   =   (int)   (y1   +   200   *   Math.sin(this.getAngle()*Math.PI/180));  
          g.drawLine(x1,   y1,   x2,   y2);  
      }  
   
  }  
   
  public   class   Applet1  
          extends   JApplet   {  
      private   boolean   isStandalone   =   false;  
      myPane   p   =   new   myPane();  
      JPanel   control   =   new   JPanel();  
      JSlider   slider   =   new   JSlider(0,   360,   30);  
      //Get   a   parameter   value  
      public   String   getParameter(String   key,   String   def)   {  
          return   isStandalone   ?   System.getProperty(key,   def)   :  
                  (getParameter(key)   !=   null   ?   getParameter(key)   :   def);  
      }  
   
      //Construct   the   applet  
      public   Applet1()   {  
      }  
   
      //Initialize   the   applet  
      public   void   init()   {  
          try   {  
              jbInit();  
          }  
          catch   (Exception   e)   {  
              e.printStackTrace();  
          }  
      }  
   
      //Component   initialization  
      private   void   jbInit()   throws   Exception   {  
          this.setSize(new   Dimension(800,   600));  
   
          Container   contentPane   =   getContentPane();  
          contentPane.setLayout(new   BorderLayout());  
          contentPane.add(p,   BorderLayout.CENTER);  
          contentPane.add(control,   BorderLayout.EAST);  
          control.add(slider);  
          slider.setMajorTickSpacing(90);  
          slider.setMinorTickSpacing(30);  
          slider.setPaintLabels(true);  
          slider.setPaintLabels(true);  
          slider.addChangeListener(new   ChangeListener()   {  
              public   void   stateChanged(ChangeEvent   e)   {  
                  p.setAngle(slider.getValue());  
                  p.repaint();  
                  System.out.println("changed");  
              }  
          }  
          );  
      }  
   
      //Get   Applet   information  
      public   String   getAppletInfo()   {  
          return   "Applet   Information";  
      }  
   
      //Get   parameter   info  
      public   String[][]   getParameterInfo()   {  
          return   null;  
      }  
   
      //static   initializer   for   setting   look   &   feel  
      static   {  
          try   {  
              //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());  
              //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());  
          }  
          catch   (Exception   e)   {  
          }  
      }  
  }  
  Top

6 楼aico(aico)回复于 2005-07-29 13:22:01 得分 0

//测试HTML代码  
  <applet   code=Applet1.class   codebase=.   width=1000   height=600></applet>Top

7 楼secondflying(金乌贼)回复于 2005-07-29 15:21:36 得分 0

谢谢aico(aico),第一次发贴,就得到了这么详细的指导,多谢了!!Top

相关问题

  • 100分求助一个简单程序!!!!!!!!!!!
  • 300分,简单题目,要源程序
  • 简单的小程序?送20分!
  • 简单jsp程序,回答就给分!!!!
  • 新人50分求一简单程序
  • 50分求一个win32简单程序
  • 程序界面简单问题(送分)
  • 程序题, 比较简单, 给100分
  • 50分求一个简单程序
  • 50分求一个简单程序

关键词

  • swing
  • 鼎鼎鼎鼎鼎鼎鼎鼎
  • mypane
  • getangle
  • isstandalone
  • angle
  • paintcomponent
  • jpanel
  • math
  • jslider

得分解答快速导航

  • 帖主:secondflying
  • aico

相关链接

  • CSDN Java频道
  • Java类图书
  • Java类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo