I have been stuck on this now for almost a week, the problem is that the textfield keeps flickering. I have read that swing does not work well with fullscreen exclusive mode, but I'm not sure what else to do other than create my own class similar to a textfield and detect every single key press which doesn't seem the right thing to do. I have created a cut down version of the problem and have pasted below, any help would be appreciated :)
- If this line is uncommented "TextFieldTest.this.tfChatField.paint(g);" then it will display the textfield at the top of the screen, and a flickering textfield at the bottom(which is where it's meant to be)
Java Code:
package textfieldtest;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferStrategy;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class TextFieldTest implements Runnable {
private JFrame frame;
private BufferStrategy bufferStrategy;
private JTextField tfChatField;
public TextFieldTest(){
frame = new JFrame();
tfChatField = new JTextField("Type here to chat");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(tfChatField, BorderLayout.SOUTH);
frame.setVisible(true);
frame.createBufferStrategy(2);
DisplayMode dm = new DisplayMode(640,480,32,60);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();
gd.setFullScreenWindow(frame);
gd.setDisplayMode(dm);
bufferStrategy = frame.getBufferStrategy();
}
public static void main(String[] args) {
TextFieldTest test = new TextFieldTest();
new Thread(test).start();
}
public void render(final Graphics g){
g.setColor(Color.black);
g.fillRect(0, 0, 640, 480);
try {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
//TextFieldTest.this.tfChatField.paint(g);
}
});
} catch (InterruptedException ex) {
Logger.getLogger(TextFieldTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(TextFieldTest.class.getName()).log(Level.SEVERE, null, ex);
}
g.dispose();
bufferStrategy.show();
}
@Override
public void run() {
while(true){
Graphics g = bufferStrategy.getDrawGraphics();
render(g);
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
No comments:
Post a Comment