2013-01-07 3 views
1

Possible en double:
Iframe over applet flickeringJava Applet vacillant

Quand je place iframe sur l'applet, il est vacillante et clignote beaucoup.

C'est ce que je pense en causer.

// 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(); 

Voici le code source:

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); 

} 

    } 

Mais est-il possible de le faire d'une autre manière?

+0

Assurez-vous que le tampon est double. – aioobe

+0

Comment puis-je vérifier cela? Je suis nouveau à Java –

+1

BTW - 'AvatarClient.instance.getGraphics()' a une très mauvaise odeur de code. Ce n'est pas quelque chose que vous feriez avec n'importe quel composant AWT ou Swing. Pour une meilleure aide plus tôt, postez un [SSCCE] (http://sscce.org/). –

Répondre

1

Le double tampon résoudrait cela.

Fondamentalement, au lieu de dessiner sur le canevas en direct, vous devez dessiner sur un canevas qui n'est pas affiché. Immédiatement après avoir dessiné, permutez les deux instances de la toile, en faisant de la toile "cachée" la toile affichée.

http://en.wikipedia.org/wiki/Multiple_buffering#Double_buffering_in_computer_graphics fournira plus de détails à ce sujet.

Vous pouvez trouver un exemple sur la façon de faire ceci par exemple http://www.apl.jhu.edu/Notes/LMBrown/courses/605-481/notes/Concurrent-Programming/Double-Buffer-Example.html, mais fondamentalement le mot-clé que vous cherchez est "double tampon" pour trouver des tonnes de détails sur la résolution du scintillement.