JScrollPane und JPanel Problem

hi,

ich hät da mal ein problem.
Und zwar hab ich einen JPanel in einem JScrollPane. Und das alles mit Matisse gemacht. Ich hole mir mit getGraphics den Grafikkontext von dem JPanel und zeichne in selbiges. Soweit so gut. Er zeichnet. Wenn ich dann aber aus dem Bereich raus scrolle in dem die Zeichnung ist und den Teil der nicht zu sehen war sehen will(weil er nicht darauf passte) ist da nichts. Scrolle ich wieder zurück in den Bereich der am Anfang zu sehen war ist dieser auch weg.

public void malen(JPanel K)
{

Dimension d=new Dimension();
Graphics g=K.getGraphics();
d.width=35;
d.height=35;
MalRoutinen MR=new MalRoutinen(d,g);
PointEx [] Felder1=RandPkt.getPntField();
int Cnt1=RandPkt.getCount();
PolygonSpeicher PS1=new PolygonSpeicher(Felder1,Cnt1,Ecken,Color.black);
MR.vielePolygoneMalen(PS1);
PointEx [] Felder2=InnenPkt.getPntField();
int Cnt2=InnenPkt.getCount();
PolygonSpeicher PS2=new PolygonSpeicher(Felder2,Cnt2,Ecken,Color.green);
MR.vielePolygoneMalen(PS2);
}

Ich weis das man irgendwie mit dem Viewport arbeiten muss und das woll scrollable was damit zu tun hat. Nur leider hab ich es nicht kapiert.

lg j.b.
 
hi J.B.
dein geposteter Code sagt leider gar gar gar gar nix aus.
Interessant ist für eine grobe analyse, wie Dein Panel zusammengestellt ist, also wie bringst Du Dein JPanel in Dein Scrollpanel. Und dann ist interessant zu wissen wo Deine malen()-Routine eigetnlich aufgerufen wird.

MFG

Takidoso
 
Überschreib mal die rapaint methode.
Sieht ganz dannach aus, als ob das ScrollPanel diese beim Scrollen aufruft(Nur vermutung),
 
Okay werd das mit der repaint methode mal probieren.
Trotzdem hier noch mal der Ort des Aufrufs von malen.
Das Panel und die JScrollPane hab ich im Design-Editor
(Matisse) in mein Frame eingefügt. Weis jemand wies weiter geht?

Danke für die Antworten in Vergangenheit und Zukunft.

*
*
//import&package
*
*

Code:
public class WachstumsFrontEndFrame extends javax.swing.JFrame 
{


    Gewachsenes G=new Gewachsenes();
    
    /** Creates new form WachstumsFrontEndFrame */
    public WachstumsFrontEndFrame() {
        initComponents();
        
    }

    /** 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() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        BtnSave = new javax.swing.JToggleButton();
        BtnLoad = new javax.swing.JToggleButton();
        RBtn3Ecke = new javax.swing.JRadioButton();
        RBtn4Ecke = new javax.swing.JRadioButton();
        RBtn6Ecke = new javax.swing.JRadioButton();
        JSPKarte = new javax.swing.JScrollPane();
        JPK = new javax.swing.JPanel();
        BtnCreate = new javax.swing.JToggleButton();
        TxtFFlaeche = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Gewachsenes");

        BtnSave.setText("Speichern");

        BtnLoad.setText("Laden");

        buttonGroup1.add(RBtn3Ecke);
        RBtn3Ecke.setText("Dreiecke");
        RBtn3Ecke.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RBtn3EckeActionPerformed(evt);
            }
        });

        buttonGroup1.add(RBtn4Ecke);
        RBtn4Ecke.setSelected(true);
        RBtn4Ecke.setText("Vierecke");

        buttonGroup1.add(RBtn6Ecke);
        RBtn6Ecke.setText("Sechsecke");

        JSPKarte.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Karte", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 204, 0))); // NOI18N
        JSPKarte.setWheelScrollingEnabled(false);

        JPK.setMinimumSize(new java.awt.Dimension(5000, 5000));
        JPK.setPreferredSize(new java.awt.Dimension(10000, 10000));

        javax.swing.GroupLayout JPKLayout = new javax.swing.GroupLayout(JPK);
        JPK.setLayout(JPKLayout);
        JPKLayout.setHorizontalGroup(
            JPKLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 10000, Short.MAX_VALUE)
        );
        JPKLayout.setVerticalGroup(
            JPKLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 10000, Short.MAX_VALUE)
        );

        JSPKarte.setViewportView(JPK);

        BtnCreate.setFont(new java.awt.Font("Tahoma", 1, 11));
        BtnCreate.setForeground(new java.awt.Color(255, 0, 0));
        BtnCreate.setText("Start");
        BtnCreate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnCreateActionPerformed(evt);
            }
        });

        jLabel1.setText("Fläche");

        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()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(JSPKarte, javax.swing.GroupLayout.PREFERRED_SIZE, 405, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(BtnLoad, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 194, Short.MAX_VALUE)
                            .addComponent(BtnSave, javax.swing.GroupLayout.DEFAULT_SIZE, 194, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(RBtn3Ecke)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(RBtn4Ecke)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(RBtn6Ecke)
                        .addGap(23, 23, 23)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(BtnCreate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(TxtFFlaeche))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(449, Short.MAX_VALUE)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(JSPKarte, javax.swing.GroupLayout.PREFERRED_SIZE, 412, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BtnSave)
                    .addComponent(RBtn3Ecke)
                    .addComponent(RBtn4Ecke)
                    .addComponent(RBtn6Ecke)
                    .addComponent(TxtFFlaeche, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BtnLoad)
                    .addComponent(BtnCreate))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                        

private void BtnCreateActionPerformed(java.awt.event.ActionEvent evt) {                                          
    
    int f,e=4;    
    f=Integer.parseInt(TxtFFlaeche.getText());
    if(RBtn3Ecke.isSelected())
    {
        e=3;
    }
    if(RBtn4Ecke.isSelected())
    {
        e=4;
    }
    if(RBtn6Ecke.isSelected())
    {
        e=6;
    }
    
    if((f>1)&&(f<10001))
    {
       G.setRahmenWerte(f,e);
       G.erstelleFlaeche();
       //this.setContentPane(JSPKarte);
       G.malen(JPK);
       System.out.println("Fertig");
    }                                         
}

private void RBtn3EckeActionPerformed(java.awt.event.ActionEvent evt) {                                          
// TODO add your handling code here:
}                                         

    /**
    * @param args the command line arguments
    */

    // Variables declaration - do not modify                     
    private javax.swing.JToggleButton BtnCreate;
    private javax.swing.JToggleButton BtnLoad;
    private javax.swing.JToggleButton BtnSave;
    private javax.swing.JPanel JPK;
    private javax.swing.JScrollPane JSPKarte;
    private javax.swing.JRadioButton RBtn3Ecke;
    private javax.swing.JRadioButton RBtn4Ecke;
    private javax.swing.JRadioButton RBtn6Ecke;
    private javax.swing.JTextField TxtFFlaeche;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration                   

}
 
Also es ist mir so denke ich klar warum Du dieses Phänomen hast.
Du musst dein JPanel auf dem gezeichnet werden soll ableiten und die paintComponent-Routine nach deinen Bedürfnissen nach überschreiben.
Ich denke mal dass dann dort deine malen-Methode reinkommen oder zumindest angestoßen werden sollte.


Mit freundlichen Grüßen

Takidoso
 

Neue Beiträge

Zurück