JFrame Window size

I’m trying to visualise a Plan for Busservices. For that I need to create a Graphic Panel that is at least 2900 Pixels wide (to show 24h, 1 Pixel = 30sec). First I tried it with a “normal” JFrame, but this didn’t want to create a window wider as my Monitor even tho i set the size to 2900. Then I tried to go around the problem by using scroll bars, but there I’m stuck now.
I’m not very experienced in java, I only had a beginners course that told me the basic stuff for little programs but nothing more.
Hope you can help me with this, thanks in advance!

                JFrame f = new JFrame();
            f.setSize(1000, 800);
                
            JPanel panel= new JPanel();
            panel.setSize(2900,800);
            
            panel.setPreferredSize(new Dimension(2900,800));
            
            Planzeichner dp = new Planzeichner(Umläufe); 
            panel.add(dp);

            JScrollPane scPa = new JScrollPane (panel, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
            f.getContentPane().add(scPa);
            
            f.setVisible(true);

Planzeichner.java:

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import java.awt.Graphics2D;
import java.util.Vector;

public class Planzeichner extends JPanel{
    
    //Aus "Main" übertragene Variablen  
    private Vector Umläufe;
    
    //Konstruktor zum Annehmen der Werte
        public Planzeichner(Vector Umläufe) {
            super();
            this.Umläufe = Umläufe;
            }
        
    //Laufvariablen
    int k = 0;
    int i = 0;
    int j = 0;
    
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        
        Graphics2D g2d = (Graphics2D) g;
        
        //Koordinatensystem
        g.setColor(Color.black);
        g.drawLine(10, 10, 2890, 10);
        g.setColor(Color.LIGHT_GRAY);
        for(i=0; i<24; i++) {;
        g.drawLine(10+i*120,10,10+i*120,900);
        String Stunde = String.valueOf(i+5);
        g.drawString(Stunde, 10+i*120, 9);
        }
    }
}

  • 1

    1) I can’t see the things I have drawn on the panel. – because you didn’t override the getPreferredSize() method of your panel. When you do custom painting you need to determine its preferred size so the layout manager can work properly. 2) Why are you creating the “panel” variable? You are doing your custom painting on a JPanel so just add that panel to the scroll pane. Read the Swing tutorial on Custom Painting for more information and working examples.

    – 

Do not create a JFrame of that size. Instead tell your Planzeichner the size you prefer so the layout manager can do it’s job.

Finally just make up your mind whether you want to use that panel in a JFrame and use the JFrame’s pack() method to size it accordingly, or whether you stuff it into a JScrollPane before adding it to the JFrame.

In code this might look like this:

Planzeichner.java:

public class Planzeichner extends JPanel{
    ...
    int secondsPerPixel = 30;

    public void setZoom(int secondsPerPixel) {
       this.secondsPerPixel = secondsPerPixel;
    }

    public Dimension getPreferredSize() {
        return new Dimension(20 + (86400 / secondsPerPixel), 800);
    }
}

Application code:

Planzeichner dp = new Planzeichner(Umläufe); 

JFrame f = new JFrame();
f.setLayout(new BorderLayout());

// use either one of these two lines            
f.add(dp);
f.add(new JScrollPane(dp));

f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);

Leave a Comment