CodeWalrus

Development => PC, Mac & Vintage Computers => Topic started by: c4ooo on October 17, 2016, 11:44:25 PM

Title: Simple graphics in [java] [tutorial]
Post by: c4ooo on October 17, 2016, 11:44:25 PM
So, recently some p2 has gotten into java. I have put together this code to help him figure out how to do stuff with the Java 2D api. This excample will display a magenta square that will follow your cursor.
[spoiler]

/**
*
* Copyright @German Kuznetsov
*/

package your.package;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;

/**
*
* some code ripped from
* http://www.gamedev.net/page/resources/_/technical/general-programming/java-games-active-rendering-r2418
*/
public class Main {

    private static JFrame frame; //Lets define some stuff we will be using
    private static Canvas canvas;
    private static BufferStrategy buffer;

    public static final int WIDTH = 600; //Our window size
    public static final int HEIGHT = 400;

    public static int MouseX, MouseY; //These variables will hold values for the mouse.
    public static boolean LeftMouseButtonDown, MouseWheelDown, RightMouseButtonDown;

    public static final boolean[] keyDown = new boolean[65536]; //This list will hold the position of keys (up/down)

    public static void main(String[] args) {
        //create our window
        frame = new JFrame();
        frame.setIgnoreRepaint(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //create our canvas to draw on
        canvas = new Canvas();
        canvas.setIgnoreRepaint(true);
        canvas.setSize(WIDTH, HEIGHT);

        //pack our canvas into our frame
        frame.add(canvas);
        frame.pack();
        frame.setVisible(true);

        //add a mouselistener to our window (mouse button pressed / released)
        canvas.addMouseListener(new MouseListener() {

            public void mouseClicked(MouseEvent e) {
            }

            public void mousePressed(MouseEvent e) {
                switch (e.getButton()) {
                    case 1:
                        LeftMouseButtonDown = true;
                        break;
                    case 2:
                        MouseWheelDown = true;
                        break;
                    case 3:
                        RightMouseButtonDown = true;
                        break;
                }
            }

            public void mouseReleased(MouseEvent e) {
                switch (e.getButton()) {
                    case 1:
                        LeftMouseButtonDown = true;
                        break;
                    case 2:
                        MouseWheelDown = true;
                        break;
                    case 3:
                        RightMouseButtonDown = true;
                        break;
                }
            }

            public void mouseEntered(MouseEvent e) {
            }

            public void mouseExited(MouseEvent e) {
            }

        });
       // //add a mouse motion listener to our window (Mouse moved)
        canvas.addMouseMotionListener(new MouseMotionListener() {

            @Override
            public void mouseDragged(MouseEvent e) {
                MouseX = e.getX();
                MouseY = e.getY();
            }

            @Override
            public void mouseMoved(MouseEvent e) {
                MouseX = e.getX();
                MouseY = e.getY();
            }
        });

       //add Key Listener (Key pressed/ released)
        canvas.addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent ke) {
            }

            @Override
            public void keyPressed(KeyEvent e) {
                keyDown[e.getKeyCode()] = true;
            }

            @Override
            public void keyReleased(KeyEvent e) {
                keyDown[e.getKeyCode()] = false;
            }
        });
//buffer setup
        canvas.createBufferStrategy(2);
        buffer = canvas.getBufferStrategy();

      //our game loop
        Graphics graphics = null;
        while (true) {
            try {
                update();
                render(graphics);
                if (!buffer.contentsLost()) {
                    buffer.show();
                }
            } finally {
                if (graphics != null) {
                    graphics.dispose();
                }

            }

        }
    }

   //RENDER YOUR STUFF HERE
    public static void render(Graphics graphics) {
        graphics = buffer.getDrawGraphics(); //Clears back buffer

        graphics.setColor(Color.BLACK);
        graphics.fillRect(0, 0, WIDTH, HEIGHT);
        graphics.setColor(new Color(255, 0, 255));
        graphics.fillRect(MouseX, MouseY, 5, 5);
    }

    public static void update() {

    }
}
Title: Re: Simple graphics in [java] [tutorial]
Post by: kotu on October 18, 2016, 03:41:52 AM
Nice but not meaning to confuse young p2 - the key listener stuff..... and that big array of booleans
public static final boolean[] keyDown = new boolean[65536];

