-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlockEditGUI.java
788 lines (729 loc) · 40.3 KB
/
BlockEditGUI.java
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
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
public class BlockEditGUI {
private static boolean firstButton = true;
private static String sortType = "Name";
protected static int mouseY;
protected static int mouseX;
private static int scrollDist = 0;
private static Map<String, BufferedImage> imageCache = new HashMap<>();
private static String [] blocksArray;
private static String backgroundType = "HighRes/Background 1.png";
public static void blockEditGUI() {
JFrame newFrame = new JFrame("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
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
// Create Canvas 1 at the top
JPanel canvas1 = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Font font = new Font("Arial", Font.PLAIN, 60);
BufferedImage textBar = loadImage("ViewPannelAssets\\TopTypeBar.png");
if (textBar != null) {
g.drawImage(textBar, 0, 0, 1080, 720, this);
}
BufferedImage quantityBG = loadImage("ViewPannelAssets\\Spacer.png");
if (quantityBG != null) {
g.drawImage(quantityBG, 0, 0, 1080, 720, this);
}
// Draw Sort Type
String buttonFilePath = ""; // File path for dropdown button
String buttonFilePathON = ""; // File path for top
String buttonFilePathOFF = ""; // 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;
}
BufferedImage activeSortMethod = loadImage(buttonFilePath);
if (activeSortMethod != null) {
g.drawImage(activeSortMethod, 544, 0, 276, 50, this);
}
BufferedImage sortOption1 = loadImage(buttonFilePathON);
if (sortOption1 != null) {
g.drawImage(sortOption1, 820, 0, 130, 50, this);
}
BufferedImage sortOption2 = loadImage(buttonFilePathOFF);
if (sortOption2 != null) {
g.drawImage(sortOption2, 950, 0, 130, 50, this);
}
}
};
canvas1.setPreferredSize(new Dimension(1080, 50));
canvas1.setBackground(Color.BLUE); // Set background color for demonstration
canvas1.addMouseListener(new MouseInputAdapter() {
@Override
public void mouseClicked(java.awt.event.MouseEvent e) {
handleClickEventSort(e.getX(), e.getY());
}
});
// Add mouse listener to canvas panel to track mouse movements
canvas1.addMouseMotionListener(new MouseInputAdapter() {
@Override
public void mouseMoved(java.awt.event.MouseEvent e) {
mouseX = (e.getX());
mouseY = (e.getY());
}
});
// Create Canvas 2 on the left (60% height of the bottom space)
JPanel canvas2 = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
BufferedImage backgroundImage = loadImage(backgroundType);
if (backgroundImage != null) {
g.drawImage(backgroundImage, 0, -2000 + 50 + (scrollDist) / 3, 1080, 10000, this);
}
parserAndReadin parser = new parserAndReadin(); // initiallize new parser
performSortingMethods sorter = new performSortingMethods(); // 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
Font font = new Font("Arial", Font.PLAIN, 60);
int numberOfObjects = 0;
int sizeOfFrontPng = 0;
for (int i = 0; i < blocksArray.length; i++) {
int y = (scrollDist) + (i * 100) + 100 + 20; // Adjust Y position based on loop iteration (for drwaing name and desc)
Block block = parser.getBlockByName(blocks, blocksArray[i]);
if (isWithinButtonRange(mouseX, mouseY, 5, 950, (scrollDist) + (i * 100) + 50, (scrollDist) + (i * 100) + 150)) {
BufferedImage highlighted = 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 = new Font("Arial", Font.PLAIN, 45);
sizeOfFrontPng = 70;
}
BufferedImage blockFront = 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 {
BufferedImage unselected = loadImage("ViewPannelAssets\\BackGroundOfBlockUnselected.png");
if (unselected != null) {
g.drawImage(unselected, 0 , (scrollDist) + (i * 100), 1080, 720, this);
g.setColor(Color.LIGHT_GRAY);
font = new Font("Arial", Font.PLAIN, 40);
sizeOfFrontPng = 60;
}
BufferedImage blockFront = 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
String text = String.valueOf(i + 1);
g.drawString(text, 15, y);
text = blocksArray[i];
g.drawString(text, 150, y);
numberOfObjects += 1;
}
}
};
canvas2.setPreferredSize(new Dimension(500, 670)); // Adjust width as needed
canvas2.setBackground(Color.RED); // Set background color for demonstration
canvas2.addMouseListener(new MouseInputAdapter() {
@Override
public void mouseClicked(java.awt.event.MouseEvent e) {
handleClickEventSort(e.getX(), e.getY());
}
});
// Add mouse listener to canvas panel to track mouse movements
canvas2.addMouseMotionListener(new MouseInputAdapter() {
@Override
public void mouseMoved(java.awt.event.MouseEvent e) {
mouseX = (e.getX());
mouseY = (e.getY());
}
});
canvas2.addMouseWheelListener(new MouseWheelListener() {
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
int scrollAmount = e.getWheelRotation();
if (scrollAmount < 0) {
if(scrollDist > 0) {
} else {
scrollDist += 20;
}
} else if (scrollAmount > 0) {
scrollDist -= 20;
}
canvas2.repaint();
}
});
// Create Canvas 3 on the right of Canvas 2 (40% height of the bottom space)
JPanel canvas3 = new JPanel();
canvas3.setPreferredSize(new Dimension(220, 670)); // Adjust width as needed
canvas3.setBackground(Color.LIGHT_GRAY); // Set background color for demonstration
// Add buttons to canvas3
canvas3.setLayout(new GridLayout(3, 1)); // 3 rows, 1 column
JButton addButton = new JButton();
JButton removeButton = new JButton();
JButton editButton = new JButton();
// Load images for the buttons' states
ImageIcon addButtonIconON = new ImageIcon("ViewPannelAssets\\addOn.png");
ImageIcon addButtonIconOFF = new ImageIcon("ViewPannelAssets\\addOff.png");
ImageIcon removeButtonIconON = new ImageIcon("ViewPannelAssets\\removeOn.png");
ImageIcon removeButtonIconOFF = new ImageIcon("ViewPannelAssets\\removeOff.png");
ImageIcon editButtonIconON = new ImageIcon("ViewPannelAssets\\editOn.png");
ImageIcon editButtonIconOFF = new ImageIcon("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(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// Initialize variables
String blockName;
boolean blockRenewability = false; // default value
String blockStackability;
double blockBlastRes;
double blockHardness;
double blockLuminous;
boolean blockFlammability = false; // default value
String blockDimension;
parserAndReadin parser = new parserAndReadin();
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
int isRenewable = 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;
}
else if (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 (NumberFormatException ex)
{
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 (NumberFormatException ex)
{
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 (NumberFormatException ex)
{
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
int isFlammable = 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;
}
else if (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
Block newBlock = new Block(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(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
String blockName;
parserAndReadin parser = new parserAndReadin();
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(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
String blockName;
boolean blockRenewability = false; // default value
String blockStackability;
double blockBlastRes;
double blockHardness;
double blockLuminous;
boolean blockFlammability = false; // default value
String blockDimension;
System.out.println("Button 3 clicked");
parserAndReadin parser = new parserAndReadin();
ArrayList<Block> blocks = parser.readInData("data/Blocks.txt");
String blockChoice = JOptionPane.showInputDialog(null, "Enter the name:");
if (blockChoice != null && !blockChoice.trim().isEmpty())
{
System.out.println("You entered: " + blockChoice);
try
{
Block preEditBlock = parser.getBlockByName(blocks, blockChoice);
if (preEditBlock == null)
{
throw new Exception("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();
Block updatedBlock = new Block(blockName, blockRenewability, blockStackability, blockBlastRes, blockHardness, blockLuminous, blockFlammability, blockDimension, false, "null");
Object[] options = {"Name", "Renewability", "Stackability", "Blastres", "Hardness", "Luminousity", "Flammability", "Dimension"};
int choice = 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)
{
case 0: // Name
String newName = JOptionPane.showInputDialog(null, "Enter the new name:");
if (newName != null && !newName.trim().isEmpty())
{
updatedBlock = new Block(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;
case 1: // Renewability
boolean newRenewability;
int renewabilityChoice = 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;
}
else if (renewabilityChoice == JOptionPane.NO_OPTION)
{
System.out.println("User chose No.");
newRenewability = false;
}
else
{
System.out.println("User cancelled the operation.");
return;
}
updatedBlock = new Block(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;
case 2: // Stackability
String newStackability = JOptionPane.showInputDialog(null, "Enter the new stackability:");
if (newStackability != null && !newStackability.trim().isEmpty())
{
updatedBlock = new Block(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;
case 3: // Blast resistance
try
{
double newBlastRes = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter the new blast resistance:"));
updatedBlock = new Block(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 (NumberFormatException ex)
{
JOptionPane.showMessageDialog(null, "Invalid input. Please enter a numeric value!");
}
break;
case 4: // Hardness
try
{
double newHardness = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter the new hardness:"));
updatedBlock = new Block(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 (NumberFormatException ex)
{
JOptionPane.showMessageDialog(null, "Invalid input. Please enter a numeric value!");
}
break;
case 5: // Luminosity
try
{
double newLuminous = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter the new luminosity:"));
updatedBlock = new Block(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 (NumberFormatException ex)
{
JOptionPane.showMessageDialog(null, "Invalid input. Please enter a numeric value!");
}
break;
case 6: // Flammability
boolean newFlammability;
int flammChoice = JOptionPane.showConfirmDialog(null, "Is it flammable?", "Flammability", JOptionPane.YES_NO_OPTION);
if (flammChoice == JOptionPane.YES_OPTION)
{
System.out.println("User chose Yes.");
newFlammability = true;
}
else if (flammChoice == JOptionPane.NO_OPTION)
{
System.out.println("User chose No.");
newFlammability = false;
}
else
{
System.out.println("User cancelled the operation.");
return;
}
updatedBlock = new Block(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;
case 7: // Dimension
String newDimension = JOptionPane.showInputDialog(null, "Where does the block spawn?");
if (newDimension != null && !newDimension.trim().isEmpty())
{
updatedBlock = new Block(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 (Exception ex)
{
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
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new BorderLayout());
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
Timer timer = new Timer(1000 / Main.FRAME_RATE, e -> {
mainPanel.repaint(); // Repaint the canvas every frame
});
timer.start(); // Start the timer
}
private static void handleClickEventSort(int x, int y) {
if (isWithinButtonRange(x, y, 544, 819, 0, 50)) {
if (sortType == "Name") {
sortType = "Stackable";
backgroundType = "HighRes/Background 2.png";
} else if (sortType == "Stackable") {
sortType = "Dimension";
backgroundType = "HighRes/Background 3.png";
} else if (sortType == "Dimension") {
sortType = "Hardness";
backgroundType = "HighRes/Background 4.png";
} else if (sortType == "Hardness") {
sortType = "BlastRes";
backgroundType = "HighRes/Background 5.png";
} else if (sortType == "BlastRes") {
sortType = "Luminous";
backgroundType = "HighRes/Background 6.png";
} else if (sortType == "Luminous") {
sortType = "Renewable";
backgroundType = "HighRes/Background 7.png";
} else if (sortType == "Renewable") {
sortType = "Fire";
backgroundType = "HighRes/Background 8.png";
} else if (sortType == "Fire") {
sortType = "Name";
backgroundType = "HighRes/Background 1.png";
} else {
sortType = "Name";
backgroundType = "HighRes/Background 1.png";
}
} else if (isWithinButtonRange(x, y, 820, 950, 0, 50)) {
firstButton = true;
} else if (isWithinButtonRange(x, y, 950, 1080, 0, 50)) {
firstButton = false;
}
}
private static boolean isWithinButtonRange(int mouseX, int mouseY, int buttonX, int buttonWidth, int buttonY, int buttonHeight) {
return mouseX > buttonX && mouseX < buttonWidth && mouseY > buttonY && mouseY < buttonHeight;
}
// Define a class to represent each sorting type with its associated file paths
static class SortType {
String type;
String buttonFilePath;
String buttonFilePathON;
String buttonFilePathOFF;
public SortType(String type, String buttonFilePath, String buttonFilePathON, String buttonFilePathOFF) {
this.type = type;
this.buttonFilePath = buttonFilePath;
this.buttonFilePathON = buttonFilePathON;
this.buttonFilePathOFF = buttonFilePathOFF;
}
}
private static BufferedImage loadImage(String filename) {
if (imageCache.containsKey(filename)) {
return imageCache.get(filename);
} else {
try {
BufferedImage image = ImageIO.read(new File(filename));
imageCache.put(filename, image);
return image;
} catch (IOException e) {
//e.printStackTrace();
return null;
}
}
}
}