SlideShare a Scribd company logo
Why am I getting an out of memory error and no window of my application is appearing?
code starts here:
package slidePuzzle;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.util.ArrayList;
import java.util.Collections;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Game extends JFrame {
// declare variables, arrays, and constants
private Buttons emptyButton;
private ArrayList<Buttons> buttons;
private ArrayList<Point> answer;
private final int TOTAL_BUTTONS = 12;
private final int TOTAL_WIDTH = 400;
private JPanel panel;
private Image img;
private BufferedImage src;
private BufferedImage mod;
private int width;
private int height;
// default constructor
public Game() {
// call initializer
initializeInterface();
}
private void initializeInterface() {
ArrayList<Point> answer = new ArrayList<Point>();
ArrayList<Buttons> buttons = new ArrayList<Buttons>();
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createLineBorder(Color.black));
panel.setLayout(new GridLayout(4, 3, 0, 0));
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 3; y++) {
answer.add(new Point(x, y));
}
}
try {
src = loadImg();
int height = modifyHeight(src.getWidth(), src.getHeight());
mod = resizeImg(src, TOTAL_WIDTH, height, BufferedImage.TYPE_INT_ARGB);
}
catch (IOException exception) {
JOptionPane.showMessageDialog(this, "Image is unable to be rendered.", "Error",
JOptionPane.ERROR_MESSAGE);
}
height = mod.getHeight(null);
width = mod.getWidth(null);
for (int x = 0; x < 4; x++) {
for (int y = 0; y <3; y++) {
img = createImage(new FilteredImageSource(mod.getSource(),
new CropImageFilter(x * width / 3, y * height / 4, (width / 3), height / 4)));
Buttons button = new Buttons(img);
button.putClientProperty("position", new Point(x, y));
if (x == 3 && y == 2) {
emptyButton = new Buttons();
emptyButton.setBorderPainted(false);
emptyButton.setContentAreaFilled(false);
emptyButton.setEmptyButton();
emptyButton.putClientProperty("position", new Point(x, y));
} else {
buttons.add(button);
}
}
}
Collections.shuffle(buttons);
buttons.add(emptyButton);
for (int z = 0; z < TOTAL_BUTTONS; z++) {
Buttons button = buttons.get(z);
panel.add(button);
button.setBorder(BorderFactory.createLineBorder(Color.black));
button.addActionListener(new Click());
}
JFrame frame = new JFrame("Tile Slider");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Game());
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
}
private BufferedImage loadImg() throws IOException {
BufferedImage bufferedImage = ImageIO.read(new File("src/pond.jpg"));
return bufferedImage;
}
private BufferedImage resizeImg(BufferedImage natural, int width, int height, int type) {
BufferedImage adjustedImg = new BufferedImage(width, height, type);
Graphics2D graphics = adjustedImg.createGraphics();
// draw image to fit inside our window and return
graphics.drawImage(natural, 0, 0, width, height, null);
graphics.dispose();
return adjustedImg;
}
private int modifyHeight(int width, int height) {
double widthRatio = TOTAL_WIDTH / (double) width;
int modHeight = (int) (height * widthRatio);
return modHeight;
}
private class Click extends AbstractAction {
@Override
public void actionPerformed(ActionEvent event) {
check(event);
isCorrectConfiguration();
}
private void check(ActionEvent event) {
int emptyButtonIndex = 0;
for (Buttons btn: buttons) {
if (btn.getEmptyButton()) {
emptyButtonIndex = buttons.indexOf(btn);
}
}
JButton btn = (JButton) event.getSource();
int buttonIndex = buttons.indexOf(btn);
if ((buttonIndex + 1 == emptyButtonIndex) ||
(buttonIndex + 3 == emptyButtonIndex) ||
(buttonIndex - 1 == emptyButtonIndex) ||
(buttonIndex - 3 == emptyButtonIndex)) {
Collections.swap(buttons, emptyButtonIndex, buttonIndex);
update();
}
}
private void update() {
panel.removeAll();
for (JComponent btn: buttons) {
panel.add(btn);
}
panel.validate();
}
}
public void isCorrectConfiguration() {
ArrayList<Point> userState = new ArrayList<Point>();
for (JComponent btn: buttons) {
userState.add((Point) btn.getClientProperty("position"));
}
if (comparePuzzleState(answer, userState)) {
JOptionPane.showMessageDialog(panel, "You have completed the puzzle!");
}
}
public static boolean comparePuzzleState(ArrayList<Point> list1, ArrayList<Point> list2) {
return list1.toString().contentEquals(list2.toString());
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
Game slidePuzzle = new Game();
slidePuzzle.setVisible(true);
});
}
}

More Related Content

Similar to Why am I getting an out of memory error and no window of my .pdf (20)

PDF
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf
apexcomputer54
 
PDF
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdf
venkt12345
 
PDF
This is Java,I am currently stumped on how to add a scoreboard for.pdf
anjandavid
 
PDF
Hello!This is Java assignment applet.Can someone help me writing.pdf
jyothimuppasani1
 
PDF
Groovy-er Desktop Applications With Griffon
Matthew McCullough
 
PDF
Groovy-er desktop applications with Griffon
Eric Wendelin
 
PDF
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
rajkumarm401
 
PPT
J2 me 07_5
vanliemtb
 
PDF
Othello.javapackage othello;import core.Game; import userInter.pdf
arccreation001
 
PDF
Need help writing the code for a basic java tic tac toe game Tic.pdf
hainesburchett26321
 
PDF
package buttongui; import static com.sun.deploy.config.JREInf.pdf
arjuntiwari586
 
PDF
I need help creating a java gui that draws lines, shapes, characters.pdf
ezhilvizhiyan
 
PDF
java code please Add event handlers to the buttons in your TicTacToe.pdf
ezzi97
 
PDF
PLEASE HELP ME !!IT IS Due Tonight ;(!How can I make the add but.pdf
badshetoms
 
PDF
I am getting a syntax error. I cant seem to find whats causing t.pdf
fashionfolionr
 
PPT
Oop lecture9 10
Shahriar Robbani
 
PPT
Java awt
GaneshKumarKanthiah
 
PDF
Creating a Facebook Clone - Part XV - Transcript.pdf
ShaiAlmog1
 
PDF
I hope the below code is helpful to you, please give me rewards.im.pdf
apexelectronices01
 
PDF
Write Java FX code for this pseudocode of the void initilizaHistoryL.pdf
sales98
 
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf
apexcomputer54
 
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdf
venkt12345
 
This is Java,I am currently stumped on how to add a scoreboard for.pdf
anjandavid
 
Hello!This is Java assignment applet.Can someone help me writing.pdf
jyothimuppasani1
 
Groovy-er Desktop Applications With Griffon
Matthew McCullough
 
Groovy-er desktop applications with Griffon
Eric Wendelin
 
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
rajkumarm401
 
J2 me 07_5
vanliemtb
 
Othello.javapackage othello;import core.Game; import userInter.pdf
arccreation001
 
Need help writing the code for a basic java tic tac toe game Tic.pdf
hainesburchett26321
 
package buttongui; import static com.sun.deploy.config.JREInf.pdf
arjuntiwari586
 
I need help creating a java gui that draws lines, shapes, characters.pdf
ezhilvizhiyan
 
java code please Add event handlers to the buttons in your TicTacToe.pdf
ezzi97
 
PLEASE HELP ME !!IT IS Due Tonight ;(!How can I make the add but.pdf
badshetoms
 
I am getting a syntax error. I cant seem to find whats causing t.pdf
fashionfolionr
 
Oop lecture9 10
Shahriar Robbani
 
Creating a Facebook Clone - Part XV - Transcript.pdf
ShaiAlmog1
 
I hope the below code is helpful to you, please give me rewards.im.pdf
apexelectronices01
 
Write Java FX code for this pseudocode of the void initilizaHistoryL.pdf
sales98
 

More from aakarcreations1 (20)

PDF
Annies Land Company has identified the following two mutual.pdf
aakarcreations1
 
PDF
19 Critical thinking is required inaAll aspects of our .pdf
aakarcreations1
 
PDF
Discussion making decidion and overcoming bias using researc.pdf
aakarcreations1
 
PDF
Dave Czarnecki es el socio gerente de Czarnecki y Hogan una.pdf
aakarcreations1
 
PDF
Busque en lnea un informe de noticias sobre un artefacto de.pdf
aakarcreations1
 
PDF
Cules de las siguientes afirmaciones sobre el poder relaci.pdf
aakarcreations1
 
PDF
Cul de las siguientes afirmaciones sobre los anillos de Sa.pdf
aakarcreations1
 
PDF
Cul de estas afirmaciones es correcta Flujo de caja libre.pdf
aakarcreations1
 
PDF
Consider the following events X student gets an B+ on pap.pdf
aakarcreations1
 
PDF
Bir rnn hizmetin veya markann tketicilerin zihnindeki i.pdf
aakarcreations1
 
PDF
Using Table 111 calculate the compound amount and compound.pdf
aakarcreations1
 
PDF
Uzun zaman nce ok ok uzak bir galakside Atba Bulutsus.pdf
aakarcreations1
 
PDF
New Archaeologists made great strides forward in their appro.pdf
aakarcreations1
 
PDF
Un paciente VIH positivo ingres con lesiones cutneas en t.pdf
aakarcreations1
 
PDF
True or falseChina was struck with serious and significant .pdf
aakarcreations1
 
PDF
Tketim Biyografi Girii 2 Dikkat Her gn mesaj bombardmann.pdf
aakarcreations1
 
PDF
The US bilateral real exchange rate with Mexico is define.pdf
aakarcreations1
 
PDF
The comparative balance sheet of Whitman Co at December 31.pdf
aakarcreations1
 
PDF
The development of the FeedFish app will be done increment.pdf
aakarcreations1
 
PDF
The adjusting entry to record accrued expenses aincludes a .pdf
aakarcreations1
 
Annies Land Company has identified the following two mutual.pdf
aakarcreations1
 
19 Critical thinking is required inaAll aspects of our .pdf
aakarcreations1
 
Discussion making decidion and overcoming bias using researc.pdf
aakarcreations1
 
Dave Czarnecki es el socio gerente de Czarnecki y Hogan una.pdf
aakarcreations1
 
Busque en lnea un informe de noticias sobre un artefacto de.pdf
aakarcreations1
 
Cules de las siguientes afirmaciones sobre el poder relaci.pdf
aakarcreations1
 
Cul de las siguientes afirmaciones sobre los anillos de Sa.pdf
aakarcreations1
 
Cul de estas afirmaciones es correcta Flujo de caja libre.pdf
aakarcreations1
 
Consider the following events X student gets an B+ on pap.pdf
aakarcreations1
 
Bir rnn hizmetin veya markann tketicilerin zihnindeki i.pdf
aakarcreations1
 
Using Table 111 calculate the compound amount and compound.pdf
aakarcreations1
 
Uzun zaman nce ok ok uzak bir galakside Atba Bulutsus.pdf
aakarcreations1
 
New Archaeologists made great strides forward in their appro.pdf
aakarcreations1
 
Un paciente VIH positivo ingres con lesiones cutneas en t.pdf
aakarcreations1
 
True or falseChina was struck with serious and significant .pdf
aakarcreations1
 
Tketim Biyografi Girii 2 Dikkat Her gn mesaj bombardmann.pdf
aakarcreations1
 
The US bilateral real exchange rate with Mexico is define.pdf
aakarcreations1
 
The comparative balance sheet of Whitman Co at December 31.pdf
aakarcreations1
 
The development of the FeedFish app will be done increment.pdf
aakarcreations1
 
The adjusting entry to record accrued expenses aincludes a .pdf
aakarcreations1
 
Ad

Recently uploaded (20)

PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PPTX
SCHOOL-BASED SEXUAL HARASSMENT PREVENTION AND RESPONSE WORKSHOP
komlalokoe
 
PPTX
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPTX
How to Manage Promotions in Odoo 18 Sales
Celine George
 
PPTX
Capitol Doctoral Presentation -July 2025.pptx
CapitolTechU
 
PPTX
How to Configure Storno Accounting in Odoo 18 Accounting
Celine George
 
PPTX
How to Configure Access Rights of Manufacturing Orders in Odoo 18 Manufacturing
Celine George
 
PDF
BÀI TẬP BỔ TRỢ THEO LESSON TIẾNG ANH - I-LEARN SMART WORLD 7 - CẢ NĂM - CÓ ĐÁ...
Nguyen Thanh Tu Collection
 
PDF
1, 2, 3… E MAIS UM CICLO CHEGA AO FIM!.pdf
Colégio Santa Teresinha
 
PPTX
Accounting Skills Paper-I, Preparation of Vouchers
Dr. Sushil Bansode
 
PPTX
Views on Education of Indian Thinkers J.Krishnamurthy..pptx
ShrutiMahanta1
 
PDF
Zoology (Animal Physiology) practical Manual
raviralanaresh2
 
PPSX
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
PPTX
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
PPTX
Pyhton with Mysql to perform CRUD operations.pptx
Ramakrishna Reddy Bijjam
 
PPSX
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
PPTX
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
PPTX
How to Create Rental Orders in Odoo 18 Rental
Celine George
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
SCHOOL-BASED SEXUAL HARASSMENT PREVENTION AND RESPONSE WORKSHOP
komlalokoe
 
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
How to Manage Promotions in Odoo 18 Sales
Celine George
 
Capitol Doctoral Presentation -July 2025.pptx
CapitolTechU
 
How to Configure Storno Accounting in Odoo 18 Accounting
Celine George
 
How to Configure Access Rights of Manufacturing Orders in Odoo 18 Manufacturing
Celine George
 
BÀI TẬP BỔ TRỢ THEO LESSON TIẾNG ANH - I-LEARN SMART WORLD 7 - CẢ NĂM - CÓ ĐÁ...
Nguyen Thanh Tu Collection
 
1, 2, 3… E MAIS UM CICLO CHEGA AO FIM!.pdf
Colégio Santa Teresinha
 
Accounting Skills Paper-I, Preparation of Vouchers
Dr. Sushil Bansode
 
Views on Education of Indian Thinkers J.Krishnamurthy..pptx
ShrutiMahanta1
 
Zoology (Animal Physiology) practical Manual
raviralanaresh2
 
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
Pyhton with Mysql to perform CRUD operations.pptx
Ramakrishna Reddy Bijjam
 
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
How to Create Rental Orders in Odoo 18 Rental
Celine George
 
Ad

Why am I getting an out of memory error and no window of my .pdf

  • 1. Why am I getting an out of memory error and no window of my application is appearing? code starts here: package slidePuzzle; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.image.BufferedImage; import java.awt.image.CropImageFilter; import java.awt.image.FilteredImageSource; import java.util.ArrayList; import java.util.Collections; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class Game extends JFrame { // declare variables, arrays, and constants private Buttons emptyButton; private ArrayList<Buttons> buttons; private ArrayList<Point> answer; private final int TOTAL_BUTTONS = 12; private final int TOTAL_WIDTH = 400; private JPanel panel; private Image img; private BufferedImage src; private BufferedImage mod; private int width; private int height; // default constructor public Game() { // call initializer initializeInterface(); } private void initializeInterface() { ArrayList<Point> answer = new ArrayList<Point>(); ArrayList<Buttons> buttons = new ArrayList<Buttons>(); JPanel panel = new JPanel(); panel.setBorder(BorderFactory.createLineBorder(Color.black)); panel.setLayout(new GridLayout(4, 3, 0, 0));
  • 2. for (int x = 0; x < 4; x++) { for (int y = 0; y < 3; y++) { answer.add(new Point(x, y)); } } try { src = loadImg(); int height = modifyHeight(src.getWidth(), src.getHeight()); mod = resizeImg(src, TOTAL_WIDTH, height, BufferedImage.TYPE_INT_ARGB); } catch (IOException exception) { JOptionPane.showMessageDialog(this, "Image is unable to be rendered.", "Error", JOptionPane.ERROR_MESSAGE); } height = mod.getHeight(null); width = mod.getWidth(null); for (int x = 0; x < 4; x++) { for (int y = 0; y <3; y++) { img = createImage(new FilteredImageSource(mod.getSource(), new CropImageFilter(x * width / 3, y * height / 4, (width / 3), height / 4))); Buttons button = new Buttons(img); button.putClientProperty("position", new Point(x, y)); if (x == 3 && y == 2) { emptyButton = new Buttons(); emptyButton.setBorderPainted(false); emptyButton.setContentAreaFilled(false); emptyButton.setEmptyButton(); emptyButton.putClientProperty("position", new Point(x, y)); } else { buttons.add(button); } } } Collections.shuffle(buttons); buttons.add(emptyButton);
  • 3. for (int z = 0; z < TOTAL_BUTTONS; z++) { Buttons button = buttons.get(z); panel.add(button); button.setBorder(BorderFactory.createLineBorder(Color.black)); button.addActionListener(new Click()); } JFrame frame = new JFrame("Tile Slider"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new Game()); frame.pack(); frame.setVisible(true); frame.setLocationRelativeTo(null); frame.setResizable(false); } private BufferedImage loadImg() throws IOException { BufferedImage bufferedImage = ImageIO.read(new File("src/pond.jpg")); return bufferedImage; } private BufferedImage resizeImg(BufferedImage natural, int width, int height, int type) { BufferedImage adjustedImg = new BufferedImage(width, height, type); Graphics2D graphics = adjustedImg.createGraphics(); // draw image to fit inside our window and return graphics.drawImage(natural, 0, 0, width, height, null); graphics.dispose(); return adjustedImg; } private int modifyHeight(int width, int height) { double widthRatio = TOTAL_WIDTH / (double) width; int modHeight = (int) (height * widthRatio); return modHeight; } private class Click extends AbstractAction { @Override public void actionPerformed(ActionEvent event) { check(event); isCorrectConfiguration();
  • 4. } private void check(ActionEvent event) { int emptyButtonIndex = 0; for (Buttons btn: buttons) { if (btn.getEmptyButton()) { emptyButtonIndex = buttons.indexOf(btn); } } JButton btn = (JButton) event.getSource(); int buttonIndex = buttons.indexOf(btn); if ((buttonIndex + 1 == emptyButtonIndex) || (buttonIndex + 3 == emptyButtonIndex) || (buttonIndex - 1 == emptyButtonIndex) || (buttonIndex - 3 == emptyButtonIndex)) { Collections.swap(buttons, emptyButtonIndex, buttonIndex); update(); } } private void update() { panel.removeAll(); for (JComponent btn: buttons) { panel.add(btn); } panel.validate(); } } public void isCorrectConfiguration() { ArrayList<Point> userState = new ArrayList<Point>(); for (JComponent btn: buttons) { userState.add((Point) btn.getClientProperty("position")); } if (comparePuzzleState(answer, userState)) { JOptionPane.showMessageDialog(panel, "You have completed the puzzle!");
  • 5. } } public static boolean comparePuzzleState(ArrayList<Point> list1, ArrayList<Point> list2) { return list1.toString().contentEquals(list2.toString()); } public static void main(String[] args) { EventQueue.invokeLater(() -> { Game slidePuzzle = new Game(); slidePuzzle.setVisible(true); }); } }