aren't really needed for the example. for simplicity's sake, you could take them out.

why so many bools in that array by the way??
Title: Re: Simple graphics in [java] [tutorial]
Post by: p2 on October 18, 2016, 07:56:54 AM
Heey, just because I suck at Java it doesn't mean I'm a little kid  ;D

random internet quote: I swear it, it she ever says I'm too childish, she'll never get her nose back!


I guess every key got a number and it's a bool array to check for each key separately?
But that still qouldnt explain why there are that many bools in the array xD
Title: Re: Simple graphics in [java] [tutorial]
Post by: kotu on October 18, 2016, 08:25:42 AM
Quote from: p2 on October 18, 2016, 07:56:54 AM
Heey, just because I suck at Java it doesn't mean I'm a little kid  ;D

Well not many people see the value in having as bare-bones code as possible, ALWAYS, as a starting point. I swear by that (in most instances).

If you're not going to use that code (definitely not going to use) take it out. Or you might leave it in in this case.  ;)
Title: Re: Simple graphics in [java] [tutorial]
Post by: kotu on October 18, 2016, 08:29:40 AM
Just found this list which looks more realistic.

Spoiler: ShowHide
0 - Unknown keyCode: 0x0

1 - Unknown keyCode: 0x1

2 - Unknown keyCode: 0x2

3 - Cancel

4 - Unknown keyCode: 0x4

5 - Unknown keyCode: 0x5

6 - Unknown keyCode: 0x6

7 - Unknown keyCode: 0x7

8 - Backspace

9 - Tab

10 - Enter

11 - Unknown keyCode: 0xb

12 - Clear

13 - Unknown keyCode: 0xd

14 - Unknown keyCode: 0xe

15 - Unknown keyCode: 0xf

16 - Shift

17 - Ctrl

18 - Alt

19 - Pause

20 - Caps Lock

21 - Kana

22 - Unknown keyCode: 0x16

23 - Unknown keyCode: 0x17

24 - Final

25 - Kanji

26 - Unknown keyCode: 0x1a

27 - Escape

28 - Convert

29 - No Convert

30 - Accept

31 - Mode Change

32 - Space

33 - Page Up

34 - Page Down

35 - End

36 - Home

37 - Left

38 - Up

39 - Right

40 - Down

41 - Unknown keyCode: 0x29

42 - Unknown keyCode: 0x2a

43 - Unknown keyCode: 0x2b

44 - Comma

45 - Minus

46 - Period

47 - Slash

48 - 0

49 - 1

50 - 2

51 - 3

52 - 4

53 - 5

54 - 6

55 - 7

56 - 8

57 - 9

58 - Unknown keyCode: 0x3a

59 - Semicolon

60 - Unknown keyCode: 0x3c

61 - Equals

62 - Unknown keyCode: 0x3e

63 - Unknown keyCode: 0x3f

64 - Unknown keyCode: 0x40

65 - A

66 - B

67 - C

68 - D

69 - E

70 - F

71 - G

72 - H

73 - I

74 - J

75 - K

76 - L

77 - M

78 - N

79 - O

80 - P

81 - Q

82 - R

83 - S

84 - T

85 - U

86 - V

87 - W

88 - X

89 - Y

90 - Z

91 - Open Bracket

92 - Back Slash

93 - Close Bracket

94 - Unknown keyCode: 0x5e

95 - Unknown keyCode: 0x5f

96 - NumPad-0

97 - NumPad-1

98 - NumPad-2

99 - NumPad-3

100 - NumPad-4

101 - NumPad-5

102 - NumPad-6

103 - NumPad-7

104 - NumPad-8

105 - NumPad-9

106 - NumPad *

107 - NumPad +

108 - NumPad ,

109 - NumPad -

110 - NumPad .

111 - NumPad /

112 - F1

113 - F2

114 - F3

115 - F4

116 - F5

117 - F6

118 - F7

119 - F8

120 - F9

121 - F10

122 - F11

123 - F12

