javaのことで
---MainPanel.java---
import java.awt.Dimension;
import java.awt.Graphics;
import java.event.MouseEvent;
import java.event.MouseListener;
import javax.swing.JPanel;
import java.util.Random;
public class MainPanel extends JPanel implements MouseListener {
public static final int WIDTH = 640;
public static final int HEIGHT = 480;
private static final int NUM_FILE = 4;
private File[] file;
private int prev;
public MainPanel() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
file = new File[NUM_FILE];
for (int i = 0; i < NUM_FILE; i++) {
file[i] = new File(i, this);
}
select();
addMouseListener(this);
}
private void clear() {
for (int i = 0; i < NUM_FILE; i++) {
file[i].delete();
}
}
private boolean check(int a, int b) {
return a == b || a == prev || b == prev;
}
private void select() {
Random rand = new Random();
int a, b;
do {
a = rand.nextInt(NUM_FILE);
b = rand.nextInt(NUM_FILE);
} while (check(a, b));
file[a].set(0, 0);
file[b].set(file[a].getX() + file[a].getWith() + File.SPACE, 0);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < NUM_FILE; i++) {
if (file[i].isSelected()) {
file[i].draw(g);
}
g.drawString("ファイル" + (i + 1) + " … " + file[i].getCnt(), 0, HEIGHT * 3 / 4 + i * 15);
}
}
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
for (int i = 0; i < NUM_FILE; i++) {
if (file[i].isSelected()) { // 表示されていて
// 画像内なら
if (x > file[i].getX() && x < file[i].getX() + file[i].getWidth() &&
y < file[i].getY() && y > file[i].getY() + file[i].getHeight()) {
file[i].count();
prev = i;
clear();
select();
break;
}
}
}
repaint();
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
フレームを用意してMainPanelを付加し,pack();してください.
補足
---MainPanel.java--- import java.awt.Dimension; import java.awt.Graphics; import java.event.MouseEvent; import java.event.MouseListener; import javax.swing.JPanel; import java.util.Random; public class MainPanel extends JPanel implements MouseListener { public static final int WIDTH = 640; public static final int HEIGHT = 480; private static final int NUM_FILE = 4; private File[] file; private int prev; public MainPanel() { setPreferredSize(new Dimension(WIDTH, HEIGHT)); file = new File[NUM_FILE]; for (int i = 0; i < NUM_FILE; i++) { file[i] = new File(i, this); } select(); addMouseListener(this); } private void clear() { for (int i = 0; i < NUM_FILE; i++) { file[i].delete(); } } private boolean check(int a, int b) { return a == b || a == prev || b == prev; } private void select() { Random rand = new Random(); int a, b; do { a = rand.nextInt(NUM_FILE); b = rand.nextInt(NUM_FILE); } while (check(a, b)); file[a].set(0, 0); file[b].set(file[a].getX() + file[a].getWith() + File.SPACE, 0); } public void paintComponent(Graphics g) { super.paintComponent(g); for (int i = 0; i < NUM_FILE; i++) { if (file[i].isSelected()) { file[i].draw(g); } g.drawString("ファイル" + (i + 1) + " … " + file[i].getCnt(), 0, HEIGHT * 3 / 4 + i * 15); } } public void mouseClicked(MouseEvent e) { int x = e.getX(); int y = e.getY(); for (int i = 0; i < NUM_FILE; i++) { if (file[i].isSelected()) { // 表示されていて // 画像内なら if (x > file[i].getX() && x < file[i].getX() + file[i].getWidth() && y < file[i].getY() && y > file[i].getY() + file[i].getHeight()) { file[i].count(); prev = i; clear(); select(); break; } } } repaint(); } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } } と