Alternatively, join us on Discord.

Simple graphics in [java] [tutorial]

Started by c4ooo, October 17, 2016, 11:44:25 pm

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

c4ooo


kotu

Guys, some interesting news, I have confirmed java's built in functions are capable for p2's usage... i made a tile renderer which draws a 600x408 area at 90fps, drawing all tiles on 2 layers.

he he heh
flump
SUBSCRIBE TO THE FUTURERAVE.UK MAILING LIST
http://futurerave.uk

DJ Omnimaga

Screenshots or it didn't happen. :P

c4ooo

Quote from: kotu on October 20, 2016, 02:52:31 pm
Guys, some interesting news, I have confirmed java's built in functions are capable for p2's usage... i made a tile renderer which draws a 600x408 area at 90fps, drawing all tiles on 2 layers.

he he heh
flump

Of course they are, the java API was written by smart people, and you can use higher resolution then 600*400. Post code and ile optimize it.

p2

October 20, 2016, 08:11:20 pm #19 Last Edit: October 20, 2016, 08:14:00 pm by p2
Quote from: DJ Omnimaga on October 20, 2016, 08:05:59 pm
Screenshots or it didn't happen. :P

He really wrote it and it's indeed a great help  :thumbsup:
Anyway war sucks. Just bring us your food instead of missiles  :P ~ DJ Omnimaga (11.10.2016 20:21:48)
if you cant get a jframe set up, draw stuff to it, and receive input, i can only imagine how horrible your game code is _._   ~ c4ooo (14.11.2016 22:44:07)
If they pull a Harambe on me tell my family I love them ~ u/Pwntear37d (AssangeWatch /r/)
make Walrii great again ~ DJ Omnimaga (28.11.2016 23:01:31)
God invented the pc, satan the smartphone I guess ~ p4nix (16.02.2017 22:51:49)

kotu



this is what i done

TileTestApp.java

package tiletest;

import javax.swing.JFrame;
import javax.swing.JPanel;
import tiletest.TileArea;

public class TileTestApp
{
public static void main(String[] args)
{
JFrame frame = new JFrame("TileTest");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// add TileArea to the JFrame here
TileArea tileArea = new TileArea(600, 408);
frame.add("Center", tileArea);
frame.pack();
    frame.setVisible(true);

// load tileset
TileSource tiles = new TileSource("all-3.png", 24);

//----
int r = 1;
for (;;) {
for (int x = 0; x < 25; x++) {
for (int y = 0; y < 17; y++) {
tileArea.drawTile(tiles, 0, 3, tiles.getTileSize()*x, tiles.getTileSize()*y);
tileArea.drawTile(tiles, r, 1, tiles.getTileSize()*x, tiles.getTileSize()*y);
}
}
frame.repaint();
r++;
if (r==3) r = 1;

try { // wait 10ms to avoid any flicker
    Thread.sleep(10);
} catch(InterruptedException ex) {
    Thread.currentThread().interrupt();
}
}
}
}


TileArea.java

package tiletest;

import java.awt.*;
import java.awt.image.*;

public class TileArea extends Component
{
private BufferedImage m_image;
private int m_width, m_height;

public TileArea(int width, int height)
{
m_width = width;
m_height = height;
m_image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
}

public Dimension getPreferredSize()
{
        return new Dimension(m_width, m_height);
    }

public void paint(Graphics g)
{
g.drawImage(m_image, 0, 0, null);
}

public void drawTile(TileSource tileSource, int tilex, int tiley, int x, int y)
{
Graphics2D g = m_image.createGraphics();
    g.drawImage(tileSource.getTile(tilex, tiley), x, y, null);
}
}


TileSource.java

package tiletest;

import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.lang.*;
import java.io.*;
import javax.swing.*;

public class TileSource extends Component
{
private BufferedImage m_tiles;
private int m_tileSize;

public TileSource(String filename, int tileSize)
{
m_tileSize = tileSize;
try {
m_tiles = ImageIO.read(new File(filename));
}
catch (IOException e) { // no need to do anything here
} // m_tiles will be null anyway

if (m_tiles == null) {
JOptionPane.showMessageDialog(null, "p2, no! could not load tileset.");
return;
}
}

public int getTileSize()
{
return m_tileSize;
}

public BufferedImage getTile(int tilex, int tiley)
{
if (m_tiles == null) {
return null;
}
return m_tiles.getSubimage(tilex*(m_tileSize+1)+1, tiley*(m_tileSize+1)+1, m_tileSize, m_tileSize);
}
}


@p2 you should change your TileTestApp.java to the version above.

btw c4ooo it doesn't need optimizing and we dont want it optimized at the minute, lol.
SUBSCRIBE TO THE FUTURERAVE.UK MAILING LIST
http://futurerave.uk

p2

is it the version from your second pm? if yes, its already implemented :3
Anyway war sucks. Just bring us your food instead of missiles  :P ~ DJ Omnimaga (11.10.2016 20:21:48)
if you cant get a jframe set up, draw stuff to it, and receive input, i can only imagine how horrible your game code is _._   ~ c4ooo (14.11.2016 22:44:07)
If they pull a Harambe on me tell my family I love them ~ u/Pwntear37d (AssangeWatch /r/)
make Walrii great again ~ DJ Omnimaga (28.11.2016 23:01:31)
God invented the pc, satan the smartphone I guess ~ p4nix (16.02.2017 22:51:49)

kotu

a few of the lines of code are different
SUBSCRIBE TO THE FUTURERAVE.UK MAILING LIST
http://futurerave.uk

c4ooo

Quoteimport java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.lang.*;
import java.io.*;
import javax.swing.*;

Never import entire APIs O.O

kotu

SUBSCRIBE TO THE FUTURERAVE.UK MAILING LIST
http://futurerave.uk

Scipi

Quote from: c4ooo on October 21, 2016, 10:48:56 pm
Quoteimport java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.lang.*;
import java.io.*;
import javax.swing.*;

Never import entire APIs O.O


That really shouldn't be a big issue. I'd expect the Java linker to be smart enough to only take the classes it needs from the API. It might only slow down compiling to bytecode, at worst.




Snektron

It is considered bad practise though. If you're learning java better learn it well while youre at it ;)
Legends say if you spam more than DJ Omnimaga, you will become a walrus...


kotu

Quote from: Snektron on October 22, 2016, 07:40:16 am
It is considered bad practise though.


From the point of view of reducing the chance of naming/resolution conflicts?
SUBSCRIBE TO THE FUTURERAVE.UK MAILING LIST
http://futurerave.uk

c4ooo


p2

all includes that I use in my code so far:  :ninja:
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Canvas;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import java.io.IOException;
import java.io.File;

Anyway war sucks. Just bring us your food instead of missiles  :P ~ DJ Omnimaga (11.10.2016 20:21:48)
if you cant get a jframe set up, draw stuff to it, and receive input, i can only imagine how horrible your game code is _._   ~ c4ooo (14.11.2016 22:44:07)
If they pull a Harambe on me tell my family I love them ~ u/Pwntear37d (AssangeWatch /r/)
make Walrii great again ~ DJ Omnimaga (28.11.2016 23:01:31)
God invented the pc, satan the smartphone I guess ~ p4nix (16.02.2017 22:51:49)

Powered by EzPortal