124 - Unknown keyCode: 0x7c

125 - Unknown keyCode: 0x7d

126 - Unknown keyCode: 0x7e

127 - Delete

128 - Dead Grave

129 - Dead Acute

130 - Dead Circumflex

131 - Dead Tilde

132 - Dead Macron

133 - Dead Breve

134 - Dead Above Dot

135 - Dead Diaeresis

136 - Dead Above Ring

137 - Dead Double Acute

138 - Dead Caron

139 - Dead Cedilla

140 - Dead Ogonek

141 - Dead Iota

142 - Dead Voiced Sound

143 - Dead Semivoiced Sound

144 - Num Lock

145 - Scroll Lock

146 - Unknown keyCode: 0x92

147 - Unknown keyCode: 0x93

148 - Unknown keyCode: 0x94

149 - Unknown keyCode: 0x95

150 - Ampersand

151 - Asterisk

152 - Double Quote

153 - Less

154 - Print Screen

155 - Insert

156 - Help

157 - Meta

158 - Unknown keyCode: 0x9e

159 - Unknown keyCode: 0x9f

160 - Greater

161 - Left Brace

162 - Right Brace

163 - Unknown keyCode: 0xa3

164 - Unknown keyCode: 0xa4

165 - Unknown keyCode: 0xa5

166 - Unknown keyCode: 0xa6

167 - Unknown keyCode: 0xa7

168 - Unknown keyCode: 0xa8

169 - Unknown keyCode: 0xa9

170 - Unknown keyCode: 0xaa

171 - Unknown keyCode: 0xab

172 - Unknown keyCode: 0xac

173 - Unknown keyCode: 0xad

174 - Unknown keyCode: 0xae

175 - Unknown keyCode: 0xaf

176 - Unknown keyCode: 0xb0

177 - Unknown keyCode: 0xb1

178 - Unknown keyCode: 0xb2

179 - Unknown keyCode: 0xb3

180 - Unknown keyCode: 0xb4

181 - Unknown keyCode: 0xb5

182 - Unknown keyCode: 0xb6

183 - Unknown keyCode: 0xb7

184 - Unknown keyCode: 0xb8

185 - Unknown keyCode: 0xb9

186 - Unknown keyCode: 0xba

187 - Unknown keyCode: 0xbb

188 - Unknown keyCode: 0xbc

189 - Unknown keyCode: 0xbd

190 - Unknown keyCode: 0xbe

191 - Unknown keyCode: 0xbf

192 - Back Quote

193 - Unknown keyCode: 0xc1

194 - Unknown keyCode: 0xc2

195 - Unknown keyCode: 0xc3

196 - Unknown keyCode: 0xc4

197 - Unknown keyCode: 0xc5

198 - Unknown keyCode: 0xc6

199 - Unknown keyCode: 0xc7

200 - Unknown keyCode: 0xc8

201 - Unknown keyCode: 0xc9

202 - Unknown keyCode: 0xca

203 - Unknown keyCode: 0xcb

204 - Unknown keyCode: 0xcc

205 - Unknown keyCode: 0xcd

206 - Unknown keyCode: 0xce

207 - Unknown keyCode: 0xcf

208 - Unknown keyCode: 0xd0

209 - Unknown keyCode: 0xd1

210 - Unknown keyCode: 0xd2

211 - Unknown keyCode: 0xd3

212 - Unknown keyCode: 0xd4

213 - Unknown keyCode: 0xd5

214 - Unknown keyCode: 0xd6

215 - Unknown keyCode: 0xd7

216 - Unknown keyCode: 0xd8

217 - Unknown keyCode: 0xd9

218 - Unknown keyCode: 0xda

219 - Unknown keyCode: 0xdb

220 - Unknown keyCode: 0xdc

221 - Unknown keyCode: 0xdd

222 - Quote

223 - Unknown keyCode: 0xdf

224 - Up

225 - Down

226 - Left

227 - Right

228 - Unknown keyCode: 0xe4

229 - Unknown keyCode: 0xe5

230 - Unknown keyCode: 0xe6

231 - Unknown keyCode: 0xe7

