zdravim, mam taky problem. Snazim se udelat graficky joystick v jave. Vsechno v poradku jen je tu takovej detail kdyz menim pozici mysi. Po kazde zmene pozice volam repaint(). Ale i tak obcas na jPanelu zustavaji nejakou chvili vykreslene stare pozice.
package MainFrame;
public class NewJFrame extends javax.swing.JFrame {
public NewJFrame() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
controlPanel1 = new MainFrame.ControlPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("jLabel1");
jLabel2.setText("jLabel2");
controlPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
controlPanel1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
controlPanel1MouseReleased(evt);
}
});
controlPanel1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseDragged(java.awt.event.MouseEvent evt) {
controlPanel1MouseDragged(evt);
}
});
javax.swing.GroupLayout controlPanel1Layout = new javax.swing.GroupLayout(controlPanel1);
controlPanel1.setLayout(controlPanel1Layout);
controlPanel1Layout.setHorizontalGroup(
controlPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
controlPanel1Layout.setVerticalGroup(
controlPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, 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(controlPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(82, 82, 82)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2))
.addContainerGap(202, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(controlPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel2)))
.addContainerGap(256, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void controlPanel1MouseDragged(java.awt.event.MouseEvent evt) {
jLabel1.setText(Integer.toString(evt.getX()));
jLabel2.setText(Integer.toString(evt.getY()));
controlPanel1.setxPosition(evt.getX());
controlPanel1.setyPosition(evt.getY());
controlPanel1.repaint();
}
private void controlPanel1MouseReleased(java.awt.event.MouseEvent evt) {
controlPanel1.setxPosition(142);
controlPanel1.setyPosition(142);
jLabel1.setText(Integer.toString(150));
jLabel2.setText(Integer.toString(150));
controlPanel1.repaint();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private MainFrame.ControlPanel controlPanel1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
// End of variables declaration
}
a takhle trida je odvozena od jPanelu a vykresluje.
package MainFrame;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class ControlPanel extends JPanel {
public int xPosition = 142;
public int yPosition = 142;
public int getxPosition() {
return xPosition;
}
public void setxPosition(int xPosition) {
this.xPosition = xPosition;
}
public int getyPosition() {
return yPosition;
}
public void setyPosition(int yPosition) {
this.yPosition = yPosition;
}
public ControlPanel() {
this.setSize(300, 300);
this.setBackground(Color.black);
this.setVisible(true);
}
@Override
public void paint(Graphics g) {
g.setColor(Color.red);
g.drawLine(0, 150, 300, 150);
g.drawLine(150, 0, 150, 300);
g.fillOval(xPosition, yPosition, 16, 16);
}
}
Nevite nekdo proc to tam zustava obcas zasekle to vykresleni ? Popripade nejakou kritiku jestli je to takhle v poradku nebo je lepsi zvolit jinej zpusob ? Popripade co pridat a co dodat ? Napadlo me zkusit vlakno ale jeste sem snema nedelal tak nevim.