Je suis coincé sur quelque chose d'étrange. J'ai un JFrame avec un JScrollPane contenant un jPanel beaucoup plus grand que l'écran actuel. Je dessine des carrés dans les colonnes et je veux que ces carrés dépassent la bordure droite du jPanel. (Pour qu'ils apparaissent lorsque vous faites défiler vers la droite.) Mais les carrés peints avec la méthode paintComponents s'arrêtent juste au ViewPort visible du JScrollPane.Dessin avec paintComponent dans JScrollPanel
Voici mon code pour le JScrollPane intérieur du JFrame:
initComponents public void() {
mainPanel = new DrawPanel(dim);
this.getContentPane().setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weighty = 1;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.BOTH;
JScrollPane jsp = new JScrollPane(mainPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jsp.setLayout(new ScrollPaneLayout());
jsp.setViewportView(mainPanel);
jsp.getVerticalScrollBar().setUnitIncrement(20);
jsp.setBorder(BorderFactory.createEmptyBorder());
jsp.setPreferredSize(new Dimension(dim.width,dim.height -taskBarSize));
jsp.setMinimumSize(new Dimension(dim.width,dim.height -taskBarSize));
jsp.setMaximumSize(new Dimension(dim.width,dim.height -taskBarSize));
this.getContentPane().add(jsp, gbc);
this.getContentPane().revalidate();
this.getContentPane().repaint();
}
Et voici ma classe JPanel:
DrawPanel public class étend JPanel {
private Dimension dim;
private Integer numberPanels = 7;
private Double startPointX;
private Double startPointY;
private Double heightRow;
private Double heightPanel;
public DrawPanel(Dimension d) {
this.dim = d;
//this.setBackground(Color.BLACK);
calculateStartPoint();
}
public void calculateStartPoint() {
startPointX = (dim.getWidth()/10) * 1;
startPointY = (dim.getHeight()/10) * 1;
heightRow = (dim.getHeight() * 0.8)/numberPanels;
heightPanel = heightRow - 10;
double colums = 366/numberPanels;
this.setPreferredSize(new Dimension((int)(heightRow *((int)colums + 1)), dim.height));
this.setMinimumSize(new Dimension((int)(heightRow *((int)colums + 1)), dim.height));
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.GRAY);
for (int i = 1; i <= 366; i++) {
// Si c'est le dernier d'une colonne
if (i%numberPanels == 0 && i != 0) {
g.fillRect(startPointX.intValue(), startPointY.intValue(), heightPanel.intValue(),
heightPanel.intValue());
startPointX = startPointX + heightRow;
startPointY = startPointY - ((numberPanels -1) * heightRow);
// Si c'est encore dans la meme colonne
} else {
g.fillRect(startPointX.intValue(), startPointY.intValue(), heightPanel.intValue(),
heightPanel.intValue());
startPointY = startPointY + heightRow;
}
}
}
}
Au démarrage:
Quand je me déplace ScrollPane:
En outre, sur le redimensionnement disapreas de tout. Je devais aussi voir que lorsque je revenais en arrière, les carrés déjà peints disparaissaient, comme si tout ce qui sortait de l'écran disparaissait.
Merci à tous ceux qui ont un peu de temps pour cela.
S'il vous plaît créer et afficher un valide [mcve]. –