-
Screen Flickering
Hey there
I am following a java tutorial on youtube.
It is supposed to display a image when you move your mouse around or something.
But I just get a flickering green screen with a flickering image on it.
Can you please help me? thanks
heres my code:
Look Class:
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.ImageIcon;
import javax.swing.SwingUtilities;
public class Look extends Core implements MouseMotionListener, KeyListener, MouseListener {
public static void main(String[] args) {
new Look().run();
}
private Image bg;
private Robot robot;
private Point mouse;
private Point center;
private Point image;
private boolean centering;
public void init() {
super.init();
mouse = new Point();
center = new Point();
image = new Point();
centering = false;
try {
robot = new Robot();
recenterMouse();
mouse.x = center.x;
mouse.y = center.y;
} catch (Exception ex) {
System.out.println("exception 1");
}
Window w = s.getFullScreenWinodw();
w.addMouseListener(this);
w.addKeyListener(this);
bg = new ImageIcon("D:\\Users\\test\\Desktop\\JAVA STUFF\\Netbeans projects\\game\\src\\images\\face_up.png").getImage();
}
public synchronized void draw(Graphics2D g) {
int w = s.getWidth();
int h = s.getHeight();
image.x %= w;
image.y %= h;
if (image.x < 0) {
image.x += w;
}
if (image.y < 0) {
image.y += h;
}
int x = image.x;
int y = image.y;
g.drawImage(bg, x, y, null);
g.drawImage(bg, x - w, y, null);
g.drawImage(bg, x, y - h, null);
g.drawImage(bg, x - w, y - h, null);
}
//recenter mouse using robot
private synchronized void recenterMouse() {
Window w = s.getFullScreenWinodw();
if (robot != null && w.isShowing()) {
center.x = w.getWidth() / 2;
center.y = w.getHeight() / 2;
SwingUtilities.convertPointToScreen(center, w);
centering = true;
robot.mouseMove(center.x, center.y);
}
}
//mousemotion
public void mouseDragged(MouseEvent e){
mouseMoved(e);
}
public synchronized void mouseMoved(MouseEvent e){
if(centering && center.x == e.getX() && center.y == e.getY()){
centering = false;
}else{
int dx = e.getX() - mouse.x;
int dy = e.getY() - mouse.y;
image.x += dx;
image.y += dy;
recenterMouse();
}
mouse.x = e.getX();
mouse.y = e.getY();
}
//keylistener interface
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_ESCAPE){
stop();
}
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public void MouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
Core class:
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyListener;
import java.awt.Event;
public abstract class Core {
private static DisplayMode modes[] = {
new DisplayMode(800, 600, 32, 0),
new DisplayMode(800, 600, 24, 0),
new DisplayMode(800, 600, 16, 0),
new DisplayMode(640, 480, 32, 0),
new DisplayMode(640, 480, 24, 0),
new DisplayMode(640, 480, 16, 0),
};
private boolean running;
protected ScreenManager s;
private long startTime;
//stop method
public void stop(){
running = false;
}
//call init and gameloop
public void run(){
try{
init();
gameLoop();
}finally{
s.restoreScreen();
}
}
//set to full screen
public void init(){
s = new ScreenManager();
DisplayMode dm = s.findFirstCompatibleMode(modes);
s.setFullScreen(dm);
Window w = s.getFullScreenWinodw();
w.setFont(new Font("Arial", Font.PLAIN, 20));
w.setBackground(Color.GREEN);
w.setForeground(Color.WHITE);
running = true;
}
//main game loop
public void gameLoop(){
long startTIme = System.currentTimeMillis();
long cumTime = startTime;
while(running){
long timePassed = System.currentTimeMillis() - cumTime;
cumTime += timePassed;
update(timePassed);
Graphics2D g = s.getGraphics();
draw(g);
g.dispose();
s.update();
try{
Thread.sleep(20);
}catch(Exception e){}
}
}
//update animation
public void update(long timePassed){
}
//draw to screen
public abstract void draw(Graphics2D g);{}
}
ScreenManager class:
Code:
import java.awt.*;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.lang.reflect.InvocationTargetException;
import javax.swing.JFrame;
public class ScreenManager {
private GraphicsDevice vc;
//give vc access to monitor
public ScreenManager() {
GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
vc = e.getDefaultScreenDevice();
}
public DisplayMode[] getCompatibleDisplayModes() {
return vc.getDisplayModes();
}
//compares dm passed in...
public DisplayMode findFirstCompatibleMode(DisplayMode modes[]) {
DisplayMode goodModes[] = vc.getDisplayModes();
for (int x = 0; x < modes.length; x++) {
for (int y = 0; y < goodModes.length; y++) {
if (displayModesMatch(modes[x], goodModes[y])) {
return modes[x];
}
}
}
return null;
}
//get current dm
public DisplayMode getCurrentDisplayMode() {
return vc.getDisplayMode();
}
//chechs if two modes match eachother
public boolean displayModesMatch(DisplayMode m1, DisplayMode m2) {
if (m1.getWidth() != m2.getWidth() || m1.getHeight() != m2.getHeight()) {
return false;
}
if (m1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m2.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m1.getBitDepth() != m2.getBitDepth()) {
return false;
}
if (m1.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m2.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m1.getRefreshRate() != m2.getRefreshRate()) {
return false;
}
return true;
}
//make frame full screen
public void setFullScreen(DisplayMode dm) {
JFrame f = new JFrame();
f.setUndecorated(true);
f.setIgnoreRepaint(true);
f.setResizable(false);
vc.setFullScreenWindow(f);
if (dm != null && vc.isDisplayChangeSupported()) {
try {
vc.setDisplayMode(dm);
} catch (Exception ex) {
}
}
f.createBufferStrategy(2);
}
//we will set Graphics object = to this
public Graphics2D getGraphics() {
Window w = vc.getFullScreenWindow();
if (w != null) {
BufferStrategy s = w.getBufferStrategy();
return (Graphics2D) s.getDrawGraphics();
} else {
return null;
}
}
//updates display
public void update() {
Window w = vc.getFullScreenWindow();
if (w != null) {
BufferStrategy s = w.getBufferStrategy();
if (!s.contentsLost()) {
s.show();
}
}
}
public Window getFullScreenWinodw() {
return vc.getFullScreenWindow();
}
public int getWidth() {
Window w = vc.getFullScreenWindow();
if (w != null) {
return w.getWidth();
} else {
return 0;
}
}
public int getHeight() {
Window w = vc.getFullScreenWindow();
if (w != null) {
return w.getHeight();
} else {
return 0;
}
}
public void restoreScreen(){
Window w = vc.getFullScreenWindow();
if(w != null){
w.dispose();
}
vc.setFullScreenWindow(null);
}
//create image compatible with monitor
public BufferedImage createCompatibleImage(int w, int h, int t){
Window win = vc.getFullScreenWindow();
if(win != null){
GraphicsConfiguration gc = win.getGraphicsConfiguration();
return gc.createCompatibleImage(w, h, t);
}
return null;
}
}
-
Do you have some doc describing what the code is supposed to do?
How are you sure that it is not doing what it is supposed to do?
-