黑马程序员技术交流社区

标题: 有关Applet的问题<已解决>. [打印本页]

作者: 郑冬    时间: 2012-6-9 11:22
标题: 有关Applet的问题<已解决>.
本帖最后由 郑冬 于 2012-6-10 16:32 编辑

代码如下
import javax.swing.*;
import java.applet.*;
import java.awt.*;
import java.net.*;
import java.awt.image.BufferedImage;
import java.awt.event.*;

public class ScApplet extends Applet
{
final String testStr = "Java真好玩,学习很有趣!";
private int nextX;
private int nextY;
private Color nextColor;
private Font nextFont;
java.util.Random rand = new java.util.Random(System.currentTimeMillis());
public void init()
{
  Timer timer = new Timer(200 , new ActionListener()
  {
   public void actionPerformed(ActionEvent event)
   {
   
    nextX = rand.nextInt(400);
    nextY = rand.nextInt(300);
    nextColor = new Color(rand.nextInt(255) , rand.nextInt(255) , rand.nextInt(255));
    nextFont = new Font("Times" , Font.BOLD , rand.nextInt(60));
    repaint();
   }
  });
  timer.start();
}

public void paint(Graphics g)
{
  g.setColor(nextColor);
  g.setFont(nextFont);
  g.drawString(testStr, nextX, nextY);
}
public static void main(String[] args)
{
  JFrame jf = new JFrame();
  ScApplet panel = new ScApplet();
  panel.setPreferredSize(new Dimension(400, 300));
  panel.init();
  panel.start();
  jf.add(panel);
  jf.pack();
  jf.setVisible(true);
}
}
运行结果如下:
[img]file:///C:/Users/think/AppData/Roaming/Tencent/Users/915458516/QQ/WinTemp/RichOle/MUMT9YG[W%7DH0EQPSG%7BTBU_G.jpg[/img]
程序中重写print方法时绘制了一个字符串,但运行结果上出现了很多的字符串,是为什么?

作者: 吴小铁你好    时间: 2012-6-9 11:44
楼主把print 写错了,所以就没覆盖print方法:
  1. package pack;

  2. import javax.swing.*;
  3. import java.applet.*;
  4. import java.awt.*;
  5. import java.awt.event.*;

  6. public class ScApplet extends Applet
  7. {
  8. final String testStr = "Java真好玩,学习很有趣!";
  9. private int nextX;
  10. private int nextY;
  11. private Color nextColor;
  12. private Font nextFont;
  13. java.util.Random rand = new java.util.Random(System.currentTimeMillis());
  14. public void init()
  15. {
  16.   Timer timer = new Timer(200 , new ActionListener()
  17.   {
  18.    public void actionPerformed(ActionEvent event)
  19.    {
  20.    
  21.     nextX = rand.nextInt(400);
  22.     nextY = rand.nextInt(300);
  23.     nextColor = new Color(rand.nextInt(255) , rand.nextInt(255) , rand.nextInt(255));
  24.     nextFont = new Font("Times" , Font.BOLD , rand.nextInt(60));
  25.     repaint();
  26.    }
  27.   });
  28.   timer.start();
  29. }

  30. public void print(Graphics g)
  31. {
  32.   g.setColor(nextColor);
  33.   g.setFont(nextFont);
  34.   g.drawString(testStr, nextX, nextY);
  35. }
  36. public static void main(String[] args)
  37. {
  38.   JFrame jf = new JFrame();
  39.   ScApplet panel = new ScApplet();
  40.   panel.setPreferredSize(new Dimension(400, 300));
  41.   panel.init();
  42.   panel.start();
  43.   jf.add(panel);
  44.   jf.pack();
  45.   jf.setVisible(true);
  46. }
  47. }
复制代码

作者: 郑冬    时间: 2012-6-9 12:23
吴小铁你好 发表于 2012-6-9 11:44
楼主把print 写错了,所以就没覆盖print方法:

是我把问题写错了,程序中重写paint方法时绘制了一个字符串,但运行结果上出现了很多的字符串,是为什么?

作者: 吴小铁你好    时间: 2012-6-9 13:05
你运行这个试下,我的好像不是。
  1. package pack;

  2. import javax.swing.*;
  3. import java.applet.*;
  4. import java.awt.*;
  5. import java.awt.event.*;

  6. public class ScApplet extends Applet
  7. {
  8. final String testStr = "Java真好玩,学习很有趣!";
  9. private int nextX;
  10. private int nextY;
  11. private Color nextColor;
  12. private Font nextFont;
  13. java.util.Random rand = new java.util.Random(System.currentTimeMillis());
  14. public void init()
  15. {
  16.   Timer timer = new Timer(200 , new ActionListener()
  17.   {
  18.    public void actionPerformed(ActionEvent event)
  19.    {
  20.    
  21.     nextX = rand.nextInt(400);
  22.     nextY = rand.nextInt(300);
  23.     nextColor = new Color(rand.nextInt(255) , rand.nextInt(255) , rand.nextInt(255));
  24.     nextFont = new Font("Times" , Font.BOLD , rand.nextInt(60));
  25.     repaint();
  26.    }
  27.   });
  28.   timer.start();
  29. }

  30. public void paint(Graphics g)
  31. {
  32.   super.paint(g);
  33.   g.setColor(nextColor);
  34.   g.setFont(nextFont);
  35.   g.drawString(testStr, nextX, nextY);
  36. }
  37. public static void main(String[] args)
  38. {
  39.   JFrame jf = new JFrame();
  40.   ScApplet panel = new ScApplet();
  41.   panel.setPreferredSize(new Dimension(400, 300));
  42.   panel.init();
  43.   panel.start();
  44.   jf.add(panel);
  45.   jf.pack();
  46.   jf.setVisible(true);
  47. }
  48. }
复制代码

作者: 郑冬    时间: 2012-6-9 13:14
吴小铁你好 发表于 2012-6-9 13:05
你运行这个试下,我的好像不是。

运行了你的代码,也是这样的,很多字符串在闪动,怎么样能只有一个字符串呢?
作者: 郑冬    时间: 2012-6-9 20:01
程序中重写paint方法时绘制了一个字符串,但运行结果上出现了很多的字符串,是为什么?

作者: 彭翠    时间: 2012-6-9 21:54
郑冬 发表于 2012-6-9 20:01
程序中重写paint方法时绘制了一个字符串,但运行结果上出现了很多的字符串,是为什么?
...

这是由于Swing的JApplet的绘图机制决定的,Swing组件的绘图默认启动双缓冲机制,当程序调用paint方法进行绘制时,实际上是先绘制到内存中的图像里,然后才将该图像绘制到Swing组件上,因而每次调用paint方法进行绘制时,实际上是向内存中图像上添加一条字符串,所以会看到程序执行的效果,如果想看到面板上只有一条字符串的闪烁效果,则将程序改为继承Applet即可,Applet没有双缓冲机制,所以程序执行的结果是Applet面板上最多只有一条字符串!
作者: 郑冬    时间: 2012-6-9 22:03
标题: 有关Applet的问题<已解决>.
本帖最后由 郑冬 于 2012-6-10 16:29 编辑
彭翠 发表于 2012-6-9 21:54
这是由于Swing的JApplet的绘图机制决定的,Swing组件的绘图默认启动双缓冲机制,当程序调用paint方法进行 ...

很有帮助,谢谢




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2