generated from sigonasr2/JavaProjectTemplate
Resizing window canvas
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
parent
ef4a529157
commit
321f682030
@ -86,8 +86,6 @@ public class JavaProjectTemplate {
|
|||||||
game.Draw(100,20,Color.BLACK); //That's not a dead pixel, it's a black one!
|
game.Draw(100,20,Color.BLACK); //That's not a dead pixel, it's a black one!
|
||||||
game.Draw_Line(10,10,35,35,Color.BLACK); //Line Drawing
|
game.Draw_Line(10,10,35,35,Color.BLACK); //Line Drawing
|
||||||
|
|
||||||
game.FillCircle(WINDOW_WIDTH/2, WINDOW_HEIGHT/2, 100, Color.BLACK);
|
|
||||||
|
|
||||||
game.Draw_Sprite(450,75,bookSpr,Color.GREEN); //Sprite drawing (with green tint)
|
game.Draw_Sprite(450,75,bookSpr,Color.GREEN); //Sprite drawing (with green tint)
|
||||||
game.Draw_Sprite(450,75+bookSpr.getHeight(),bookSpr,Transform.VERTICAL); //Sprite drawing with vertical flip
|
game.Draw_Sprite(450,75+bookSpr.getHeight(),bookSpr,Transform.VERTICAL); //Sprite drawing with vertical flip
|
||||||
|
|
||||||
|
@ -11,6 +11,8 @@ import java.awt.event.MouseEvent;
|
|||||||
import java.awt.event.MouseMotionListener;
|
import java.awt.event.MouseMotionListener;
|
||||||
import java.awt.event.MouseWheelEvent;
|
import java.awt.event.MouseWheelEvent;
|
||||||
import java.awt.event.MouseWheelListener;
|
import java.awt.event.MouseWheelListener;
|
||||||
|
import java.awt.event.ComponentEvent;
|
||||||
|
import java.awt.event.ComponentAdapter;
|
||||||
|
|
||||||
import java.awt.GraphicsEnvironment;
|
import java.awt.GraphicsEnvironment;
|
||||||
import java.awt.GraphicsConfiguration;
|
import java.awt.GraphicsConfiguration;
|
||||||
@ -25,6 +27,7 @@ import java.awt.Graphics2D;
|
|||||||
import java.awt.RenderingHints;
|
import java.awt.RenderingHints;
|
||||||
|
|
||||||
import java.awt.Cursor;
|
import java.awt.Cursor;
|
||||||
|
import java.awt.Dimension;
|
||||||
|
|
||||||
import sig.JavaProjectTemplate;
|
import sig.JavaProjectTemplate;
|
||||||
|
|
||||||
@ -62,6 +65,8 @@ public class Panel extends JPanel implements Runnable,KeyListener {
|
|||||||
public static Panel p;
|
public static Panel p;
|
||||||
public static JFrame f;
|
public static JFrame f;
|
||||||
|
|
||||||
|
int ACTUAL_WINDOW_WIDTH,ACTUAL_WINDOW_HEIGHT;
|
||||||
|
|
||||||
static Cursor currentCursor = new Cursor(Cursor.DEFAULT_CURSOR);
|
static Cursor currentCursor = new Cursor(Cursor.DEFAULT_CURSOR);
|
||||||
|
|
||||||
public static void InitializeEngine(JavaProjectTemplate instance){
|
public static void InitializeEngine(JavaProjectTemplate instance){
|
||||||
@ -78,12 +83,21 @@ public class Panel extends JPanel implements Runnable,KeyListener {
|
|||||||
RENDERHINTS.put(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_SPEED);
|
RENDERHINTS.put(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_SPEED);
|
||||||
|
|
||||||
f = new JFrame(JavaProjectTemplate.PROGRAM_NAME);
|
f = new JFrame(JavaProjectTemplate.PROGRAM_NAME);
|
||||||
f.setResizable(false);
|
f.setResizable(true);
|
||||||
f.setSize(JavaProjectTemplate.WINDOW_WIDTH,JavaProjectTemplate.WINDOW_HEIGHT);
|
f.setSize(JavaProjectTemplate.WINDOW_WIDTH,JavaProjectTemplate.WINDOW_HEIGHT);
|
||||||
|
f.addComponentListener(new ComponentAdapter() {
|
||||||
|
public void componentResized(ComponentEvent e) {
|
||||||
|
Dimension d = p.getSize();
|
||||||
|
p.pixel=new int[(int)d.getWidth()*(int)d.getHeight()];
|
||||||
|
p.ACTUAL_WINDOW_WIDTH=(int)d.getWidth();
|
||||||
|
p.ACTUAL_WINDOW_HEIGHT=(int)d.getHeight();
|
||||||
|
p.init(false);
|
||||||
|
}
|
||||||
|
});
|
||||||
p = new Panel(f);
|
p = new Panel(f);
|
||||||
JavaProjectTemplate.game=p;
|
JavaProjectTemplate.game=p;
|
||||||
|
|
||||||
p.init();
|
p.init(true);
|
||||||
|
|
||||||
f.add(p);
|
f.add(p);
|
||||||
f.addKeyListener(p);
|
f.addKeyListener(p);
|
||||||
@ -206,27 +220,26 @@ public class Panel extends JPanel implements Runnable,KeyListener {
|
|||||||
/**
|
/**
|
||||||
* Call it after been visible and after resizes.
|
* Call it after been visible and after resizes.
|
||||||
*/
|
*/
|
||||||
public void init(){
|
public void init(boolean runThread){
|
||||||
cm = getCompatibleColorModel();
|
cm = getCompatibleColorModel();
|
||||||
int screenSize = JavaProjectTemplate.WINDOW_WIDTH * JavaProjectTemplate.WINDOW_HEIGHT;
|
int screenSize = ACTUAL_WINDOW_WIDTH * ACTUAL_WINDOW_HEIGHT;
|
||||||
if(pixel == null || pixel.length < screenSize){
|
if(pixel == null || pixel.length < screenSize){
|
||||||
pixel = new int[screenSize];
|
pixel = new int[screenSize];
|
||||||
}
|
}
|
||||||
mImageProducer = new MemoryImageSource(JavaProjectTemplate.WINDOW_WIDTH, JavaProjectTemplate.WINDOW_HEIGHT, cm, pixel,0, JavaProjectTemplate.WINDOW_WIDTH);
|
mImageProducer = new MemoryImageSource(ACTUAL_WINDOW_WIDTH, ACTUAL_WINDOW_HEIGHT, cm, pixel,0, ACTUAL_WINDOW_WIDTH);
|
||||||
mImageProducer.setAnimated(true);
|
mImageProducer.setAnimated(true);
|
||||||
mImageProducer.setFullBufferUpdates(true);
|
mImageProducer.setFullBufferUpdates(true);
|
||||||
imageBuffer = Toolkit.getDefaultToolkit().createImage(mImageProducer);
|
imageBuffer = Toolkit.getDefaultToolkit().createImage(mImageProducer);
|
||||||
if(thread.isInterrupted() || !thread.isAlive()){
|
if(runThread && thread.isInterrupted() || !thread.isAlive()){
|
||||||
thread.start();
|
thread.start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void paintComponent(Graphics g) {
|
public void paintComponent(Graphics g) {
|
||||||
//super.paintComponent(g);
|
|
||||||
// perform draws on pixels
|
// perform draws on pixels
|
||||||
long startTime = System.currentTimeMillis();
|
long startTime = System.currentTimeMillis();
|
||||||
g.drawImage(this.imageBuffer,0,0,JavaProjectTemplate.WINDOW_WIDTH,JavaProjectTemplate.WINDOW_HEIGHT,0,0,JavaProjectTemplate.WINDOW_WIDTH,JavaProjectTemplate.WINDOW_HEIGHT,this);
|
g.drawImage(this.imageBuffer,0,0,ACTUAL_WINDOW_WIDTH,ACTUAL_WINDOW_HEIGHT,0,0,ACTUAL_WINDOW_WIDTH,ACTUAL_WINDOW_HEIGHT,this);
|
||||||
scaleTime=System.currentTimeMillis()-startTime;
|
scaleTime=System.currentTimeMillis()-startTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -308,12 +321,12 @@ public class Panel extends JPanel implements Runnable,KeyListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void Draw(int x, int y, Color col) {
|
public void Draw(int x, int y, Color col) {
|
||||||
if (x<0||y<0||x>=JavaProjectTemplate.WINDOW_WIDTH||y>=JavaProjectTemplate.WINDOW_HEIGHT) return;
|
if (x<0||y<0||x>=ACTUAL_WINDOW_WIDTH||y>=ACTUAL_WINDOW_HEIGHT) return;
|
||||||
Draw(y*JavaProjectTemplate.WINDOW_WIDTH+x,col.getColor());
|
Draw(y*ACTUAL_WINDOW_WIDTH+x,col.getColor());
|
||||||
}
|
}
|
||||||
|
|
||||||
void Draw(int index, int col) {
|
void Draw(int index, int col) {
|
||||||
if (((col>>>24)&0xff)==0) return;
|
if (((col>>>24)&0xff)==0||index>=pixel.length) return;
|
||||||
if (((col>>>24)&0xff)!=255) {
|
if (((col>>>24)&0xff)!=255) {
|
||||||
pixel[index]=ColorLerpNoAlpha(new Color(pixel[index]),new Color(col),((col>>>24)&0xff)/255f).getColor();
|
pixel[index]=ColorLerpNoAlpha(new Color(pixel[index]),new Color(col),((col>>>24)&0xff)/255f).getColor();
|
||||||
} else {
|
} else {
|
||||||
@ -443,7 +456,7 @@ public class Panel extends JPanel implements Runnable,KeyListener {
|
|||||||
}
|
}
|
||||||
px=px+2*(dy1-dx1);
|
px=px+2*(dy1-dx1);
|
||||||
}
|
}
|
||||||
Draw(y*JavaProjectTemplate.WINDOW_WIDTH+x,col.getColor());
|
Draw(y*ACTUAL_WINDOW_WIDTH+x,col.getColor());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (dy>=0) {
|
if (dy>=0) {
|
||||||
@ -451,7 +464,7 @@ public class Panel extends JPanel implements Runnable,KeyListener {
|
|||||||
} else {
|
} else {
|
||||||
x=x2-1;y=y2-1;ye=y1;
|
x=x2-1;y=y2-1;ye=y1;
|
||||||
}
|
}
|
||||||
Draw(y*JavaProjectTemplate.WINDOW_WIDTH+x,col.getColor());
|
Draw(y*ACTUAL_WINDOW_WIDTH+x,col.getColor());
|
||||||
while (y<ye) {
|
while (y<ye) {
|
||||||
y=y+1;
|
y=y+1;
|
||||||
if (py<=0) {
|
if (py<=0) {
|
||||||
@ -464,7 +477,7 @@ public class Panel extends JPanel implements Runnable,KeyListener {
|
|||||||
}
|
}
|
||||||
py=py+2*(dx1-dy1);
|
py=py+2*(dx1-dy1);
|
||||||
}
|
}
|
||||||
Draw(y*JavaProjectTemplate.WINDOW_WIDTH+x,col.getColor());
|
Draw(y*ACTUAL_WINDOW_WIDTH+x,col.getColor());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -472,8 +485,8 @@ public class Panel extends JPanel implements Runnable,KeyListener {
|
|||||||
public void Fill_Rect(double x,double y,double w,double h,Color col) {
|
public void Fill_Rect(double x,double y,double w,double h,Color col) {
|
||||||
for (int xx=0;xx<w;xx++) {
|
for (int xx=0;xx<w;xx++) {
|
||||||
for (int yy=0;yy<h;yy++) {
|
for (int yy=0;yy<h;yy++) {
|
||||||
if (x+xx>=0&&y+yy>=0&&x+xx<JavaProjectTemplate.WINDOW_WIDTH&&y+yy<JavaProjectTemplate.WINDOW_HEIGHT) {
|
if (x+xx>=0&&y+yy>=0&&x+xx<ACTUAL_WINDOW_WIDTH&&y+yy<ACTUAL_WINDOW_HEIGHT) {
|
||||||
int index = ((int)y+yy)*JavaProjectTemplate.WINDOW_WIDTH+(int)x+xx;
|
int index = ((int)y+yy)*ACTUAL_WINDOW_WIDTH+(int)x+xx;
|
||||||
Draw(index,col.getColor());
|
Draw(index,col.getColor());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -518,14 +531,14 @@ public class Panel extends JPanel implements Runnable,KeyListener {
|
|||||||
boolean vertical = transform==Transform.VERTICAL||transform==Transform.HORIZ_VERTIC;
|
boolean vertical = transform==Transform.VERTICAL||transform==Transform.HORIZ_VERTIC;
|
||||||
for(int X=(int)xOffset;X<(int)(w+xOffset);X++){
|
for(int X=(int)xOffset;X<(int)(w+xOffset);X++){
|
||||||
for(int Y=(int)yOffset;Y<(int)(h+yOffset);Y++){
|
for(int Y=(int)yOffset;Y<(int)(h+yOffset);Y++){
|
||||||
if (X+x-xOffset<0||Y+y-yOffset<0||X-xOffset+x>=JavaProjectTemplate.WINDOW_WIDTH||Y-yOffset+y>=JavaProjectTemplate.WINDOW_HEIGHT) {
|
if (X+x-xOffset<0||Y+y-yOffset<0||X-xOffset+x>=ACTUAL_WINDOW_WIDTH||Y-yOffset+y>=ACTUAL_WINDOW_HEIGHT) {
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
int index =
|
int index =
|
||||||
((vertical?
|
((vertical?
|
||||||
sprite.getHeight()-(Y-(int)yOffset):
|
sprite.getHeight()-(Y-(int)yOffset):
|
||||||
(Y-(int)yOffset))
|
(Y-(int)yOffset))
|
||||||
+(int)y)*JavaProjectTemplate.WINDOW_WIDTH+
|
+(int)y)*ACTUAL_WINDOW_WIDTH+
|
||||||
(horizontal?
|
(horizontal?
|
||||||
sprite.getWidth()-(X-(int)xOffset):
|
sprite.getWidth()-(X-(int)xOffset):
|
||||||
(X-(int)xOffset))
|
(X-(int)xOffset))
|
||||||
@ -838,7 +851,7 @@ public class Panel extends JPanel implements Runnable,KeyListener {
|
|||||||
|
|
||||||
//The mask indicates which pie slices to draw, each bit of the mask represents 1/8th of a pie slice. By default all pie slices are drawn.
|
//The mask indicates which pie slices to draw, each bit of the mask represents 1/8th of a pie slice. By default all pie slices are drawn.
|
||||||
public void Draw_Circle(int x, int y, int radius, Color col, byte mask) {
|
public void Draw_Circle(int x, int y, int radius, Color col, byte mask) {
|
||||||
if (radius < 0 || x < -radius || y < -radius || x - JavaProjectTemplate.WINDOW_WIDTH > radius || y - JavaProjectTemplate.WINDOW_HEIGHT > radius)
|
if (radius < 0 || x < -radius || y < -radius || x - ACTUAL_WINDOW_WIDTH > radius || y - ACTUAL_WINDOW_HEIGHT > radius)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (radius > 0)
|
if (radius > 0)
|
||||||
@ -873,7 +886,7 @@ public class Panel extends JPanel implements Runnable,KeyListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void Fill_Circle(int x, int y, int radius, Color col) {
|
public void Fill_Circle(int x, int y, int radius, Color col) {
|
||||||
if (radius < 0 || x < -radius || y < -radius || x - JavaProjectTemplate.WINDOW_WIDTH > radius || y - JavaProjectTemplate.WINDOW_HEIGHT > radius)
|
if (radius < 0 || x < -radius || y < -radius || x - ACTUAL_WINDOW_WIDTH > radius || y - ACTUAL_WINDOW_HEIGHT > radius)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (radius > 0)
|
if (radius > 0)
|
||||||
@ -957,9 +970,9 @@ public class Panel extends JPanel implements Runnable,KeyListener {
|
|||||||
|
|
||||||
|
|
||||||
public void Clear(Color col){
|
public void Clear(Color col){
|
||||||
for (int y=0;y<JavaProjectTemplate.WINDOW_HEIGHT;y++) {
|
for (int y=0;y<ACTUAL_WINDOW_HEIGHT;y++) {
|
||||||
for (int x=0;x<JavaProjectTemplate.WINDOW_WIDTH;x++) {
|
for (int x=0;x<ACTUAL_WINDOW_WIDTH;x++) {
|
||||||
Draw(y*JavaProjectTemplate.WINDOW_WIDTH+x,col.getColor());
|
Draw(y*ACTUAL_WINDOW_WIDTH+x,col.getColor());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user