Skip to content

Commit

Permalink
remove window decorations, reimplement graphics constants and render …
Browse files Browse the repository at this point in the history
…test
  • Loading branch information
rystills committed Mar 2, 2019
1 parent 974eb01 commit e193b0f
Showing 1 changed file with 56 additions and 51 deletions.
107 changes: 56 additions & 51 deletions src/TSpinTutor.java
Original file line number Diff line number Diff line change
@@ -1,59 +1,64 @@
import java.awt.*;
import javax.swing.*;
import static java.awt.GraphicsDevice.WindowTranslucency.*;
import java.util.Random;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TSpinTutor extends JFrame {
public TSpinTutor() {
super("GradientTranslucentWindow");

//global random generator
static Random r = new Random();
//graphics constants
static int sw = 1920;
static int sh = 1080;
static int bSize = 36;
static int bSizePrev = 28;
static int bSizePrevFuture = 22;
static int gridLeft = 308;
static int gridRight = 632;
static int gridTop = 160;
static int gridBot = 844;
static int numRows = 20;
static int numCols = 10;
//timing vars
public static long time = 0;
public static int fps = 30;

TSpinTutor() {
//set the frame properties for a transparent, always on top overlay
setUndecorated(true);
setSize(sw, sh);
setLocation(0, 0);
setLayout(new BorderLayout());
setAlwaysOnTop(true);
setBackground(new Color(0,0,0,0));
setSize(new Dimension(300,200));
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Paint p = new Color(0,0,0,0);

JPanel panel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
if (g instanceof Graphics2D) {
Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(p);
g2d.fillRect(0, 0, getWidth(), getHeight());
}
}
};
setContentPane(panel);
setLayout(new GridBagLayout());
add(new JButton("I am a Button"));
}

public static void main(String[] args) {
// Determine what the GraphicsDevice can support.
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
boolean isPerPixelTranslucencySupported =
gd.isWindowTranslucencySupported(PERPIXEL_TRANSLUCENT);

//If translucent windows aren't supported, exit.
if (!isPerPixelTranslucencySupported) {
System.out.println(
"Per-pixel translucency is not supported");
System.exit(0);
}

public static class Panel extends JPanel {
public Panel() {
setBackground(new Color(0,0,0,0));
}

JFrame.setDefaultLookAndFeelDecorated(true);

// Create the GUI on the event-dispatching thread
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TSpinTutor gtw = new TSpinTutor();

// Display the window.
gtw.setVisible(true);
}
});
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int x = gridLeft + bSize*r.nextInt(numCols);
int y = gridTop + bSize*r.nextInt(numRows);
g.setColor(Color.RED);
g.fillRect(x,y,bSize,bSize);
}
}

public static void main(String[] args) {
final TSpinTutor tutor = new TSpinTutor();
Panel panel = new Panel();
tutor.add(panel);
tutor.setVisible(true);
while(true) {
time = System.nanoTime();
tutor.repaint();
time = System.nanoTime() - time;
try {Thread.sleep( (1000/fps) - (time/1000000) );} catch (Exception e){}
}
}
}

0 comments on commit e193b0f

Please sign in to comment.