The shoutbox is currently out of service. Join us on Discord instead.
You can help CodeWalrus stay online by donating here.

Simple graphics in [java] [tutorial]

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

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

kotu

@p2 here is some good info regarding the keypress events you require

i have got some code up and running with this, will post it later

http://www.codeprogress.com/java/showSamples.php?index=40&key=JFrameHandleKeyEvent

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

c4ooo

Quote from: kotu on October 31, 2016, 08:04:35 pm
@p2 here is some good info regarding the keypress events you require

i have got some code up and running with this, will post it later

http://www.codeprogress.com/java/showSamples.php?index=40&key=JFrameHandleKeyEvent

:)

Thats what i do in my code >_>

kotu

here is the new TileTestApp.java unit. the other units stay the same. this example changes the background when you press a key (it goes up til 7 then stops). you will also need to check against specific values for specific keys. i'll look at that later

package tiletest;

import javax.swing.JFrame;
import tiletest.TileArea;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;

public class TileTestApp implements KeyListener
{
int tilex = 0;

public void keyPressed(KeyEvent e)
{
if (tilex < 7) {
tilex++;
}
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}

TileTestApp()
{
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);
    frame.addKeyListener(this);

// 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, tilex, 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();
}
}

}

public static void main(String[] args)
{
new TileTestApp();
}
}
SUBSCRIBE TO THE FUTURERAVE.UK MAILING LIST
http://futurerave.uk


c4ooo

Double post >_>
Also rip you derailed my thread _._

p2

right, maybe we should move these posts to a topic like "simple key stuff in java" ^^
anyways thanks to both of you!! :)  :thumbsup:
I guess I really couldnt finish my game without all your help ^.^  :love:
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

Quote from: p2 on October 31, 2016, 10:32:35 pm
right, maybe we should move these posts to a topic like "simple key stuff in java" ^^


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

Powered by EzPortal