232 - Unknown keyCode: 0xe8

233 - Unknown keyCode: 0xe9

234 - Unknown keyCode: 0xea

235 - Unknown keyCode: 0xeb

236 - Unknown keyCode: 0xec

237 - Unknown keyCode: 0xed

238 - Unknown keyCode: 0xee

239 - Unknown keyCode: 0xef

240 - Alphanumeric

241 - Katakana

242 - Hiragana

243 - Full-Width

244 - Half-Width

245 - Roman Characters

246 - Unknown keyCode: 0xf6

247 - Unknown keyCode: 0xf7

248 - Unknown keyCode: 0xf8

249 - Unknown keyCode: 0xf9

250 - Unknown keyCode: 0xfa

251 - Unknown keyCode: 0xfb

252 - Unknown keyCode: 0xfc

253 - Unknown keyCode: 0xfd

254 - Unknown keyCode: 0xfe

255 - Unknown keyCode: 0xff

256 - All Candidates

257 - Previous Candidate

258 - Code Input

259 - Japanese Katakana

260 - Japanese Hiragana

261 - Japanese Roman

262 - Kana Lock

263 - Input Method On/Off

264 - Unknown keyCode: 0x108

265 - Unknown keyCode: 0x109

266 - Unknown keyCode: 0x10a

267 - Unknown keyCode: 0x10b

268 - Unknown keyCode: 0x10c

269 - Unknown keyCode: 0x10d

270 - Unknown keyCode: 0x10e

271 - Unknown keyCode: 0x10f

272 - Unknown keyCode: 0x110

273 - Unknown keyCode: 0x111

274 - Unknown keyCode: 0x112

275 - Unknown keyCode: 0x113

276 - Unknown keyCode: 0x114

277 - Unknown keyCode: 0x115

278 - Unknown keyCode: 0x116

279 - Unknown keyCode: 0x117

280 - Unknown keyCode: 0x118

281 - Unknown keyCode: 0x119

282 - Unknown keyCode: 0x11a

283 - Unknown keyCode: 0x11b

284 - Unknown keyCode: 0x11c

285 - Unknown keyCode: 0x11d

286 - Unknown keyCode: 0x11e

287 - Unknown keyCode: 0x11f

288 - Unknown keyCode: 0x120

289 - Unknown keyCode: 0x121

290 - Unknown keyCode: 0x122

291 - Unknown keyCode: 0x123

292 - Unknown keyCode: 0x124

293 - Unknown keyCode: 0x125

294 - Unknown keyCode: 0x126

295 - Unknown keyCode: 0x127

296 - Unknown keyCode: 0x128

297 - Unknown keyCode: 0x129

298 - Unknown keyCode: 0x12a

299 - Unknown keyCode: 0x12b

300 - Unknown keyCode: 0x12c

301 - Unknown keyCode: 0x12d

302 - Unknown keyCode: 0x12e

303 - Unknown keyCode: 0x12f

304 - Unknown keyCode: 0x130

305 - Unknown keyCode: 0x131

306 - Unknown keyCode: 0x132

307 - Unknown keyCode: 0x133

308 - Unknown keyCode: 0x134

309 - Unknown keyCode: 0x135

310 - Unknown keyCode: 0x136

311 - Unknown keyCode: 0x137

312 - Unknown keyCode: 0x138

313 - Unknown keyCode: 0x139

314 - Unknown keyCode: 0x13a

315 - Unknown keyCode: 0x13b

316 - Unknown keyCode: 0x13c

317 - Unknown keyCode: 0x13d

318 - Unknown keyCode: 0x13e

319 - Unknown keyCode: 0x13f

320 - Unknown keyCode: 0x140

321 - Unknown keyCode: 0x141

322 - Unknown keyCode: 0x142

323 - Unknown keyCode: 0x143

324 - Unknown keyCode: 0x144

325 - Unknown keyCode: 0x145

326 - Unknown keyCode: 0x146

327 - Unknown keyCode: 0x147

328 - Unknown keyCode: 0x148

329 - Unknown keyCode: 0x149

330 - Unknown keyCode: 0x14a

331 - Unknown keyCode: 0x14b

332 - Unknown keyCode: 0x14c

333 - Unknown keyCode: 0x14d

334 - Unknown keyCode: 0x14e

335 - Unknown keyCode: 0x14f

336 - Unknown keyCode: 0x150

337 - Unknown keyCode: 0x151

338 - Unknown keyCode: 0x152

339 - Unknown keyCode: 0x153

340 - Unknown keyCode: 0x154

341 - Unknown keyCode: 0x155

342 - Unknown keyCode: 0x156

343 - Unknown keyCode: 0x157

344 - Unknown keyCode: 0x158

345 - Unknown keyCode: 0x159

346 - Unknown keyCode: 0x15a

347 - Unknown keyCode: 0x15b

348 - Unknown keyCode: 0x15c

349 - Unknown keyCode: 0x15d

350 - Unknown keyCode: 0x15e

351 - Unknown keyCode: 0x15f

352 - Unknown keyCode: 0x160

353 - Unknown keyCode: 0x161

354 - Unknown keyCode: 0x162

355 - Unknown keyCode: 0x163

356 - Unknown keyCode: 0x164

357 - Unknown keyCode: 0x165

358 - Unknown keyCode: 0x166

359 - Unknown keyCode: 0x167

360 - Unknown keyCode: 0x168

361 - Unknown keyCode: 0x169

362 - Unknown keyCode: 0x16a

363 - Unknown keyCode: 0x16b

364 - Unknown keyCode: 0x16c

365 - Unknown keyCode: 0x16d

366 - Unknown keyCode: 0x16e

367 - Unknown keyCode: 0x16f

368 - Unknown keyCode: 0x170

369 - Unknown keyCode: 0x171

370 - Unknown keyCode: 0x172

371 - Unknown keyCode: 0x173

372 - Unknown keyCode: 0x174

373 - Unknown keyCode: 0x175

374 - Unknown keyCode: 0x176

375 - Unknown keyCode: 0x177

376 - Unknown keyCode: 0x178

377 - Unknown keyCode: 0x179

378 - Unknown keyCode: 0x17a

379 - Unknown keyCode: 0x17b

380 - Unknown keyCode: 0x17c

381 - Unknown keyCode: 0x17d

382 - Unknown keyCode: 0x17e

383 - Unknown keyCode: 0x17f

384 - Unknown keyCode: 0x180

385 - Unknown keyCode: 0x181

386 - Unknown keyCode: 0x182

387 - Unknown keyCode: 0x183

388 - Unknown keyCode: 0x184

389 - Unknown keyCode: 0x185

390 - Unknown keyCode: 0x186

391 - Unknown keyCode: 0x187

392 - Unknown keyCode: 0x188

393 - Unknown keyCode: 0x189

394 - Unknown keyCode: 0x18a

395 - Unknown keyCode: 0x18b

396 - Unknown keyCode: 0x18c

397 - Unknown keyCode: 0x18d

398 - Unknown keyCode: 0x18e

399 - Unknown keyCode: 0x18f

400 - Unknown keyCode: 0x190

401 - Unknown keyCode: 0x191

402 - Unknown keyCode: 0x192

403 - Unknown keyCode: 0x193

404 - Unknown keyCode: 0x194

405 - Unknown keyCode: 0x195

406 - Unknown keyCode: 0x196

407 - Unknown keyCode: 0x197

408 - Unknown keyCode: 0x198

409 - Unknown keyCode: 0x199

410 - Unknown keyCode: 0x19a

411 - Unknown keyCode: 0x19b

412 - Unknown keyCode: 0x19c

413 - Unknown keyCode: 0x19d

414 - Unknown keyCode: 0x19e

415 - Unknown keyCode: 0x19f

416 - Unknown keyCode: 0x1a0

417 - Unknown keyCode: 0x1a1

418 - Unknown keyCode: 0x1a2

419 - Unknown keyCode: 0x1a3

420 - Unknown keyCode: 0x1a4

421 - Unknown keyCode: 0x1a5

422 - Unknown keyCode: 0x1a6

423 - Unknown keyCode: 0x1a7

424 - Unknown keyCode: 0x1a8

425 - Unknown keyCode: 0x1a9

426 - Unknown keyCode: 0x1aa

427 - Unknown keyCode: 0x1ab

428 - Unknown keyCode: 0x1ac

429 - Unknown keyCode: 0x1ad

430 - Unknown keyCode: 0x1ae

431 - Unknown keyCode: 0x1af

432 - Unknown keyCode: 0x1b0

433 - Unknown keyCode: 0x1b1

434 - Unknown keyCode: 0x1b2

435 - Unknown keyCode: 0x1b3

436 - Unknown keyCode: 0x1b4

437 - Unknown keyCode: 0x1b5

438 - Unknown keyCode: 0x1b6

439 - Unknown keyCode: 0x1b7

440 - Unknown keyCode: 0x1b8

441 - Unknown keyCode: 0x1b9

442 - Unknown keyCode: 0x1ba

443 - Unknown keyCode: 0x1bb

444 - Unknown keyCode: 0x1bc

445 - Unknown keyCode: 0x1bd

446 - Unknown keyCode: 0x1be

447 - Unknown keyCode: 0x1bf

448 - Unknown keyCode: 0x1c0

449 - Unknown keyCode: 0x1c1

450 - Unknown keyCode: 0x1c2

451 - Unknown keyCode: 0x1c3

452 - Unknown keyCode: 0x1c4

453 - Unknown keyCode: 0x1c5

454 - Unknown keyCode: 0x1c6

455 - Unknown keyCode: 0x1c7

456 - Unknown keyCode: 0x1c8

457 - Unknown keyCode: 0x1c9

458 - Unknown keyCode: 0x1ca

459 - Unknown keyCode: 0x1cb

460 - Unknown keyCode: 0x1cc

461 - Unknown keyCode: 0x1cd

462 - Unknown keyCode: 0x1ce

463 - Unknown keyCode: 0x1cf

464 - Unknown keyCode: 0x1d0

465 - Unknown keyCode: 0x1d1

466 - Unknown keyCode: 0x1d2

467 - Unknown keyCode: 0x1d3

468 - Unknown keyCode: 0x1d4

469 - Unknown keyCode: 0x1d5

470 - Unknown keyCode: 0x1d6

471 - Unknown keyCode: 0x1d7

472 - Unknown keyCode: 0x1d8

473 - Unknown keyCode: 0x1d9

474 - Unknown keyCode: 0x1da

475 - Unknown keyCode: 0x1db

476 - Unknown keyCode: 0x1dc

477 - Unknown keyCode: 0x1dd

478 - Unknown keyCode: 0x1de

479 - Unknown keyCode: 0x1df

480 - Unknown keyCode: 0x1e0

481 - Unknown keyCode: 0x1e1

482 - Unknown keyCode: 0x1e2

483 - Unknown keyCode: 0x1e3

484 - Unknown keyCode: 0x1e4

485 - Unknown keyCode: 0x1e5

486 - Unknown keyCode: 0x1e6

487 - Unknown keyCode: 0x1e7

488 - Unknown keyCode: 0x1e8

489 - Unknown keyCode: 0x1e9

490 - Unknown keyCode: 0x1ea

491 - Unknown keyCode: 0x1eb

492 - Unknown keyCode: 0x1ec

493 - Unknown keyCode: 0x1ed

494 - Unknown keyCode: 0x1ee

495 - Unknown keyCode: 0x1ef

496 - Unknown keyCode: 0x1f0

497 - Unknown keyCode: 0x1f1

498 - Unknown keyCode: 0x1f2

499 - Unknown keyCode: 0x1f3

500 - Unknown keyCode: 0x1f4

501 - Unknown keyCode: 0x1f5

502 - Unknown keyCode: 0x1f6

503 - Unknown keyCode: 0x1f7

504 - Unknown keyCode: 0x1f8

505 - Unknown keyCode: 0x1f9

506 - Unknown keyCode: 0x1fa

507 - Unknown keyCode: 0x1fb

508 - Unknown keyCode: 0x1fc

509 - Unknown keyCode: 0x1fd

510 - Unknown keyCode: 0x1fe

