Bom dia!
Senhores, estou fazendo um aplicativo que ao clicar num botão mostra um desenho no jPanel, porém acontece o erro abaixo descrito:
[i]Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException: javax.swing.JPanel cannot be cast to [/i]
Aqui a minha classe principal:
public class plote extends javax.swing.JFrame {
/** Creates new form plote */
public plote() {
initComponents();
}
@SuppressWarnings("unchecked")
>
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
((desenha)this.jPanel4).desenhaLinha(150, 5);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new plote().setVisible(true);
}
});
}
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
}
Aqui a classe que chamo quando clico no botão jButton1ActionPerformed
import java.awt.Font;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
/**
*
* @author Leandro
*/
public class desenha extends JPanel{
public desenha() {
this.setFocusable(true);
this.setBackground(Color.GRAY);
}
public void paint(Graphics g){
super.paint(g);
//desenhaLinha();
}
public void desenhaLinha(int x, int y) {
Graphics g2 = (Graphics) this.getGraphics();
//Graphics g = null;
int xi = x, yi = y, xf = xi + 100, yf = yi + 100;
g2.setColor(Color.black);
g2.drawLine(xi, yi, xf, yf); //Desenha uma linha entre o ponto (Xi,Yi) e o ponto (Xf , Yf)
g2.setColor(Color.black);
Font serif = new Font("Serif", Font.BOLD, 18);
g2.setFont(serif);
//g.drawString("drawLine", xi+10, yi+20);
}
}
[i]Mais uma coisa: não posso usar a classe Graphics G2D.[/i]
Alguém sabe onde está o problema?
Obrigado.