Witam, mam taki problem, wziąłem się za pisanie gry Warcaby i staram się małymi krokami zrealizować swój cel, jestem na etapie rysowania planszy oraz pionków, dodatkowo zrobiłem takie coś, że gdy klikniemy na dany pionek to jego pole się podświetla i problem polega na tym, że gdy zbyt szybko klikamy na pionki to albo ich nie zaznacza albo robi to bardzo wolno.
Oto kod:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package warcaby;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
/**
*
* @author Rafal
*/
public class Warcaby extends javax.swing.JFrame implements MouseListener {
int xLocation, yLocation, xRysowanie, yRysowanie;
int rozmPola = 40;
int[][] plansza = new int[][]{
{0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0}};
int[][] pionki = new int[][]{
{0, 2, 0, 2, 0, 2, 0, 2},
{2, 0, 2, 0, 2, 0, 2, 0},
{0, 2, 0, 2, 0, 2, 0, 2},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{1, 0, 1, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0}};
/**
* Creates new form Warcaby
*/
public Warcaby() {
this.setLocation(200, 200);
initComponents();
addMouseListener(this);
}
public void paint(Graphics g) {
xLocation = 40;
yLocation = 50;
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
if (xLocation < x_mouse && (xLocation + rozmPola) > x_mouse && yLocation < y_mouse && (yLocation + rozmPola) > y_mouse && plansza[x][y] == 1 && pionki[y][x] != 0) {
g.setColor(Color.getHSBColor(300, 400, 200));
} else {
if (plansza[x][y] == 1) {
g.setColor(Color.getHSBColor(0, 0, 0));
} else if (plansza[x][y] == 0) {
g.setColor(Color.getHSBColor(200, 200, 200));
}
}
g.fillRect(xLocation, yLocation, rozmPola, rozmPola);
yLocation = yLocation + rozmPola;
}
xLocation = xLocation + rozmPola;
yLocation = 50;
}
xLocation = 40;
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
if (pionki[y][x] == 1) {
g.setColor(Color.getHSBColor(100, 100, 100));
} else if (pionki[y][x] == 2) {
g.setColor(Color.getHSBColor(250, 250, 250));
}
if (pionki[y][x] != 0) {
g.fillOval(xLocation, yLocation, rozmPola, rozmPola);
}
yLocation = yLocation + rozmPola;
}
xLocation = xLocation + rozmPola;
yLocation = 50;
}
x_mouse = 0;
y_mouse = 0;
}
public void update(Graphics g)
{
paint(g);
}
int x_mouse = 0, y_mouse = 0;
public void przesowanie() {
}
@Override
public void mouseClicked(MouseEvent ev) {
x_mouse = ev.getX();
y_mouse = ev.getY();
repaint();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setPreferredSize(new java.awt.Dimension(400, 400));
setResizable(false);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Warcaby.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Warcaby.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Warcaby.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Warcaby.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Warcaby().setVisible(true);
}
});
}
@Override
public void mousePressed(MouseEvent me) {
}
@Override
public void mouseReleased(MouseEvent me) {
}
@Override
public void mouseEntered(MouseEvent me) {
}
@Override
public void mouseExited(MouseEvent me) {
}
}
// Variables declaration - do not modify
// End of variables declaration
Czy ktoś byłby mi wstanie wytłumaczyć dlaczego tak się dzieje i dać jakieś wskazówki jak zrobić to aby działo się to odpowiednio szybko? Dodam, że nie jestem jakimś wirtuozem programowania i zajmuję się tym od niedawna.