Java 小程序闪烁

标签 java swing applet

Possible Duplicate:
Iframe over applet flickering

当我放置iframe时在小程序上它闪烁并且闪烁很多。

我认为这就是造成它的原因。

    // Create a timer that will handle the canvas updates, effectively drawing it at the desired FPS
    tickTimer = new javax.swing.Timer(1000/Constants.APPLET_FRAMERATE, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            update(AvatarClient.instance.getGraphics());               


        }
     });

    // Start the timer
    tickTimer.start();

这是源代码:

 package main;



   import java.applet.Applet;
   import java.awt.Dimension;
   import java.awt.Graphics;
   import java.awt.MediaTracker;
   import java.awt.event.ActionEvent;
   import java.awt.event.ActionListener;
   import javascript.JavascriptCommands;

   import javax.swing.JOptionPane;
   import javax.swing.Timer;

   import client.GraphicsHandler;
   import client.avatar.AvatarController;
   import client.guiboxes.QuestionBox;
   import client.moods.MoodController;
   import client.rooms.RoomController;
   import client.tools.StuffPreloader;
   import networking.ConnectionHandler;
   import networking.NetworkMessage;
   import networking.NetworkingConstants;

   import misc.Constants;
   import misc.Language;



  public class AvatarClient extends jApplet{

/**
 * 
 */
private static final long serialVersionUID = 3792036962966286054L;
public GraphicsHandler canvas;
public ConnectionHandler connection;
Timer tickTimer;
public MediaTracker mediaTracker;

public String userName = "a"; //$NON-NLS-1$
public String localizationFolder = "";

public static AvatarClient instance;

/* called when the Applet is loaded */
public void init()    {
    instance = this;


    //setSize(new Dimension( Constants.APPLET_WIDTH,  Constants.APPLET_HEIGHT ) );
    // SET THE APPLET SIZE

    // Get the applet parameters
    userName = getParameter("userName"); //$NON-NLS-1$
    String password = getParameter("password"); //$NON-NLS-1$
    localizationFolder = getParameter("localization");


    // Create a mediatracker for image loding
    mediaTracker = new MediaTracker(this);



    System.out.println("Jesper er sej!");


    canvas = new GraphicsHandler(this);// Create a canvas, all the drawing is done within the canvas        
    canvas.setStatusMsg(Language.getString("AvatarClient.connectMessage")); // Apply a status message //$NON-NLS-1$
    canvas.setPreferredSize(new Dimension(Constants.APPLET_WIDTH,  Constants.APPLET_HEIGHT));

    // Adjust the applet options as well ( THIS SHOULD REMOVE THE BLINKING )
    setBackground(new java.awt.Color(229, 229, 229));
    setMaximumSize(new java.awt.Dimension(Constants.APPLET_WIDTH,  Constants.APPLET_HEIGHT));
    setPreferredSize(new java.awt.Dimension(Constants.APPLET_WIDTH,  Constants.APPLET_HEIGHT));
    setSize(new java.awt.Dimension(Constants.APPLET_WIDTH,  Constants.APPLET_HEIGHT ));
    setLayout(null);
    this.setIgnoreRepaint(true);
    this.add(canvas);
    validate();

    // FINALLY, create a connection handler thread ( It tries to connect to the as the first thing it does )
    connection= new ConnectionHandler(this, userName, password);
    new Thread(connection).start();





    // Create a timer that will handle the canvas updates, effectively drawing it at the desired FPS
    tickTimer = new javax.swing.Timer(1000/Constants.APPLET_FRAMERATE, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            update(AvatarClient.instance.getGraphics());               


        }
     });

    // Start the timer
    tickTimer.start();




    // Add mouse and keyboard listeners to the canvas of this applet
    canvas.addMouseListener(EventsHandler.getInstance());
    canvas.addKeyListener(EventsHandler.getInstance());
    canvas.addMouseMotionListener(EventsHandler.getInstance());
    //System.out.println(canvas.);
    //this.setFocusable(false);

    new JavascriptCommands();


    // Preload some of the gui elements
    StuffPreloader.instance.loadStuff();




    // TO OVERCOME THE SECURITY PROBLEMS FOR CALLING NEW NOTIFICATIONS FROM JS
    Timer notif = new javax.swing.Timer(100, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(newNotif)    {
                GraphicsHandler.instance.addQbox(notif_title, notif_text, QuestionBox.QUESTION_TIPBOX, 0);
                newNotif = false;
            }


        }
     });

    // Start the timer
    notif.start();
    notif.setRepeats(true);







    // Get focus to this applet
    getFocusBack();


}


boolean newNotif = false;
String notif_text, notif_title;


