Zdravim... mam tu taky problem a neviem ako dalej. snazim sa vykreslit ciaru od suradnice nad ktorou boli stlacene tlacitko mysi, k suradnici nad ktorou bolo pustene:
tu je zdrojak:
Kód:
import java .awt . BasicStroke ; // typ ciary
import java .awt . Color ;
import java .awt . Font ;
import java .awt . Graphics ; // zakl . grafika
import java .awt . Graphics2D ; // Java2D
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax . swing . JPanel ;
public class Panel extends JPanel {
int xStart;
int yStart;
int xEnd;
int yEnd;
Graphics2D g2;
MouseListener mListen = new MouseListener() {
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
xStart = e.getX();
yStart = e.getY();
System.out.println(xStart+":"+yStart);
}
public void mouseReleased(MouseEvent e) {
xEnd = e.getX();
yEnd = e.getY();
System.out.println(xEnd+":"+yEnd);
paint();
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
};
public void paint(){
g2. setFont (new Font (" Arial ", Font.BOLD , 14) );
g2. setStroke (new BasicStroke (2, BasicStroke.CAP_SQUARE , BasicStroke.JOIN_MITER));
g2. setColor ( Color.RED );
g2.drawLine(xStart,yStart,xEnd,yEnd);
}
@Override
public void paintComponent ( Graphics g) {
super.paintComponent (g);
g2 = ( Graphics2D ) g;
this.addMouseListener(mListen);
g2. setFont (new Font (" Arial ", Font.BOLD , 14) );
g2. setStroke (new BasicStroke (2, BasicStroke.CAP_SQUARE , BasicStroke.JOIN_MITER));
g2. setColor ( Color.black );
g2. drawRect (10 , 10 , this.getWidth()-20 , this.getHeight()-20);
paint();
}
}
ked to spustim vykresli sa cierny ram z metody paintComponent ( Graphics g), a po kliknuti a odkliknuti mi vypise do konzoly suradnice ako je to v mouseListenery, zavola sa mi aj metoda paint(), ale vyslednu ciaru mi to nenakresli...