Muy buenas señores de HackHispano, en esta ocasion estoy aquí para resolver una duda que tengo. La vez pasada solucione un problema dl graphics con un bufferstrategy. El punto es q ahora, los jlabel jbutton y demas componentes no se visualizan cuando hago createBufferStrategy(2); asi que quisiera saber si alguno de uds tiene alguna idea para solucionar el muestreo de dichos componenentes. Gracias de antemano por sus respuestas
podría ser que estas re implementando algún método paint, y te falta invocar a super.paint( g ) ? para dibujar el control normalmente y luego dibujar tus elementos personalizados ?
si no es asi, podrias dar mas detalles ? codigo ?
buenas rudygt, antes que nada, gracias por la ayuda...a ver, posteare mi codigo de ejemplo:
public class Frame01 extends javax.swing.JFrame {
private final BufferStrategy bf;
/** Creates new form Frame01 */
public Frame01() {
initComponents();
createBufferStrategy(2);
bf=this.getBufferStrategy();
// this.setResizable(false);
}
@Override
public void paint(Graphics g)
{
Graphics2D g2 = null;
try {
super.paint(g); //obtenemos uno de los buffers para dibujar
g2 = (Graphics2D) bf.getDrawGraphics();
paint(g2);
} finally
{
g2.dispose();
}
//pintamos el buffer en pantalla
bf.show();
}
public void paint(Graphics2D g2)
{
// g2.setColor(Color.red);
// g2.drawLine(WIDTH, WIDTH, WIDTH, WIDTH);
int x1=15,x2=60;
int y1=40,y2=60;
g2.setColor(Color.BLUE);
for(int i=0;i<5;i++)
{
g2.drawLine(x1, y1, x2, y2);
y1=y1+20;
g2.drawLine(x2, y2, x1, y1);
y2=y2+20;
}
}
/** 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() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jToggleButton1 = new javax.swing.JToggleButton();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jSlider1 = new javax.swing.JSlider();
setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
jLabel1.setText("jLabel1");
jButton1.setText("jButton1");
jToggleButton1.setText("jToggleButton1");
jLabel2.setText("jLabel2");
jLabel3.setText("jLabel3");
jLabel4.setText("jLabel4");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jToggleButton1)
.addGap(101, 101, 101)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED, 159, Short.MAX_VALUE)
.addComponent(jLabel4)
.addGap(76, 76, 76))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, jPanel1Layout.createSequentialGroup()
.addComponent(jLabel3)
.addGap(117, 117, 117)
.addComponent(jButton1)
.addGap(124, 124, 124))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, jPanel1Layout.createSequentialGroup()
.addComponent(jLabel2)
.addGap(191, 191, 191))))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(99, 99, 99)
.addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(220, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(69, 69, 69)
.addComponent(jLabel2)
.addGap(18, 18, 18)
.addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, jPanel1Layout.createSequentialGroup()
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED, 64, Short.MAX_VALUE)
.addComponent(jLabel3)
.addGap(90, 90, 90))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.TRAILING)
.addComponent(jToggleButton1)
.addComponent(jLabel1))
.addGap(34, 34, 34)
.addComponent(jButton1)
.addContainerGap(102, Short.MAX_VALUE))))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Frame01().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPanel jPanel1;
private javax.swing.JSlider jSlider1;
private javax.swing.JToggleButton jToggleButton1;
// End of variables declaration
}
inclui tambien el codigo generado por el netbeans. bueno, si use el super.paint(g); pero sigue saliendo lo mismo. agradecere mucho su respuesta.
ya encontré el problema
cuando usas en el metodo paint, el super( g ) lo estas haciendo en el buffer viejo
asi deberías de hacerlo
g2 = (Graphics2D) bf.getDrawGraphics();
super.paint(g2);
paint(g2);
porque
al final del metodo paint, cuando le das bf.show(); lo que habia dibujado en el frame se borra ( cuando llamas a super.paint(g) eso se borra al darle bf.show)
porque estas cambiando el buffer que esta en pantalla
saludos !
Muchas gracias por la respuesta, ya vi el error gracias a ti rudygt, funciona perfectamente. No me habia percatado de lo del buffer. Gracias nuevamente.
de nada, saludos y éxitos. en mi blog tengo algunas animaciones java 2d, tal vez te puedan servir también rdebug.blogspot.com (http://rdebug.blogspot.com)