Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4543190
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T15:29:48+00:00 2026-05-21T15:29:48+00:00

I am trying to get this program to show two pictures and check if

  • 0

I am trying to get this program to show two pictures and check if they are the same picture, i am having trouble getting bildeSjekk() to do this, it shows all pictures and if you double click a picture it removes it, first i need to store the previous instance of the int i, then when teller should become two when two pictures have been revealed, and then i will use current int i and int temp in the int array index and check if the value is the same. It’s a Picture memory game.

package prosjekt_1139;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.net.URL;


import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class Hukommelse extends JPanel implements MouseListener, ActionListener{

//private JLabel[] kort = new JLabel[16];
private JButton nyOmgang = new JButton("Del ut kortene");
private JButton tilbake = new JButton("Tilbake");
private HovedVinduet vindu;
private int[] index = new int[16];
private int teller =0, temp = 0;
private Image img;
private Image[] imgarray;
private Rectangle[] bokser;
private Point point1;
private URL path1, path2[]= new URL[8];
private boolean sjekk[] = new boolean[16];

public Hukommelse(HovedVinduet vindu) throws IOException{
    this.vindu = vindu;

    bokser = new Rectangle[16];
    imgarray = new Image[8];
    point1 = new Point();

    img = null; 
    setBackground(Color.GREEN);
    setPreferredSize(new Dimension(720,690));
    setLocation(0,0);
    nyOmgang.addActionListener(this);
    tilbake.addActionListener(this);
    add(nyOmgang);
    add(tilbake);
    this.addMouseListener(this);
    boks();
}

//  this is my randomisere metode
public void  kortIndex(){
    int temp;

    for (int i = 0;i<index.length;i++){
        index[i] = i/2;
        //System.out.println(index[i]);
    }

    for (int i=0;i<1000;i++){
        int index1 = (int)(Math.random()*16);
        int index2 = (int)(Math.random()*16);
        temp = index[index1];
        index[index1] = index[index2];
        index[index2] = temp;
    }
    //      for (int i = 0; i<index.length;i++)
    //          System.out.print(index[i]+"\t");
    //      System.out.println();
}

public void paintComponent(Graphics g){
    super.paintComponents(g);
    g.setColor(Color.green);

    int j = 0;
    int k = 0;
    for (int i = 0; i<16;i++){
        g.drawImage(img, 20+(k*175), 50+(j*160), 150, 150, this);
        k++;
        if(i == 3 || i == 7 || i == 11 || i == 15){
            j++;
            k = 0;
        }
    }

    for (int i=0; i<bokser.length; i++) {
        if(sjekk[i]){
            g.drawImage(imgarray[index[i]], bokser[i].x, bokser[i].y, bokser[i].width, bokser[i].height, this);
        }
    }
}

//Metode For checking if the image is clicked on
public void bildeSjekk(){

    for (int i = 0;i<bokser.length;i++){
        if(bokser[i].contains(point1)){
            sjekk[i] = true;
            teller++;
            temp = i;
        }
        if(teller >= 2 ){
            sjekk[i] = false;
            sjekk[temp] = false;
            teller = 0;
        }
    }
}

public void boks(){
    int j = 0;
    int k = 0;
    for(int i = 0; i <bokser.length; i++){
        bokser[i] = new Rectangle(20+(j*175), 50+(k*160), 150, 150);
        j++;
        if(i == 3 || i == 7 || i == 11 || i == 15){
            j =0;
            k++;
        }
    }
}

public void bilder() throws IOException{
    img = ImageIO.read(new File("Image/grass.jpg"));

    //repaint();

    imgarray[0] = ImageIO.read(new File("Image/bekk.jpg")); 
    imgarray[1] = ImageIO.read(new File("Image/solnedgang.jpg"));
    imgarray[2] = ImageIO.read(new File("Image/tåge.jpg"));
    imgarray[3] = ImageIO.read(new File("Image/vile.jpg"));
    imgarray[4] = ImageIO.read(new File("Image/fuglekasse.jpg"));
    imgarray[5] = ImageIO.read(new File("Image/gullfugl.jpg"));
    imgarray[6] = ImageIO.read(new File("Image/byen.jpg"));
    imgarray[7] = ImageIO.read(new File("Image/bekk.jpg"));
}


@Override
public void mouseClicked(MouseEvent agr0) {
    // TODO Auto-generated method stub
}

@Override
public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub
}

@Override
public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub
}

@Override
public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub
    System.out.println(e.getX()+"\t"+e.getY());
    point1 = e.getPoint();

    bildeSjekk();
    repaint();
}

@Override
public void mouseReleased(MouseEvent arg0) {
    // TODO Auto-generated method stub
}

@Override
public void actionPerformed(ActionEvent e) {
    if(e.getActionCommand().equals("Del ut kortene")){  
        try {
            bilder();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        point1 = new Point(0,0);
        for (int i = 0;i<bokser.length;i++){
            sjekk[i] = false;
        }
        teller = 0;
        kortIndex();
        repaint();
    }
    if(e.getSource() == tilbake){
        vindu.setMenyPanelAktivt();
        vindu.setSize(800, 600);
        vindu.setLocation(0,0);
    }
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T15:29:49+00:00Added an answer on May 21, 2026 at 3:29 pm

    You might like this related memory game that uses JToggleBUtton and Unicode glyphs instead of pictures.

    Addendum: As an aside, you may get more helpful answers if you prepare an sscce that doesn’t depend on a large number of inaccessible images. As an example, RotatableImage is a simple, static class that can be adapted as required.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to write a program which get two 6-digit decimal numbers and show
i'm trying to get this program to break down a user defined amount of
I'm trying to get this simple program to work on windows, but it crashes:
Trying to get this to work, with no luck: [DataMember] public Type ParameterType {
Im trying to get this working but for some reason it's just not right.
Been trying to get this up and running for a while now. Basically i
I'm trying to get this sitemap clas s working. It appears to use LINQ,
I'm trying to get this css layout to work with IE7 and I'm a
I've been trying to get this module to work and no matter what I've
I am trying to get this awesome JQuery plugin working with SQL Server. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.