Skip to content

Instantly share code, notes, and snippets.

@cambiata
Created November 12, 2014 05:45
Show Gist options
  • Save cambiata/d025fc7e66352a0b0681 to your computer and use it in GitHub Desktop.
Save cambiata/d025fc7e66352a0b0681 to your computer and use it in GitHub Desktop.
Minimalistic Haxe Java graphics example
-java bin
-main Main
package;
import java.lang.System;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Color;
/**
* Java graphics example
* Fully based on Justinfront's code: http://old.haxe.org/doc/java/graphics2d
*/
class Main extends JFrame
{
static function main()
{
trace ('hello');
new Main();
}
public function new() {
// The view windows title.
super('Graphics 2D Example');
// Specify to use Hardware rendering.
System.setProperty("sun.java2d.opengl","True");
// Set window size to 700 pixels wide by 500 pixels high.
setSize( 700, 500 );
// Add a close button to window.
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
// Set the background to black.
setBackground( Color.black );
// Centre the view window in centre of users screen.
setLocationRelativeTo(null);
var surface = new Surface();
getContentPane().add( surface );
// Show the view window.
setVisible( true );
}
}
@echo off
cd bin
java -jar Main.jar
pause
package;
import java.lang.System;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
class Surface extends JPanel
{
public function new()
{
// turn on doublebuffering
super( true );
}
// draw in an overriden painting method
@:overload public function paintComponent( g: Graphics )
{
// magic used to stop flickering when redrawing for animation later.
super.paintComponent( g );
// Strange but we need to cast so we can paint
var g2D: Graphics2D = cast g;
// make drawing antialias so smooth not pixelated edges
g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
g2D.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY );
// set drawing color
g2D.setColor(Color.red);
// draw a fill oval, x, y, wide, hi
g2D.fillOval( 300, 120, 150, 150 );
// release memory of graphics2d after finished drawing with it.
g2D.dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment