Core-Dump said:
aarrgh my eyes ! :)
Sure it is possible to create this in jme but it might be a bit of a overkill.
What do you want to do with the application, use it as a applet or desktop app?
I want to create it as a desktop application , that would be used to test the vision system of babies until the age of 2 years.
Why am I not just moving the white bars ? , this is a good question, i already implemented the idea on that way but it did not work, attached is the code for that. I think the problem is the snychronizatipon with the monitir refresh rate but I do not know how to do this, in the previous version attached above , i tried to use double buffering but the result is as you see.
import java.awt.Toolkit;
import javax.swing.ScrollPaneConstants;
import javax.swing.JScrollPane;
import javax.swing.JViewport;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.awt.Graphics;
import java.awt.Dimension;
import javax.swing.JFrame;
import java.awt.event.ActionListener;
import java.awt.Point;
import java.awt.event.ActionEvent;
import javax.swing.Timer;
import javax.swing.KeyStroke;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import java.awt.GraphicsConfiguration;
import java.awt.Frame;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.DisplayMode;
import java.awt.image.BufferStrategy;
import java.awt.Font;
import java.awt.BorderLayout;
public class DrawingBars {
double[] width = {17.453, 13.864, 11.012, 8.747, 6.948, 5.519, 4.384, 3.482,
2.766, 2.197, 1.745, 1.386,
29.089, 23.106, 18.354, 14.579, 11.580, 9.199, 7.307,
5.804,4.610, 3.662, 2.909, 2.311, 1.835, 1.458, 1.158, 0.92,
58.178, 46.212, 36.708, 29.158, 23.161, 18.397, 14.614,
11.608,9.221, 7.324, 5.818, 4.621, 3.671, 2.916, 2.316, 1.840,
1.461, 1.161, 0.992};
double[] snelling_ratio = {0.010, 0.013,0.016, 0.020, 0.025, 0.032, 0.040,0.050,
0.063,0.079,0.100,0.126,
0.010, 0.013, 0.016, 0.020, 0.025, 0.032, 0.040, 0.050,
0.063, 0.079, 0.100, 0.126, 0.158, 0.200, 0.251,0.316,
0.010,0.013,0.016,0.020,0.025,0.032,0.040,0.050,0.063,
0.079, 0.100, 0.126, 0.158, 0.200, 0.251, 0.316, 0.398,
0.501,0.631};
int bar_index;
int width_In_Pixel;
int max_width_In_Pixel;
int scrolling_delta;
int max_scroll_time, min_scroll_time, timerUpdate;
float scrolling_time;
String direction, snellingANDgroup, snelling, group;
BufferedImage image;
JLabel label,snellingLabel;
JScrollPane scrollPane;
int dots_per_inch = Toolkit.getDefaultToolkit().getScreenResolution();
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
JFrame frame;
Graphics g;
public DrawingBars() {
this.bar_index = 0;
max_width_In_Pixel = (int) ((width[28] * dots_per_inch) / 25.4);
width_In_Pixel = (int) ((width[bar_index] * dots_per_inch) / 25.4);
direction = "right";
this.max_scroll_time = 5;
this.min_scroll_time = 1;
this.timerUpdate = 50;
this.scrolling_time = this.max_scroll_time;
this.scrolling_delta = dim.width /
(int)(scrolling_time * 1000 / timerUpdate);
this.snellingANDgroup = "Snelling Ratio : 0.010, Group : A";
this.group = "A";
}
public void triggerBars() {
image = new BufferedImage(dim.width + max_width_In_Pixel * 2,
dim.height, BufferedImage.TYPE_INT_RGB);
label = new JLabel(new ImageIcon(image));
snellingLabel = new JLabel(snellingANDgroup);
snellingLabel.setText("ooof");
snellingLabel.setBackground( Color.BLACK );
snellingLabel.setForeground( Color.YELLOW );
snellingLabel.setOpaque( true );
snellingLabel.setFont( new Font( "Dialog", Font.BOLD, 15 ) );
snellingLabel.setHorizontalTextPosition( JLabel.CENTER );
snellingLabel.setEnabled( true );
drawImage();
scrollPane = new JScrollPane(label);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.
VERTICAL_SCROLLBAR_NEVER);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.
HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.getViewport().setScrollMode(JViewport.
BLIT_SCROLL_MODE);
// GraphicsDevice screen = screenSetup();
// DisplayMode dmode = new DisplayMode(dim.width, dim.height, 32, 60);
// BufferStrategy bufstrat = frame.getBufferStrategy();
// Graphics g = bufstrat.getDrawGraphics();
// GraphicsConfiguration gc = device.getDefaultConfiguration();
frame = new JFrame();
frame.setUndecorated(true);
//frame.createBufferStrategy(2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(scrollPane);
frame.getContentPane().add(snellingLabel,BorderLayout.SOUTH);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
GraphicsEnvironment env = GraphicsEnvironment.
getLocalGraphicsEnvironment();
GraphicsDevice device = env.getDefaultScreenDevice();
device.setFullScreenWindow(frame);
// screen.setFullScreenWindow(frame);
// screen.setDisplayMode(dmode);
ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JViewport vp = scrollPane.getViewport();
int x = vp.getViewPosition().x;
if (direction.equals("right")) {
x -= scrolling_delta;
if (x < 0) {
x += (-x+width_In_Pixel * 2-1)/(width_In_Pixel * 2) * width_In_Pixel * 2;
}
} else if (direction.equals("left")) {
x += scrolling_delta;
if (x >= width_In_Pixel * 2) {
x -= x/(width_In_Pixel * 2) * width_In_Pixel * 2;
}
}
vp.setViewPosition(new Point(x, 0));
}
};
new Timer(timerUpdate, al).start();
}
public void drawImage() {
snellingLabel.setText(snellingANDgroup);
g = image.getGraphics();
g.setColor(Color.WHITE);
g.clearRect(0, 0, dim.width + max_width_In_Pixel * 2, dim.height);
for (int x = 0; x < dim.width + width_In_Pixel * 2;
x += width_In_Pixel * 2) {
g.fillRect(x, 0, width_In_Pixel, dim.height);
}
}
public void keyBoardListening() {
scrollPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.
getKeyStroke("DOWN"), "decreaseWidth");
scrollPane.getActionMap().put("decreaseWidth",
new AbstractAction("decreaseWidth") {
public void actionPerformed(ActionEvent evt) {
increaseBarIndex();
setWidthInPixel();
drawImage();
}
}
);
scrollPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.
getKeyStroke("UP"), "increaseWidth");
scrollPane.getActionMap().put("increaseWidth",
new AbstractAction("increaseWidth") {
public void actionPerformed(ActionEvent evt) {
decreaseBarIndex();
setWidthInPixel();
drawImage();
}
}
);
scrollPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.
getKeyStroke("A"), "GroupA");
scrollPane.getActionMap().put("GroupA", new AbstractAction("GroupA") {
public void actionPerformed(ActionEvent evt) {
setBarIndex(0);
setWidthInPixel();
drawImage();
}
}
);
scrollPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.
getKeyStroke("B"), "GroupB");
scrollPane.getActionMap().put("GroupB", new AbstractAction("GroupB") {
public void actionPerformed(ActionEvent evt) {
setBarIndex(12);
setWidthInPixel();
drawImage();
}
}
);
scrollPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.
getKeyStroke("C"), "GroupC");
scrollPane.getActionMap().put("GroupC", new AbstractAction("GroupC") {
public void actionPerformed(ActionEvent evt) {
setBarIndex(28);
setWidthInPixel();
drawImage();
}
}
);
scrollPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.
getKeyStroke("RIGHT"), "changeDirectionToRight");
scrollPane.getActionMap().put("changeDirectionToRight",
new AbstractAction(
"changeDirectionToRight") {
public void actionPerformed(ActionEvent evt) {
setDirection("right");
drawImage();
}
}
);
scrollPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.
getKeyStroke("LEFT"), "changeDirectionToLeft");
scrollPane.getActionMap().put("changeDirectionToLeft",
new AbstractAction(
"changeDirectionToLeft") {
public void actionPerformed(ActionEvent evt) {
setDirection("left");
drawImage();
}
}
);
scrollPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.
getKeyStroke("Z"), "increaseSpeed");
scrollPane.getActionMap().put("increaseSpeed",
new AbstractAction("increaseSpeed") {
public void actionPerformed(ActionEvent evt) {
increaseSpeed();
drawImage();
}
}
);
scrollPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.
getKeyStroke("X"), "decreaseSpeed");
scrollPane.getActionMap().put("decreaseSpeed",
new AbstractAction("decreaseSpeed") {
public void actionPerformed(ActionEvent evt) {
decreaseSpeed();
drawImage();
}
}
);
scrollPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.
getKeyStroke("ESCAPE"), "EXIT");
scrollPane.getActionMap().put("EXIT",
new AbstractAction("EXIT") {
public void actionPerformed(ActionEvent evt) {
System.exit(0);
}
}
);
}
public void setBarIndex(int barIndex) {
this.bar_index = barIndex;
if(bar_index == 0)
group = "A";
else if(bar_index == 12)
group = "B";
else if(bar_index == 28)
group = "C";
snelling = " "+ snelling_ratio[bar_index];
snellingANDgroup = snelling + " " + group ;
}
public int getBarIndex() {
return this.bar_index;
}
public void increaseBarIndex() {
if (bar_index == 11) {
bar_index = 11;
} else if (bar_index == 27) {
bar_index = 27;
} else if (bar_index == 46) {
bar_index = 46;
} else {
bar_index++;
}
snelling = " " + snelling_ratio[bar_index];
snellingANDgroup = snelling + " " + group ;
}
public void decreaseBarIndex() {
if (bar_index == 0) {
bar_index = 0;
} else if (bar_index == 12) {
bar_index = 12;
} else if (bar_index == 28) {
bar_index = 28;
} else {
bar_index--;
}
snelling = " " + snelling_ratio[bar_index];
snellingANDgroup = snelling + " " + group ;
}
public void setWidthInPixel() {
this.width_In_Pixel = (int) ((width[bar_index] * dots_per_inch) /
25.4);
}
public int getWidthInPixel() {
return width_In_Pixel;
}
public void setDirection(String direction) {
this.direction = new String(direction);
}
public void increaseSpeed() {
if (scrolling_time == min_scroll_time) {
scrolling_time = min_scroll_time;
} else {
scrolling_time -= 0.5;
scrolling_delta = dim.width /
(int)(scrolling_time * 1000 / timerUpdate);
System.out.println("Speed increased, time is now " + scrolling_time +
" and delta is" + scrolling_delta );
}
}
public void decreaseSpeed() {
if (scrolling_time == max_scroll_time) {
scrolling_time = max_scroll_time;
} else {
scrolling_time += 0.5;
scrolling_delta = dim.width /
(int)(scrolling_time * 1000 / timerUpdate);
System.out.println("Speed decreased, time is now " + scrolling_time +
" and delta is" + scrolling_delta );
}
}
private static GraphicsDevice screenSetup() {
GraphicsEnvironment ge = GraphicsEnvironment.
getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
GraphicsDevice gdev = gs[0];
return gdev;
}
}
package sliding_bars;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
public class Sliding
{
public static void main(String[] args)
{
DrawingBars drawingBars = new DrawingBars();
drawingBars.triggerBars();
drawingBars.keyBoardListening();
}
}