// START AND WHEN USER RETURNS
public void start() {
    //setSize(new Dimension( Constants.APPLET_WIDTH,  Constants.APPLET_HEIGHT ) );
    getFocusBack();




    GraphicsHandler.instance.stepsToDraw = 30;

}

public void stop()  {
    //connection.closeConnection();
}


/* Applet termination */
public void destroy()    {
    tickTimer.stop(); // Stop the update timer
}

// OVERRIDE
public void update(Graphics g)    {
    canvas.update(g);
}

public void paint(Graphics g)    {
        //canvas.requestFocus(); // Try to get focus so the user doesnt have to click for chat input
}



public void showError(String message)   {
JOptionPane.showMessageDialog(this,
        "Error:\n\n" + message, //$NON-NLS-1$
        "Error!", //$NON-NLS-1$
        JOptionPane.ERROR_MESSAGE);
}






public void getFocusBack()  {
    canvas.requestFocus(); // Try to get focus so the user doesnt have to click for chat input
    canvas.requestFocusInWindow();
}


/**************************
/** JAVASCRIPT COMMANDS
 * *****************************/

public void changeRoom(int id)  {
    getFocusBack();

     NetworkMessage msg = new NetworkMessage();
     msg.add(NetworkingConstants.N_ROOMCHANGE);
     msg.add((int)id); // REquest for data
     msg.send(connection.out);
}

public void changeMood(int id)  {
    getFocusBack();

    MoodController.instance.SetMoodByIndex(id);
}

public void nextMood()  {
    getFocusBack();

    MoodController.instance.SetNextMood();
}

public void previousMood()  {
    getFocusBack();

    MoodController.instance.SetPreviousMood();
}



public void openLogView()   {
    getFocusBack();
    AvatarController.instance.outputLog();
}

public void refreshSidebar()    {
    RoomController.instance.updateSidebarInfo(true);
}

public void openWardrobe()  {
    getFocusBack();

    // OPEN WARDROBE
    NetworkMessage msg = new NetworkMessage();
    msg.add(NetworkingConstants.N_WARDROBEUPDATE);
    msg.add((short)-2);
    msg.send(ConnectionHandler.instance.out);
}

public void notifyB()   {
    System.out.println("addnotification: ");
}



public void addNotification(String title, String text)  {
    getFocusBack();
    notif_title = title;
    notif_text = text;
    newNotif = true;

    //GraphicsHandler.instance.addQbox("This is a test message lores ipsum testing the line flow with a longer text, I wonder how it will work", QuestionBox.QUESTION_APARTMENT, 123); //$NON-NLS-1$ //$NON-NLS-2$
    //GraphicsHandler.instance.addQbox(text, title, QuestionBox.QUESTION_TIPBOX, 0); 



}

public void placeItem(int itemID)   {
    NetworkMessage msg = new NetworkMessage();
    msg.add(NetworkingConstants.N_ITEMPLACEMENT);
    msg.add((short)1); // Placing the item
    msg.add((short)itemID);
    msg.send(ConnectionHandler.instance.out);
}

public void forceFocus() {

    Timer focusBack = new javax.swing.Timer(150, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            getFocusBack();           


        }
     });

    // Start the timer
    focusBack.start();
    focusBack.setRepeats(false);

}

   }

但是可以用其他方式实现吗?

最佳答案

双缓冲可以解决这个问题。

基本上,您应该在未显示的 Canvas 上绘制,而不是在实时 Canvas 上绘制。绘制后立即交换两个 Canvas 实例,使“隐藏” Canvas 成为显示的 Canvas 。

http://en.wikipedia.org/wiki/Multiple_buffering#Double_buffering_in_computer_graphics将提供有关此的更多详细信息。

您可以在 http://www.apl.jhu.edu/Notes/LMBrown/courses/605-481/notes/Concurrent-Programming/Double-Buffer-Example.html 中找到有关如何执行此操作的示例。 ,但基本上您要寻找的关键字是“双缓冲”,以查找有关解决闪烁问题的大量详细信息。

关于Java 小程序闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14201489/

相关文章:

java批量生产者消费者

java - 使用 java 调用过程时显示参数号 2 不是 OUT 参数

java - 插入新数据后用数据库数据更新 JcomboBox 中的数据

java - 如何更改单个 JTree 节点的样式(颜色、字体)

Java Applet 通过 JNLP : System. 属性不是 "set"

java - 丢失 JRE 7 安全警报和警告

java - 在 OS X 10.9.0 Mavericks 不安全模式下启用对 java 小程序的文件系统访问

java - 使用 rxJava 的下游运算符的数据库(SQLite)事务

java - 将 Resteasy 与 javassist 一起使用?

java - 如何使用 BorderLayout/GroupLayout 从 JPanel 中删除特定元素?