Galera, e o seguinte, eu tenho um trab de sistemas multimidia, e eu sugeri para o professor que fizer um editor de imagem bem simples
então é o seguninte:
Tenho a classe imagem, que nada mais é que um jlabel com muitas ações!
package br.ufpa.linc.telas;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
public class Imagem extends JLabel{
/**
*
*/
private static final long serialVersionUID = -3001651125896781505L;
private Imagem imagem = null;
private GUI gui = null;
private int x1, y1, x2, y2, raio, xc, yc;
private JLabel selecaoQuadrado = null;
public Imagem(){
super();
}
public Imagem(GUI gui){
super();
this.gui = gui;
getImagem();
}
private void getImagem(){
imagem = this;
imagem.addMouseListener(new MouseAdapter() {
public void mousePressed( MouseEvent evt) {
if ( gui.getModeloSelecao().equals("quadrado") ) selecaoQuadradoInicio( evt.getX(), evt.getY() );
if ( gui.getModeloSelecao().equals("circulo") ) selecaoCirculoInicio( evt.getX(), evt.getY() );
}
});
imagem.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent evt) {
if ( gui.getModeloSelecao().equals("quadrado") ) selecaoQuadradoFim( evt.getX(), evt.getY() );
if ( gui.getModeloSelecao().equals("circulo") ) selecaoCirculoFim( evt.getX(), evt.getY() );
}
});
}
private void selecaoQuadradoInicio( int a, int b ) {
selecaoQuadrado = new JLabel();
if ( gui.isPreenchido() ) {
selecaoQuadrado.setOpaque(true);
selecaoQuadrado.setBackground( gui.getOpcoes().getCorAtual().getBackground() );
}
selecaoQuadrado.setBorder(BorderFactory.createLineBorder( gui.getOpcoes().getCorAtual().getBackground(), gui.getEspessura()));
selecaoQuadrado.setBounds(0, 0, 80, 80);
x1 = a;
y1 = b;
if (x1 > getWidth()) x1 = getWidth();
else if (x1 < 0) x1 = 0;
if (y1 > getHeight()) y1 = getHeight();
else if (y1 < 0) y1 = 0;
}
private void selecaoQuadradoFim( int x, int y){
x2 = x;
y2 = y;
if (x2 > getWidth())
x2 = getWidth();
else if (x2 < 0)
x2 = 1;
if (y2 > getHeight())
y2 = getHeight();
else if (y2 < 0)
y2 = 1;
if ( x1 < x2 && y1 < y2 ) selecaoQuadrado.setBounds(x1, y1, x2-x1, y2-y1);
else if ( x1 > x2 && y1 < y2 ) selecaoQuadrado.setBounds(x2, y1, x1-x2, y2-y1);
else if ( x1 < x2 && y1 > y2 ) selecaoQuadrado.setBounds(x1, y2, x2-x1, y1-y2);
else if ( x1 > x2 && y1 > y2 ) selecaoQuadrado.setBounds(x2, y2, x1-x2, y1-y2);
add( selecaoQuadrado );
validate();
repaint();
}
private void selecaoCirculoInicio(int x, int y){
x1 = x;
y1 = y;
if ( gui.isPreenchido() ) {
// selecaoQuadrado.setBackground( gui.getOpcoes().getCorAtual().getBackground() );
}
// selecaoQuadrado.setBorder(BorderFactory.createLineBorder( gui.getOpcoes().getCorAtual().getBackground(), gui.getEspessura()));
// selecaoQuadrado.setBounds(0, 0, 80, 80);
if (x1 > getWidth()) x1 = getWidth();
else if (x1 < 0) x1 = 0;
if (y1 > getHeight()) y1 = getHeight();
else if (y1 < 0) y1 = 0;
}
private void selecaoCirculoFim( int x, int y){
x2 = x;
y2 = y;
if (x2 > getWidth())
x2 = getWidth();
else if (x2 < 0)
x2 = 1;
if (y2 > getHeight())
y2 = getHeight();
else if (y2 < 0)
y2 = 1;
if ( x1 < x2 && y1 < y2 ) {
raio = (int) (Math.sqrt( Math.pow(( x2 - x1 ), 2) + Math.pow(( y2 - y1 ), 2) ))/2;
xc = ( x1 + x2 ) / 2;
yc = ( y1 + y2 ) / 2;
}
else if ( x1 > x2 && y1 < y2 ) {
raio = (int) (Math.sqrt( Math.pow(( y1 - x2), 2) + Math.pow(( y2-y1 - x1-x2 ), 2) ))/2;
xc = ( x2 + y1 ) / 2;
yc = ( x1-x2 + y2-y1 ) / 2;
}
else if ( x1 < x2 && y1 > y2 ) {
raio = (int) (Math.sqrt( Math.pow(( y2 - x1 ), 2) + Math.pow(( y1-y2 - x2-x1 ), 2) ))/2;
xc = ( x1 + y2 ) / 2;
yc = ( x2-x1 + y1-y2 ) / 2;
}
else if ( x1 > x2 && y1 > y2 ) {
raio = (int) (Math.sqrt( Math.pow(( y2 - x2 ), 2) + Math.pow(( y1-y2 - x1-x2 ), 2) ))/2;
xc = ( x2 + y2 ) / 2;
yc = ( x1-x2 + y1-y2 ) / 2;
}
System.out.println("\n\n\nXc: " + xc + "\nYc: " + yc +"\nR: "+ raio );
getGraphics().fillArc(x1, y1, x2-x1, y2-y1, 140, -270);
}
}
Então, quando na interface eu clico no botao referente a um quadrado, funciona que é uma beleza, pois é so desenhar um JLABEL (métodos selecaoQuadradoInicio e selecaoQuadradoFim )
Basta que o usuário clique e arraste o mouse, (métodos mouse pressed e mouseDragged ) no mouseDragged eu fico alterando o tamanho do JLABEL e repintando, desta forma funciona a "seleção" em forma de quadrado.
Alguém tem uma idéia de como eu possa fazer a mesma coisa so que com um circulo??