2017-03-26 5 views
1

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:

enter image description here

Quand je me déplace ScrollPane:

enter image description here

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.

+0

S'il vous plaît créer et afficher un valide [mcve]. –

Répondre

2

Votre problème est que vous devez recalculer les points de départ à chaque fois que la peinture est terminée. Sinon, les variables continuent d'augmenter inutilement. Alors ajoutez deux lignes:

@Override 
protected void paintComponent(Graphics g) { // should be protected 
    super.paintComponent(g); 

    // need to re-initialize variables within this 
    startPointX = (dim.getWidth()/10) * 1; 
    startPointY = (dim.getHeight()/10) * 1; 

Pour votre information, à l'avenir, s'il vous plaît poster un MCVE avec votre question. Par exemple, voici le MCVE que je fait de votre code, code qui peut maintenant être copié, collé et dirigé par quelqu'un:

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import javax.swing.*; 

@SuppressWarnings("serial") 
public class Foo02 extends JPanel { 
    private DrawPanel mainPanel; 
    private Dimension dim = new Dimension(200, 200); 

    public Foo02() { 
     initComponents(); 
    } 

    public void initComponents() { 
     mainPanel = new DrawPanel(dim); 
     // !! this.getContentPane().setLayout(new GridBagLayout()); 
     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)); 
     jsp.setMinimumSize(new Dimension(dim.width, dim.height)); 
     jsp.setMaximumSize(new Dimension(dim.width, dim.height)); 
     add(jsp, gbc); 
     revalidate(); 
     repaint(); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(() -> createAndShowGui()); 
    } 

    private static void createAndShowGui() { 
     Foo02 mainPanel = new Foo02(); 
     JFrame frame = new JFrame("Foo02"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frame.add(mainPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 
} 

@SuppressWarnings("serial") 
class DrawPanel extends 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 
    protected void paintComponent(Graphics g) { // should be protected 
     super.paintComponent(g); 
     // need to re-initialize variables within this 
     startPointX = (dim.getWidth()/10) * 1; 
     startPointY = (dim.getHeight()/10) * 1; 

     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; 
      } 
     } 
    } 
} 
+0

Oh mon bon j'étais un vrai morron, j'ai totalement oublié ça !! Merci beaucoup, j'étais tellement concentré sur la méthode paintComponents que j'ai oublié de réinitialiser ces points! – nightslit

+0

@nightslit: de rien! Notez la modification pour répondre qui montre un [mcve] que j'ai créé pour aider à comprendre le problème. À l'avenir, vous voudrez inclure un MCVE valide avec votre question pour les rendre plus faciles à répondre. Bonne chance! –

+1

merci pour la correction, je vais le faire la prochaine fois. – nightslit