我试图让这个程序显示两张图片并检查它们是否是相同的图片,我无法让bildeSjekk()执行此操作,它会显示所有图片,如果您双击图片则会将其删除,首先我需要存储int i的前一个实例,然后当两个图片被显示时出纳员应该变成两个,然后我将在int数组索引中使用当前的int i和int temp并检查值是否相同。这是一个图片记忆游戏。
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);
}
}
}
答案 0 :(得分:2)
您可能会喜欢使用JToggleBUtton
和memory game字形代替照片的相关Unicode。
附录:另外,如果您准备一个不依赖于大量无法访问的图像的sscce,您可能会获得更多有用的答案。例如,RotatableImage
是一个简单的静态类,可以根据需要进行调整。