1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class J2dFrameWithStrategy extends JFrame {
BufferedImage bigImage;
BufferedImage backBuffer;
Graphics2D backGfx;
BufferStrategy bs;
final int bigXImagesize = 1024;
final int bigYImagesize = 10000;
final int dbXImagesize = 1024;
final int dbYImagesize = 750;
protected int fpsCounter = 0;
protected int workCounter = 0;
protected int index = 0;
protected String label = "Hello";
protected String label2 = "Hello";
Image image = null;
public J2dFrameWithStrategy(String label) {
super(label);
this.setVisible(true);
this.setBounds(0, 0, this.dbXImagesize, this.dbYImagesize);
//
this.setIgnoreRepaint(true);
this.createBufferStrategy(2);
bs = this.getBufferStrategy();
backGfx = (Graphics2D)bs.getDrawGraphics();
this.bigImage = new BufferedImage(this.bigXImagesize,
this.bigYImagesize, BufferedImage.TYPE_INT_RGB);
// create a big bufer to scroll from
Graphics2D gfx = (Graphics2D) this.bigImage.getGraphics();
gfx.setPaint(Color.ORANGE);
gfx.fill(new Rectangle2D.Float(0, 0, this.bigXImagesize,
this.bigYImagesize));
gfx.setPaint(Color.BLACK);
for (int y = 0; y < bigYImagesize; y = y + 100) {
gfx.drawString("line" + y, 100, y);
gfx.drawLine(0, y + 5, this.bigXImagesize, y + 5);
}
this.image = bigImage.getSubimage(0, 0, this.bigXImagesize, this.bigYImagesize);
}
public void paintMyCpm(Graphics g) {
// super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g.drawImage(this.backBuffer, 0, 0, null);
// increase the nb of fps
setFps(getFps() + 1);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
final J2dFrameWithStrategy frame = new J2dFrameWithStrategy("2D display");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Runnable mainLoop = new Runnable() {
public void run() throws RuntimeException {
while (true) {
// update the index
int newIndex = frame.getIndex() + 1;
if (newIndex > (frame.bigYImagesize - frame.dbYImagesize))
newIndex = 0;
frame.setIndex(newIndex);
frame.backGfx.drawImage(frame.image, 0, -frame.getIndex(), null);
frame.backGfx.setColor(Color.WHITE);
frame.backGfx.drawString(frame.getLabel(), 800, 200);
frame.backGfx.drawString(frame.getLabel2(), 800, 240);
// update the work count
frame.setWorkCounter(frame.getWorkCounter() + 1);
// paint it
frame.paintMyCpm(frame.backGfx);
try {
Thread.currentThread().sleep(1, 0);
//Thread.yield();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
frame.bs.show();
}
}
};
Thread mainLoopThread = new Thread(mainLoop, "Main loop thread");
// go
mainLoopThread.start();
Thread measurementThread = new Thread(mainLoop, "measurement thread");
Timer timer = new Timer("measurement thread");
TimerTask task = new TimerTask() {
public void run() {
// get the scores and set the label
frame.setLabel("FPS : " + frame.getFps());
frame.setLabel2("Loop count : " + frame.getWorkCounter());
// reset them
frame.setFps(0);
frame.setWorkCounter(0);
}
};
timer.schedule(task, 1000);
}
public synchronized int getIndex() {
return index;
}
public synchronized void setIndex(int index) {
this.index = index;
}
public synchronized int getFps() {
return fpsCounter;
}
public synchronized void setFps(int fps) {
this.fpsCounter = fps;
}
public synchronized String getLabel() {
return label;
}
public synchronized void setLabel(String label) {
this.label = label;
}
public synchronized String getLabel2() {
return label2;
}
public synchronized void setLabel2(String label2) {
this.label2 = label2;
}
public synchronized int getWorkCounter() {
return workCounter;
}
public synchronized void setWorkCounter(int workCounter) {
this.workCounter = workCounter;
}
} |
Partager