Pessoal, preciso saber a posição do mouse, alguem saberia me dizer como fazer em java?
para mover eu uso o Robot mousemove mas não tem como pegar a posição…
[]´s
Pessoal, preciso saber a posição do mouse, alguem saberia me dizer como fazer em java?
para mover eu uso o Robot mousemove mas não tem como pegar a posição…
[]´s
procura algo sobre MouseAdapter.
talvez o cod abaixo lhe ajude em algo:
eu não lembro como usa o MouseAdapter mas tá ai
// TITULO: FRACTAIS
// AUTOR: Luiz A. Prado - www.codigorapido.com.br
// PROGRAMA: Fractais Mandelbrot e Julia set
// DATA final: 14/06/2004
// OBS: imagens interessantes
// escala = 3.15085376E9
// x = -1.7692758988833777
// y = 0.0569369624080319
//
// n = 65
// -1.7692754341120358
// 0.05693747593566051
//
// n = 75
// -1.7692780827070487
// 0.05693994566806603
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.Event;
import java.io.*;
import javax.swing.event.*;
import java.awt.event.ComponentListener;
public class FractalMandelbrot extends JPanel
{
// desligado
// 0 =========== CORPO DE TESTE ================
public static void main (String args[])
{
JFrame k = new JFrame("3D-Java");
k.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
k.getContentPane().add(new FractalMandelbrot());
k.setBounds(20,20,700,475);
k.setVisible(true);
}
//=============================================
// 1 ======== CONSTRUTOR DA CLASSE ============
static paint_mandelbrot screen;
FractalMandelbrot()
{
screen = new paint_mandelbrot();
this.setLayout(new BorderLayout());
this.add(screen, BorderLayout.CENTER);
this.add(new aba_mandelbrot(), BorderLayout.EAST);
}
//=============================================
// 2 ======= PAINEL DIREITO ===================
static JSlider
jslider_mandelbrot_escala,
jslider_mandelbrot_angulo1,
jslider_mandelbrot_angulo2
;
static JTextField
jtextField_mandelbrot_escala,
jtextField_mandelbrot_endereco_x,
jtextField_mandelbrot_endereco_y,
jtextField_mandelbrot_angulo1,
jtextField_mandelbrot_angulo2,
jtextField_mandelbrot_k,
jtextField_mandelbrot_l
;
public static class aba_mandelbrot extends JPanel
{
aba_mandelbrot()
{
setLayout(new BorderLayout());
JPanel maior = new JPanel();
maior.setLayout(new GridLayout(4,1));
//***********************************
JPanel painel_mandelbrot1 = new JPanel();
painel_mandelbrot1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Escala = 1/(1.4)^n"));
painel_mandelbrot1.setLayout(new GridLayout(2,0));
jslider_mandelbrot_escala = new JSlider(0, 90, 0);
jslider_mandelbrot_escala.setMajorTickSpacing(20);
jslider_mandelbrot_escala.setMinorTickSpacing(5);
jslider_mandelbrot_escala.setFont(new Font("Serif", Font.PLAIN, 6));
jslider_mandelbrot_escala.setPaintLabels( true );
jslider_mandelbrot_escala.addChangeListener( new mandelbrot_ChangeListener() );
painel_mandelbrot1.add(jslider_mandelbrot_escala);
JLabel La1= new JLabel("n = ");
jtextField_mandelbrot_escala = new JTextField("0", 11);
JPanel l1 = new JPanel();
l1.add(La1);
l1.add(jtextField_mandelbrot_escala);
painel_mandelbrot1.add(l1);
//***********************************
JPanel painel_mandelbrot2 = new JPanel();
painel_mandelbrot2.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Proporção de x e y"));
painel_mandelbrot2.setLayout(new GridLayout(2,0));
jslider_mandelbrot_angulo1 = new JSlider(0, 90, 90);
jslider_mandelbrot_angulo1.setMajorTickSpacing(45);
jslider_mandelbrot_angulo1.setMinorTickSpacing(15);
jslider_mandelbrot_angulo1.setPaintLabels( true );
jslider_mandelbrot_angulo1.addChangeListener( new mandelbrot_ChangeListener() );
painel_mandelbrot2.add(jslider_mandelbrot_angulo1);
JLabel Langulo1= new JLabel("x = ");
jtextField_mandelbrot_angulo1 = new JTextField("0", 11);
JPanel Pangulo1 = new JPanel();
Pangulo1.add(Langulo1);
Pangulo1.add(jtextField_mandelbrot_angulo1);
painel_mandelbrot2.add(Pangulo1);
//***********************************
JPanel painel_mandelbrot3 = new JPanel();
painel_mandelbrot3.setLayout(new GridLayout(2,1));
painel_mandelbrot3.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Coordenadas"));
JLabel La2= new JLabel("x = ");
jtextField_mandelbrot_endereco_x = new JTextField("0", 15);
JPanel l2 = new JPanel();
l2.add(La2);
l2.add(jtextField_mandelbrot_endereco_x);
painel_mandelbrot3.add(l2);
JLabel La3= new JLabel("y = ");
jtextField_mandelbrot_endereco_y = new JTextField("0", 15);
JPanel l3 = new JPanel();
l3.add(La3);
l3.add(jtextField_mandelbrot_endereco_y);
painel_mandelbrot3.add(l3);
//***********************************
JPanel painel_mandelbrot4 = new JPanel();
painel_mandelbrot4.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "k e l"));
painel_mandelbrot4.setLayout(new GridLayout(2,0));
JLabel label_k= new JLabel("k = ");
jtextField_mandelbrot_k = new JTextField("0", 15);
JPanel panel_k = new JPanel();
panel_k.add(label_k);
panel_k.add(jtextField_mandelbrot_k);
painel_mandelbrot4.add(panel_k);
JLabel label_l= new JLabel("l = ");
jtextField_mandelbrot_l = new JTextField("0", 15);
JPanel panel_l = new JPanel();
panel_l.add(label_l);
panel_l.add(jtextField_mandelbrot_l);
painel_mandelbrot4.add(panel_l);
//***********************************
JButton botao_atualiza_mandelbrot = new JButton("Atualizar");
botao_atualiza_mandelbrot.addActionListener
(
new ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent ae)
{
clicado_posicao_x = Double.parseDouble( jtextField_mandelbrot_endereco_x.getText() );
clicado_posicao_y = Double.parseDouble( jtextField_mandelbrot_endereco_y.getText() );
mandelbrot_escala = Math.pow(1.4,Double.parseDouble( jtextField_mandelbrot_escala.getText() ) );
mandelbrot_angulo1 = Float.parseFloat( jtextField_mandelbrot_angulo1.getText());
propor_x = Double.parseDouble( jtextField_mandelbrot_k.getText() );
propor_y = Double.parseDouble( jtextField_mandelbrot_l.getText() );
screen.repaint();
}
}
);
maior.add(painel_mandelbrot2);
maior.add(painel_mandelbrot4);
maior.add(painel_mandelbrot1);
maior.add(painel_mandelbrot3);
add(maior, BorderLayout.CENTER);
botao_atualiza_mandelbrot.setToolTipText("Atualiza a imagem com os dados digitados");
add(botao_atualiza_mandelbrot, BorderLayout.SOUTH);
}
}
//=============================================
// 3 ====== ESCUTA SLIDERS ====================
static double propor_x=0, propor_y=0;
static float mandelbrot_angulo1 = 90;
public static class mandelbrot_ChangeListener implements ChangeListener
{
public void stateChanged( ChangeEvent event )
{
if( event.getSource() == jslider_mandelbrot_escala )
{
mandelbrot_escala = (float) Math.pow(1.4,jslider_mandelbrot_escala.getValue());
jtextField_mandelbrot_escala.setText(" " + jslider_mandelbrot_escala.getValue());
}
if( event.getSource() == jslider_mandelbrot_angulo1 )
{
mandelbrot_angulo1 = jslider_mandelbrot_angulo1.getValue();
jtextField_mandelbrot_angulo1.setText(" " + jslider_mandelbrot_angulo1.getValue());
}
screen.repaint();
}
}
//=============================================
// 4 ========= PAINEL DA IMAGEM ===============
static double clicado_posicao_x, clicado_posicao_y;
static double mandelbrot_escala = 1.0;
public static class paint_mandelbrot extends JPanel
{
//******** CONSTRUTOR DA IMAGEM **************
static int xi, xf, yi, yf;
paint_mandelbrot()
{
//************ CENTRALIZA ONDE CLICAR **********
addMouseListener
(
new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
clicado_posicao_x += (e.getX()-( qmor/2) )/(mandelbrot_escala*qmor/4) ;
clicado_posicao_y += (e.getY()-( qmor/2) )/(mandelbrot_escala*qmor/4) ;
jtextField_mandelbrot_endereco_x.setText(" " + clicado_posicao_x);
jtextField_mandelbrot_endereco_y.setText(" " + clicado_posicao_y);
screen.repaint();
}
}
);
//******** FIM CENTRALIZA ONDE CLICAR **********
}
//******** CORES DA IMAGEM **************
static int[][] Color_2;
static boolean cores = false;
public void arcoiris()
{
float n = 0;
float ang = (float) Math.PI/128;
cores = true;
Color_2 = new int[3][256];
for (int i=0 ; i < 256 ; i++)
{
Color_2[0][i] = (int) (255 * ( (Math.sin( n )+1)/2f ) );
Color_2[1][i] = (int) (255 * ( (Math.sin( n + Math.PI/3)+1)/2f ) );
Color_2[2][i] = (int) (255 * ( (Math.sin( n + 2*Math.PI/3)+1)/2f ) );
n += ang;
}
}
//******* FIM CORES DA IMAGEM ***********
//******** FRACTAL MANDELBROT ***********
static double qmor;
static float tamanho_da_tela_x;
static float tamanho_da_tela_y;
static float angl_x, angl_y;
public void paint(Graphics g)
{
angl_x=(float) (Math.PI/180)*mandelbrot_angulo1;
angl_y=(float) (Math.PI/180)*mandelbrot_angulo1;
super.paint(g);
if (cores == false) arcoiris(); // inicia as cores
tamanho_da_tela_x = getSize().width; //pega o tamnho x da tela
tamanho_da_tela_y = getSize().height; //pega o tamnho y da tela
double xmin = -2/mandelbrot_escala + clicado_posicao_x; //smallest real value (x-axis)
double xmax = 2/mandelbrot_escala + clicado_posicao_x; //largest real value (x-axis)
double ymin = -2/mandelbrot_escala + clicado_posicao_y; //smallest imaginary value (y-axis)
double ymax = 2/mandelbrot_escala + clicado_posicao_y; //largest imaginary value (y-axis)
int Max_interacoes = 255; //numero MAXIMO de interações
double novo_x=0,novo_y=0,velho_x=0,velho_y=0, x1=0, y1=0;
int m; //numero de intereções momentaneo
qmor = tamanho_da_tela_x;
if (qmor<tamanho_da_tela_y)
qmor = tamanho_da_tela_y;
double dx = (xmax-xmin)/qmor; //how much to add for each x-pixel?
double dy = (ymax-ymin)/qmor; //how much to add for each y-pixel?
int pixeis_coluna_x = 1;
int pixeis_linha_y = 1;
double x; //Variable storing current x-value
double y = ymin;
for(pixeis_linha_y = 1; pixeis_linha_y<tamanho_da_tela_y; pixeis_linha_y++)
{
x = xmin;
y1 = y * (float)Math.sin(angl_y);
for (pixeis_coluna_x=1; pixeis_coluna_x<tamanho_da_tela_x; pixeis_coluna_x++)
{
velho_x = x;
velho_y = y;
m = 0;
x1 = x * (float) Math.sin(angl_x);
do
{
novo_x = velho_x*velho_x - velho_y*velho_y + x1 + propor_x;
novo_y = 2*velho_x*velho_y + y1 + propor_y;
velho_x = novo_x;
velho_y = novo_y;
m++;
} while (((novo_x*novo_x+novo_y*novo_y)<4) && (m<Max_interacoes));
if(m>=Max_interacoes)
{
g.setColor(new Color(0, 0, 0));
}
else
{
while(m>255)
{
m-=255;
}
g.setColor(new Color(Color_2[0][m], Color_2[1][m], Color_2[2][m]));
}
g.drawLine(pixeis_coluna_x,pixeis_linha_y,pixeis_coluna_x,pixeis_linha_y);
x+=dx;
}
y+=dy;
}
//********** Dados e nome da empresa **********
g.setColor(new Color(255, 255, 255));
g.fillRect(7,0,(int)tamanho_da_tela_x-7,33);
g.setColor(new Color(0, 0, 0));
g.setFont(new Font("Arial", Font.BOLD, 9));
g.drawString("Escala: "+mandelbrot_escala+ "x",15,12);
g.setFont(new Font("Arial", Font.BOLD, 9));
g.drawString("x ="+clicado_posicao_x,15,22);
g.drawString("y ="+clicado_posicao_y,15,32);
g.setColor(new Color(255, 0, 0));
g.setFont(new Font("Arial", Font.BOLD, 12));
g.drawString("www.actionbannersdesigners.com",(int)tamanho_da_tela_x-290,13);
//***** FIM Dados e nome da empresa **********
}
//****** FIM FRACTAL MANDELBROT *********
}
//======== FIM PAINEL DA IMAGEM ===============
}
Pow eu consegui Fazer um com o mousemove
private void jLabel1MouseMoved(java.awt.event.MouseEvent evt) {
int x=evt.getX();
int y=evt.getY();
jLabel2.setText("x:"+x+" y:"+y);
}
Onde jLabel1 é uma imagem que eu uso e ele captura as coordenadas de onde o mouse está passando e joga no jLabel2 que fica do lado
^^
Abçs.