- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockEditGUI.java
More file actions
Latest commit
788 lines (729 loc) · 40.3 KB
/
Copy pathBlockEditGUI.java
File metadata and controls
788 lines (729 loc) · 40.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
importjavax.imageio.ImageIO;
importjavax.swing.*;
importjavax.swing.event.MouseInputAdapter;
importjava.awt.*;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.MouseWheelEvent;
importjava.awt.event.MouseWheelListener;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.IOException;
importjava.util.HashMap;
importjava.util.Map;
importjava.util.ArrayList;
publicclassBlockEditGUI {
privatestaticbooleanfirstButton = true;
privatestaticStringsortType = "Name";
protectedstaticintmouseY;
protectedstaticintmouseX;
privatestaticintscrollDist = 0;
privatestaticMap<String, BufferedImage> imageCache = newHashMap<>();
privatestaticString [] blocksArray;
privatestaticStringbackgroundType = "HighRes/Background 1.png";
publicstaticvoidblockEditGUI() {
JFramenewFrame = newJFrame("Edit Blocks");
newFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
newFrame.setSize(Main.FRAME_WIDTH, Main.FRAME_HEIGHT);
newFrame.setResizable(false);
// Create the main panel to hold the canvases
JPanelmainPanel = newJPanel();
mainPanel.setLayout(newBorderLayout());
// Create Canvas 1 at the top
JPanelcanvas1 = newJPanel() {
@Override
protectedvoidpaintComponent(Graphicsg) {
super.paintComponent(g);
Fontfont = newFont("Arial", Font.PLAIN, 60);
BufferedImagetextBar = loadImage("ViewPannelAssets\\TopTypeBar.png");
if (textBar != null) {
g.drawImage(textBar, 0, 0, 1080, 720, this);
}
BufferedImagequantityBG = loadImage("ViewPannelAssets\\Spacer.png");
if (quantityBG != null) {
g.drawImage(quantityBG, 0, 0, 1080, 720, this);
}
// Draw Sort Type
StringbuttonFilePath = ""; // File path for dropdown button
StringbuttonFilePathON = ""; // File path for top
StringbuttonFilePathOFF = ""; // File path for bottom
switch (sortType) {
case"Name":
buttonFilePath = "ViewPannelAssets\\Name.png";
if (firstButton) {
buttonFilePathON = "ViewPannelAssets\\A-ZON.png";
buttonFilePathOFF = "ViewPannelAssets\\Z-AOFF.png";
} else {
buttonFilePathON = "ViewPannelAssets\\A-ZOFF.png";
buttonFilePathOFF = "ViewPannelAssets\\Z-AON.png";
}
break;
case"Stackable":
buttonFilePath = "ViewPannelAssets\\Stack.png";
if (firstButton) {
buttonFilePathON = "ViewPannelAssets\\StackedON.png";
buttonFilePathOFF = "ViewPannelAssets\\NotOFF.png";
} else {
buttonFilePathON = "ViewPannelAssets\\StackedOFF.png";
buttonFilePathOFF = "ViewPannelAssets\\NotON.png";
}
break;
case"Dimension":
buttonFilePath = "ViewPannelAssets\\Dimension.png";
if (firstButton) {
buttonFilePathON = "ViewPannelAssets\\TopON.png";
buttonFilePathOFF = "ViewPannelAssets\\BottomOFF.png";
} else {
buttonFilePathON = "ViewPannelAssets\\TopOFF.png";
buttonFilePathOFF = "ViewPannelAssets\\BottomON.png";
}
break;
case"Hardness":
buttonFilePath = "ViewPannelAssets\\Hardness.png";
if (firstButton) {
buttonFilePathON = "ViewPannelAssets\\HardestON.png";
buttonFilePathOFF = "ViewPannelAssets\\SoftestOFF.png";
} else {
buttonFilePathON = "ViewPannelAssets\\HardestOFF.png";
buttonFilePathOFF = "ViewPannelAssets\\SoftestON.png";
}
break;
case"BlastRes":
buttonFilePath = "ViewPannelAssets\\BlastRes.png";
if (firstButton) {
buttonFilePathON = "ViewPannelAssets\\MostON.png";
buttonFilePathOFF = "ViewPannelAssets\\LeastOFF.png";
} else {
buttonFilePathON = "ViewPannelAssets\\MostOFF.png";
buttonFilePathOFF = "ViewPannelAssets\\LeastON.png";
}
break;
case"Renewable":
buttonFilePath = "ViewPannelAssets\\Renewability.png";
if (firstButton) {
buttonFilePathON = "ViewPannelAssets\\TrueON.png";
buttonFilePathOFF = "ViewPannelAssets\\FalseOFF.png";
} else {
buttonFilePathON = "ViewPannelAssets\\TrueOFF.png";
buttonFilePathOFF = "ViewPannelAssets\\FalseON.png";
}
break;
case"Luminous":
buttonFilePath = "ViewPannelAssets\\Luminous.png";
if (firstButton) {
buttonFilePathON = "ViewPannelAssets\\TrueON.png";
buttonFilePathOFF = "ViewPannelAssets\\FalseOFF.png";
} else {
buttonFilePathON = "ViewPannelAssets\\TrueOFF.png";
buttonFilePathOFF = "ViewPannelAssets\\FalseON.png";
}
break;
case"Fire":
buttonFilePath = "ViewPannelAssets\\Flammable.png";
if (firstButton) {
buttonFilePathON = "ViewPannelAssets\\TrueON.png";
buttonFilePathOFF = "ViewPannelAssets\\FalseOFF.png";
} else {
buttonFilePathON = "ViewPannelAssets\\TrueOFF.png";
buttonFilePathOFF = "ViewPannelAssets\\FalseON.png";
}
break;
}
BufferedImageactiveSortMethod = loadImage(buttonFilePath);
if (activeSortMethod != null) {
g.drawImage(activeSortMethod, 544, 0, 276, 50, this);
}
BufferedImagesortOption1 = loadImage(buttonFilePathON);
if (sortOption1 != null) {
g.drawImage(sortOption1, 820, 0, 130, 50, this);
}
BufferedImagesortOption2 = loadImage(buttonFilePathOFF);
if (sortOption2 != null) {
g.drawImage(sortOption2, 950, 0, 130, 50, this);
}
}
};
canvas1.setPreferredSize(newDimension(1080, 50));
canvas1.setBackground(Color.BLUE); // Set background color for demonstration
canvas1.addMouseListener(newMouseInputAdapter() {
@Override
publicvoidmouseClicked(java.awt.event.MouseEvente) {
handleClickEventSort(e.getX(), e.getY());
}
});
// Add mouse listener to canvas panel to track mouse movements
canvas1.addMouseMotionListener(newMouseInputAdapter() {
@Override
publicvoidmouseMoved(java.awt.event.MouseEvente) {
mouseX = (e.getX());
mouseY = (e.getY());
}
});
// Create Canvas 2 on the left (60% height of the bottom space)
JPanelcanvas2 = newJPanel() {
@Override
protectedvoidpaintComponent(Graphicsg) {
super.paintComponent(g);
BufferedImagebackgroundImage = loadImage(backgroundType);
if (backgroundImage != null) {
g.drawImage(backgroundImage, 0, -2000 + 50 + (scrollDist) / 3, 1080, 10000, this);
}
parserAndReadinparser = newparserAndReadin(); // initiallize new parser
performSortingMethodssorter = newperformSortingMethods(); // initialize new sortingmethods
ArrayList<Block> blocks = parser.readInData("data/Blocks.txt"); // read in our blocks data file
blocksArray = sorter.sortBlockParameter("name", "asc"); // Initial sorting when gui open
Fontfont = newFont("Arial", Font.PLAIN, 60);
intnumberOfObjects = 0;
intsizeOfFrontPng = 0;
for (inti = 0; i < blocksArray.length; i++) {
inty = (scrollDist) + (i * 100) + 100 + 20; // Adjust Y position based on loop iteration (for drwaing name and desc)
Blockblock = parser.getBlockByName(blocks, blocksArray[i]);
if (isWithinButtonRange(mouseX, mouseY, 5, 950, (scrollDist) + (i * 100) + 50, (scrollDist) + (i * 100) + 150)) {
BufferedImagehighlighted = loadImage("ViewPannelAssets\\BackGroundOfBlockSelected.png");
if (highlighted != null) {
g.drawImage(highlighted, 0 - 5 , (scrollDist) + (i * 100) - 5, 1080 + 10, 720 + 10, this);
g.setColor(Color.WHITE);
font = newFont("Arial", Font.PLAIN, 45);
sizeOfFrontPng = 70;
}
BufferedImageblockFront = loadImage("object\\" + block.getName() + "\\" + "front.jpg.jpg"); // add front png of block to block
if (blockFront != null) {
g.drawImage(blockFront, 70, y-60, sizeOfFrontPng, sizeOfFrontPng, this);
} else {
blockFront = loadImage("object\\" + "front.jpg.jpg");
g.drawImage(blockFront, 70, y-50, sizeOfFrontPng, sizeOfFrontPng, this);
}
} else {
BufferedImageunselected = loadImage("ViewPannelAssets\\BackGroundOfBlockUnselected.png");
if (unselected != null) {
g.drawImage(unselected, 0 , (scrollDist) + (i * 100), 1080, 720, this);
g.setColor(Color.LIGHT_GRAY);
font = newFont("Arial", Font.PLAIN, 40);
sizeOfFrontPng = 60;
}
BufferedImageblockFront = loadImage("object\\" + block.getName() + "\\" + "front.jpg.jpg"); // add front png of block to block
if (blockFront != null) {
g.drawImage(blockFront, 80, y-50, sizeOfFrontPng, sizeOfFrontPng, this);
} else {
blockFront = loadImage("object\\" + "front.jpg.jpg");
g.drawImage(blockFront, 80, y-50, sizeOfFrontPng, sizeOfFrontPng, this);
}
}
g.setFont(font);
y = (scrollDist) + (i * 100) + 100 + 20 - 10; // Adjust Y position based on loop iteration
Stringtext = String.valueOf(i + 1);
g.drawString(text, 15, y);
text = blocksArray[i];
g.drawString(text, 150, y);
numberOfObjects += 1;
}
}
};
canvas2.setPreferredSize(newDimension(500, 670)); // Adjust width as needed
canvas2.setBackground(Color.RED); // Set background color for demonstration
canvas2.addMouseListener(newMouseInputAdapter() {
@Override
publicvoidmouseClicked(java.awt.event.MouseEvente) {
handleClickEventSort(e.getX(), e.getY());
}
});
// Add mouse listener to canvas panel to track mouse movements
canvas2.addMouseMotionListener(newMouseInputAdapter() {
@Override
publicvoidmouseMoved(java.awt.event.MouseEvente) {
mouseX = (e.getX());
mouseY = (e.getY());
}
});
canvas2.addMouseWheelListener(newMouseWheelListener() {
@Override
publicvoidmouseWheelMoved(MouseWheelEvente) {
intscrollAmount = e.getWheelRotation();
if (scrollAmount < 0) {
if(scrollDist > 0) {
} else {
scrollDist += 20;
}
} elseif (scrollAmount > 0) {
scrollDist -= 20;
}
canvas2.repaint();
}
});
// Create Canvas 3 on the right of Canvas 2 (40% height of the bottom space)
JPanelcanvas3 = newJPanel();
canvas3.setPreferredSize(newDimension(220, 670)); // Adjust width as needed
canvas3.setBackground(Color.LIGHT_GRAY); // Set background color for demonstration
// Add buttons to canvas3
canvas3.setLayout(newGridLayout(3, 1)); // 3 rows, 1 column
JButtonaddButton = newJButton();
JButtonremoveButton = newJButton();
JButtoneditButton = newJButton();
// Load images for the buttons' states
ImageIconaddButtonIconON = newImageIcon("ViewPannelAssets\\addOn.png");
ImageIconaddButtonIconOFF = newImageIcon("ViewPannelAssets\\addOff.png");
ImageIconremoveButtonIconON = newImageIcon("ViewPannelAssets\\removeOn.png");
ImageIconremoveButtonIconOFF = newImageIcon("ViewPannelAssets\\removeOff.png");
ImageIconeditButtonIconON = newImageIcon("ViewPannelAssets\\editOn.png");
ImageIconeditButtonIconOFF = newImageIcon("ViewPannelAssets\\editOff.png");
// Set icons to the buttons
addButton.setIcon(addButtonIconOFF);
addButton.setPressedIcon(addButtonIconON);
removeButton.setIcon(removeButtonIconOFF);
removeButton.setPressedIcon(removeButtonIconON);
editButton.setIcon(editButtonIconOFF);
editButton.setPressedIcon(editButtonIconON);
// Add action listeners to buttons
addButton.addActionListener(newActionListener()
{
@Override
publicvoidactionPerformed(ActionEvente)
{
// Initialize variables
StringblockName;
booleanblockRenewability = false; // default value
StringblockStackability;
doubleblockBlastRes;
doubleblockHardness;
doubleblockLuminous;
booleanblockFlammability = false; // default value
StringblockDimension;
parserAndReadinparser = newparserAndReadin();
ArrayList<Block> blocks = parser.readInData("data/Blocks.txt");
System.out.println("Button 1 clicked");
// Get Block Name
blockName = JOptionPane.showInputDialog(null, "Enter the name:");
if (blockName != null && !blockName.trim().isEmpty())
{
System.out.println("You entered: " + blockName);
// Get Renewability
intisRenewable = JOptionPane.showConfirmDialog(null, "Is the block renewable?", "Renewability", JOptionPane.YES_NO_OPTION);
if (isRenewable == JOptionPane.YES_OPTION)
{
System.out.println("User chose Yes.");
blockRenewability = true;
}
elseif (isRenewable == JOptionPane.NO_OPTION)
{
System.out.println("User chose No.");
blockRenewability = false;
}
else
{
System.out.println("User cancelled the operation.");
return;
}
// Get Stackability
blockStackability = JOptionPane.showInputDialog(null, "Enter the stackability:");
if (blockStackability == null || blockStackability.trim().isEmpty())
{
System.out.println("Input dialog was cancelled.");
return;
}
// Get Blast Resistance
try
{
blockBlastRes = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter the blast resistance:"));
}
catch (NumberFormatExceptionex)
{
System.out.println("Invalid input for blast resistance.");
JOptionPane.showMessageDialog(null, "Invalid input for blast resistance. Please put in a numeric value next time!");
return;
}
// Get Hardness
try
{
blockHardness = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter the hardness:"));
}
catch (NumberFormatExceptionex)
{
System.out.println("Invalid input for hardness.");
JOptionPane.showMessageDialog(null, "Invalid input for blast hardness. Please put in a numeric value next time!");
return;
}
// Get Luminous
try
{
blockLuminous = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter the luminous:"));
}
catch (NumberFormatExceptionex)
{
System.out.println("Invalid input for luminous.");
JOptionPane.showMessageDialog(null, "Invalid input for luminous. Please put in a numeric value next time!");
return;
}
// Get Flammability
intisFlammable = JOptionPane.showConfirmDialog(null, "Is the block flammable?", "Flammability", JOptionPane.YES_NO_OPTION);
if (isFlammable == JOptionPane.YES_OPTION)
{
System.out.println("User chose Yes.");
blockFlammability = true;
}
elseif (isFlammable == JOptionPane.NO_OPTION)
{
System.out.println("User chose No.");
blockFlammability = false;
}
else
{
System.out.println("User cancelled the operation.");
return;
}
// Get Dimension
blockDimension = JOptionPane.showInputDialog(null, "Enter the dimension:");
if (blockDimension == null || blockDimension.trim().isEmpty())
{
System.out.println("Input dialog was cancelled.");
return;
}
// Create a new Block and add it to the list
BlocknewBlock = newBlock(blockName, blockRenewability, blockStackability, blockBlastRes, blockHardness, blockLuminous, blockFlammability, blockDimension, false, "null");
if (parser.addEntry(blocks, newBlock, "data//blocks.txt"))
System.out.println("New block added: " + newBlock);
else
JOptionPane.showMessageDialog(null, "Block of the same name already exists!");
}
else
System.out.println("Input dialog was cancelled or empty.");
}
});
removeButton.addActionListener(newActionListener()
{
@Override
publicvoidactionPerformed(ActionEvente)
{
StringblockName;
parserAndReadinparser = newparserAndReadin();
ArrayList<Block> blocks = parser.readInData("data/Blocks.txt");
System.out.println("Button 2 clicked");
blockName = JOptionPane.showInputDialog(null, "Enter the name:");
if (blockName != null && !blockName.trim().isEmpty())
{
System.out.println("You're trying to remove: " + blockName);
if (parser.removeEntry(blocks, blockName, "data//blocks.txt"))
JOptionPane.showMessageDialog(null, "Successfully removed " + blockName);
else
JOptionPane.showMessageDialog(null, "Hm, can't find "+blockName+", are you sure you spelled it right?");
}
else
System.out.println("dialog closed.");
}
});
editButton.addActionListener(newActionListener()
{
@Override
publicvoidactionPerformed(ActionEvente)
{
StringblockName;
booleanblockRenewability = false; // default value
StringblockStackability;
doubleblockBlastRes;
doubleblockHardness;
doubleblockLuminous;
booleanblockFlammability = false; // default value
StringblockDimension;
System.out.println("Button 3 clicked");
parserAndReadinparser = newparserAndReadin();
ArrayList<Block> blocks = parser.readInData("data/Blocks.txt");
StringblockChoice = JOptionPane.showInputDialog(null, "Enter the name:");
if (blockChoice != null && !blockChoice.trim().isEmpty())
{
System.out.println("You entered: " + blockChoice);
try
{
BlockpreEditBlock = parser.getBlockByName(blocks, blockChoice);
if (preEditBlock == null)
{
thrownewException("Block not found");
}
blockName = preEditBlock.getName();
blockRenewability = preEditBlock.getRenewability();
blockStackability = preEditBlock.getStackability();
blockBlastRes = preEditBlock.getBlastres();
blockHardness = preEditBlock.getHardness();
blockLuminous = preEditBlock.getLuminous();
blockFlammability = preEditBlock.getFlammable();
blockDimension = preEditBlock.getDimension();
BlockupdatedBlock = newBlock(blockName, blockRenewability, blockStackability, blockBlastRes, blockHardness, blockLuminous, blockFlammability, blockDimension, false, "null");
Object[] options = {"Name", "Renewability", "Stackability", "Blastres", "Hardness", "Luminousity", "Flammability", "Dimension"};
intchoice = JOptionPane.showOptionDialog(null,
"What attribute would you like to change?",
"Block Edit Panel",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[0]);
if (choice != -1)
{
System.out.println("User chose: " + options[choice]);
switch (choice)
{
case0: // Name
StringnewName = JOptionPane.showInputDialog(null, "Enter the new name:");
if (newName != null && !newName.trim().isEmpty())
{
updatedBlock = newBlock(newName, blockRenewability, blockStackability, blockBlastRes, blockHardness, blockLuminous, blockFlammability, blockDimension, false, "null");
if (parser.editEntry(blocks, blockChoice, updatedBlock, "data/Blocks.txt"))
{
JOptionPane.showMessageDialog(null, "Success!");
blockName = newName; // Update blockName if the name changes
}
else
{
JOptionPane.showMessageDialog(null, "Error occurred. Try again!");
}
}
break;
case1: // Renewability
booleannewRenewability;
intrenewabilityChoice = JOptionPane.showConfirmDialog(null, "Is the block renewable?", "Renewability", JOptionPane.YES_NO_OPTION);
if (renewabilityChoice == JOptionPane.YES_OPTION)
{
System.out.println("User chose Yes.");
newRenewability = true;
}
elseif (renewabilityChoice == JOptionPane.NO_OPTION)
{
System.out.println("User chose No.");
newRenewability = false;
}
else
{
System.out.println("User cancelled the operation.");
return;
}
updatedBlock = newBlock(blockName, newRenewability, blockStackability, blockBlastRes, blockHardness, blockLuminous, blockFlammability, blockDimension, false, "null");
if (parser.editEntry(blocks, blockChoice, updatedBlock, "data/Blocks.txt"))
{
JOptionPane.showMessageDialog(null, "Success!");
}
else
{
JOptionPane.showMessageDialog(null, "Error occurred. Try again!");
}
break;
case2: // Stackability
StringnewStackability = JOptionPane.showInputDialog(null, "Enter the new stackability:");
if (newStackability != null && !newStackability.trim().isEmpty())
{
updatedBlock = newBlock(blockName, blockRenewability, newStackability, blockBlastRes, blockHardness, blockLuminous, blockFlammability, blockDimension, false, "null");
if (parser.editEntry(blocks, blockChoice, updatedBlock, "data/Blocks.txt"))
{
JOptionPane.showMessageDialog(null, "Success!");
}
else
{
JOptionPane.showMessageDialog(null, "Error occurred. Try again!");
}
}
break;
case3: // Blast resistance
try
{
doublenewBlastRes = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter the new blast resistance:"));
updatedBlock = newBlock(blockName, blockRenewability, blockStackability, newBlastRes, blockHardness, blockLuminous, blockFlammability, blockDimension, false, "null");
if (parser.editEntry(blocks, blockChoice, updatedBlock, "data/Blocks.txt"))
{
JOptionPane.showMessageDialog(null, "Success!");
}
else
{
JOptionPane.showMessageDialog(null, "Error occurred. Try again!");
}
}
catch (NumberFormatExceptionex)
{
JOptionPane.showMessageDialog(null, "Invalid input. Please enter a numeric value!");
}
break;
case4: // Hardness
try
{
doublenewHardness = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter the new hardness:"));
updatedBlock = newBlock(blockName, blockRenewability, blockStackability, blockBlastRes, newHardness, blockLuminous, blockFlammability, blockDimension, false, "null");
if (parser.editEntry(blocks, blockChoice, updatedBlock, "data/Blocks.txt"))
{
JOptionPane.showMessageDialog(null, "Success!");
}
else
{
JOptionPane.showMessageDialog(null, "Error occurred. Try again!");
}
}
catch (NumberFormatExceptionex)
{
JOptionPane.showMessageDialog(null, "Invalid input. Please enter a numeric value!");
}
break;
case5: // Luminosity
try
{
doublenewLuminous = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter the new luminosity:"));
updatedBlock = newBlock(blockName, blockRenewability, blockStackability, blockBlastRes, blockHardness, newLuminous, blockFlammability, blockDimension, false, "null");
if (parser.editEntry(blocks, blockChoice, updatedBlock, "data/Blocks.txt"))
{
JOptionPane.showMessageDialog(null, "Success!");
}
else
{
JOptionPane.showMessageDialog(null, "Error occurred. Try again!");
}
}
catch (NumberFormatExceptionex)
{
JOptionPane.showMessageDialog(null, "Invalid input. Please enter a numeric value!");
}
break;
case6: // Flammability
booleannewFlammability;
intflammChoice = JOptionPane.showConfirmDialog(null, "Is it flammable?", "Flammability", JOptionPane.YES_NO_OPTION);
if (flammChoice == JOptionPane.YES_OPTION)
{
System.out.println("User chose Yes.");
newFlammability = true;
}
elseif (flammChoice == JOptionPane.NO_OPTION)
{
System.out.println("User chose No.");
newFlammability = false;
}
else
{
System.out.println("User cancelled the operation.");
return;
}
updatedBlock = newBlock(blockName, blockRenewability, blockStackability, blockBlastRes, blockHardness, blockLuminous, newFlammability, blockDimension, false, "null");
if (parser.editEntry(blocks, blockChoice, updatedBlock, "data/Blocks.txt"))
{
JOptionPane.showMessageDialog(null, "Success!");
}
else
{
JOptionPane.showMessageDialog(null, "Error occurred. Try again!");
}
break;
case7: // Dimension
StringnewDimension = JOptionPane.showInputDialog(null, "Where does the block spawn?");
if (newDimension != null && !newDimension.trim().isEmpty())
{
updatedBlock = newBlock(blockName, blockRenewability, blockStackability, blockBlastRes, blockHardness, blockLuminous, blockFlammability, newDimension, false, "null");
if (parser.editEntry(blocks, blockChoice, updatedBlock, "data/Blocks.txt"))
{
JOptionPane.showMessageDialog(null, "Success!");
}
else
{
JOptionPane.showMessageDialog(null, "Error occurred. Try again!");
}
}
break;
default:
System.out.println("I don't know what happened???");
break;
}
}
else
{
System.out.println("User cancelled the operation.");
}
}
catch (Exceptionex)
{
JOptionPane.showMessageDialog(null, "Block not found: " + blockChoice);
System.out.println("Block not found: " + blockChoice);
}
}
}
});
// Add buttons to canvas3
canvas3.add(addButton);
canvas3.add(removeButton);
canvas3.add(editButton);
// Create a nested panel for Canvas 2 and Canvas 3
JPanelbottomPanel = newJPanel();
bottomPanel.setLayout(newBorderLayout());
bottomPanel.add(canvas2, BorderLayout.WEST);
bottomPanel.add(canvas3, BorderLayout.CENTER);
// Add Canvas 1 to the top of the main panel
mainPanel.add(canvas1, BorderLayout.NORTH);
// Add the bottom nested panel (Canvas 2 and Canvas 3) to the center of the main panel
mainPanel.add(bottomPanel, BorderLayout.CENTER);
// Add the main panel to the frame
newFrame.add(mainPanel);
// Display the frame
newFrame.setVisible(true);
// Set up a timer to update and repaint the canvas at a fixed interval
Timertimer = newTimer(1000 / Main.FRAME_RATE, e -> {
mainPanel.repaint(); // Repaint the canvas every frame
});
timer.start(); // Start the timer
}
privatestaticvoidhandleClickEventSort(intx, inty) {
if (isWithinButtonRange(x, y, 544, 819, 0, 50)) {
if (sortType == "Name") {
sortType = "Stackable";
backgroundType = "HighRes/Background 2.png";
} elseif (sortType == "Stackable") {
sortType = "Dimension";
backgroundType = "HighRes/Background 3.png";
} elseif (sortType == "Dimension") {
sortType = "Hardness";
backgroundType = "HighRes/Background 4.png";
} elseif (sortType == "Hardness") {
sortType = "BlastRes";
backgroundType = "HighRes/Background 5.png";
} elseif (sortType == "BlastRes") {
sortType = "Luminous";
backgroundType = "HighRes/Background 6.png";
} elseif (sortType == "Luminous") {
sortType = "Renewable";
backgroundType = "HighRes/Background 7.png";
} elseif (sortType == "Renewable") {
sortType = "Fire";
backgroundType = "HighRes/Background 8.png";
} elseif (sortType == "Fire") {
sortType = "Name";
backgroundType = "HighRes/Background 1.png";
} else {
sortType = "Name";
backgroundType = "HighRes/Background 1.png";
}
} elseif (isWithinButtonRange(x, y, 820, 950, 0, 50)) {
firstButton = true;
} elseif (isWithinButtonRange(x, y, 950, 1080, 0, 50)) {
firstButton = false;
}
}
privatestaticbooleanisWithinButtonRange(intmouseX, intmouseY, intbuttonX, intbuttonWidth, intbuttonY, intbuttonHeight) {
returnmouseX > buttonX && mouseX < buttonWidth && mouseY > buttonY && mouseY < buttonHeight;
}
// Define a class to represent each sorting type with its associated file paths
staticclassSortType {
Stringtype;
StringbuttonFilePath;
StringbuttonFilePathON;
StringbuttonFilePathOFF;
publicSortType(Stringtype, StringbuttonFilePath, StringbuttonFilePathON, StringbuttonFilePathOFF) {
this.type = type;
this.buttonFilePath = buttonFilePath;
this.buttonFilePathON = buttonFilePathON;
this.buttonFilePathOFF = buttonFilePathOFF;
}
}
privatestaticBufferedImageloadImage(Stringfilename) {
if (imageCache.containsKey(filename)) {
returnimageCache.get(filename);
} else {
try {
BufferedImageimage = ImageIO.read(newFile(filename));
imageCache.put(filename, image);
returnimage;
} catch (IOExceptione) {
//e.printStackTrace();
returnnull;
}
}
}
}