511 - Unknown keyCode: 0x1ff

512 - At

513 - Colon

514 - Circumflex

515 - Dollar

516 - Euro

517 - Exclamation Mark

518 - Inverted Exclamation Mark

519 - Left Parenthesis

520 - Number Sign

521 - Plus

522 - Right Parenthesis

523 - Underscore

524 - Windows

525 - Context Menu


(-_(//));
Title: Re: Simple graphics in
Post by: Snektron on October 18, 2016, 08:33:50 AM
I dont recommend using awt for anythkng serious. If you want to make a game in java you shoulf use either lwjgl (or jogl) directly or libGDX if you want helper functions. AWT is really slow.

Also: please post big chunks if code via a pastebin link, or put them in a
Spoiler: ShowHide


code


tag.
Title: Re: Simple graphics in [java] [tutorial]
Post by: p2 on October 18, 2016, 08:35:11 AM
yesss, a CODE tag please... O.o My mouse wheel is burning...  :ninja:
But anyways that list is interesting ^^
Title: Re: Simple graphics in
Post by: kotu on October 18, 2016, 08:37:06 AM
Quote from: Snektron on October 18, 2016, 08:33:50 AM
I dont recommend using awt for anythkng serious. If you want to make a game in java you shoulf use either lwjgl (or jogl) directly or libGDX if you want helper functions. AWT is really slow.
But for a 2D tilemap RPG game?
Title: Re: Simple graphics in [java] [tutorial]
Post by: Snektron on October 18, 2016, 08:38:56 AM
Why not? libGDX not only supports 2D but also had an implementation for Tiled (http://www.mapeditor.org)
Title: Re: Simple graphics in [java] [tutorial]
Post by: kotu on October 18, 2016, 08:41:16 AM
Hey. Kill C4000 instead

(Sorry, thought you were attacking me  ;) ) lol
Title: Re: Simple graphics in [java] [tutorial]
Post by: c4ooo on October 18, 2016, 11:53:55 AM
Quote from: kotu on October 18, 2016, 03:41:52 AM
Nice but not meaning to confuse young p2 - the key listener stuff..... and that big array of booleans
public static final boolean[] keyDown = new boolean[65536];

aren't really needed for the example. for simplicity's sake, you could take them out.

why so many bools in that array by the way??
Each value represents a key. You dont need every single key so its a bit overkill :P
The key constants are defined here: https://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyEvent.html
So, in order to check if, for example, the "left arrow" key is down, do keyDown[KeyEvent.VK_LEFT];

Quote from: p2 on October 18, 2016, 08:35:11 AM
yesss, a CODE tag please... O.o My mouse wheel is burning...  :ninja:
But anyways that list is interesting ^^
Either way you scroll either the page or the code box to read the code, so i guess a spoiler will do :P
Title: Re: Simple graphics in
Post by: Scipi on October 18, 2016, 05:10:05 PM
Quote from: Snektron on October 18, 2016, 08:33:50 AM
I dont recommend using awt for anythkng serious. If you want to make a game in java you shoulf use either lwjgl (or jogl) directly or libGDX if you want helper functions. AWT is really slow.

I second this. Learning awt and such isn't useful if you're going to eventually just use an external library anyways. Also, taking a look at libGDX it gives you a lot of neat features. Tiled support is also a major plus.
Title: Re: Simple graphics in [java] [tutorial]
Post by: kotu on October 18, 2016, 06:35:51 PM
Quote from: c4ooo on October 18, 2016, 11:53:55 AM
Quote from: kotu on October 18, 2016, 03:41:52 AM
Nice but not meaning to confuse young p2 - the key listener stuff..... and that big array of booleans
public static final boolean[] keyDown = new boolean[65536];

aren't really needed for the example. for simplicity's sake, you could take them out.

why so many bools in that array by the way??
Each value represents a key. You dont need every single key so its a bit overkill :P
The key constants are defined here: https://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyEvent.html
So, in order to check if, for example, the "left arrow" key is down, do keyDown[KeyEvent.VK_LEFT];

cool so it's like, virtual key codes. nice
Title: Re: Simple graphics in [java] [tutorial]
Post by: kotu on October 19, 2016, 02:17:49 AM
Quote from: kotu on October 18, 2016, 08:41:16 AM
Hey. Kill C4000 instead

LOOK

*kotu stabs C4000*
Title: Re: Simple graphics in [java] [tutorial]
Post by: p2 on October 19, 2016, 09:13:00 AM
heey, no wars here pls @kotu @c4ooo just eat another cookie and relax, ok? :)
Title: Re: Simple graphics in [java] [tutorial]
Post by: c4ooo on October 19, 2016, 07:18:58 PM
Yes, bring food not war :P
Title: Re: Simple graphics in [java] [tutorial]
Post by: 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
Title: Re: Simple graphics in
Post by: DJ Omnimaga on October 20, 2016, 08:05:59 PM
Screenshots or it didn't happen. :P
Title: Re: Simple graphics in [java] [tutorial]
Post by: c4ooo on October 20, 2016, 08:10:27 PM
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.
Title: Re: Simple graphics in [java] [tutorial]
Post by: p2 on October 20, 2016, 08:11:20 PM
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:
(https://codewalr.us/proxy.php?request=http%3A%2F%2Fimg.ourl.ca%2Fkotu.png&hash=57fe4cfaa185cec5a91a0f5f3c90a1ce1f029ff6)
Title: Re: Simple graphics in [java] [tutorial]
Post by: kotu on October 20, 2016, 11:41:35 PM
(https://s15.postimg.org/mlzadnlzf/heartless.png)

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.
Title: Re: Simple graphics in [java] [tutorial]
Post by: p2 on October 21, 2016, 07:48:18 AM
is it the version from your second pm? if yes, its already implemented :3
Title: Re: Simple graphics in [java] [tutorial]
Post by: kotu on October 21, 2016, 03:14:55 PM
a few of the lines of code are different
Title: Re: Simple graphics in [java] [tutorial]
Post by: 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
Title: Re: Simple graphics in [java] [tutorial]
Post by: kotu on October 21, 2016, 10:54:22 PM
sorry i hardly done any before

(java)
Title: Re: Simple graphics in
Post by: Scipi on October 22, 2016, 12:27:17 AM
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.
Title: Re: Simple graphics in [java] [tutorial]
Post by: Snektron on October 22, 2016, 07:40:16 AM
It is considered bad practise though. If you're learning java better learn it well while youre at it ;)
Title: Re: Simple graphics in [java] [tutorial]
Post by: kotu on October 22, 2016, 12:02:02 PM
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?
Title: Re: Simple graphics in [java] [tutorial]
Post by: c4ooo on October 22, 2016, 03:45:51 PM
I would assume yes.
Title: Re: Simple graphics in [java] [tutorial]
Post by: p2 on October 23, 2016, 05:47:21 PM
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;

Title: Re: Simple graphics in [java] [tutorial]
Post by: 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 (http://www.codeprogress.com/java/showSamples.php?index=40&key=JFrameHandleKeyEvent)

:)
Title: Re: Simple graphics in [java] [tutorial]
Post by: c4ooo on October 31, 2016, 08:09:03 PM
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 (http://www.codeprogress.com/java/showSamples.php?index=40&key=JFrameHandleKeyEvent)

:)
Thats what i do in my code >_>
Title: Re: Simple graphics in [java] [tutorial]
Post by: kotu on October 31, 2016, 08:16:22 PM
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();
}
}
Title: Re: Simple graphics in [java] [tutorial]
Post by: kotu on October 31, 2016, 08:36:32 PM
you want to look at this

https://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyEvent.html (https://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyEvent.html)
Title: Re: Simple graphics in [java] [tutorial]
Post by: c4ooo on October 31, 2016, 10:30:17 PM
Double post >_>
Also rip you derailed my thread _._
Title: Re: Simple graphics in [java] [tutorial]
Post by: 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" ^^
anyways thanks to both of you!! :)  :thumbsup:
I guess I really couldnt finish my game without all your help ^.^  :love:
Title: Re: Simple graphics in [java] [tutorial]
Post by: kotu on November 01, 2016, 07:53:21 AM
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!