Scrollbars auf JPanel

Ole

Grünschnabel
Guten Abend an alle!

Ich habe folgendes Problem: Ich habe ein Programm geschrieben was ein Bild (BufferedImage) mittels drawImage() auf der Contentpane zeichnet. Nun möchte ich aber gerne Scrollbars einfügen sobald das Bild größer als das Fenster wird.

Alle meine Ansätze bisher haben nicht funtioniert. Weiss jemand wie ich sowas prinzipell realisieren kann?

Gruß,
Ole
 
Hallo

Wie hast du es denn probiert?
Wenn das Label mit dem Bild in einer JScrollPane ist, sollte dies doch gehen?
 
Im Prinzip sieht mein Code so aus (die nicht relevanten Teile und Imports hab ich weggelassen)

Java:
/**
 * Erstellt am: 25.08.2009 / 09:44:26
 * Author: Ole
 */
package Plotter;

/**
 * 
 * @author Ole
 *
 */
public class Plotter extends JFrame
{

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private BufferedImage img = null;
	private ImagePanel ip;
	private Container c;

	/**
	 * 
	 * Erstellt am: 25.08.2009 / 09:48:55
	 * Author: Ole
	 * @param maxY 
	 * @param maxX 
	 * @param bpcount 
	 *
	 */
	public Plotter(common.Base[] _Array, int _bpcount, int _maxX, int _maxY)
	{
		super();

		array = _Array;
		bpcount = _bpcount;
		maxX = _maxX;
		maxY = _maxY;

		initialize();
	}

	/**
	 * 
	 * Erstellt am: 25.08.2009 / 09:48:50
	 * Author: Ole
	 *
	 */
	private void initialize() {
		this.setTitle("RNAPlotter - Strukturdarstellung");
		this.setContentPane(getJContentPane());
		this.setMinimumSize(new Dimension(500, 500));
		this.setExtendedState(JFrame.MAXIMIZED_BOTH);
		this.setJMenuBar(getJJMenuBar());
		
		zeichneStrukturImage();  // Initialisiert img und zeichnet den Inhalt darauf
		
		c = getJContentPane();
		c.setLayout(new BorderLayout(5,5));
		ip = new ImagePanel();
		c.add(new JScrollPane(ip), BorderLayout.CENTER);
		
		 try 
		 { 
		      ip.setImage(img); 
		 } 
		 catch (Exception x) {}
		 pack();
		 this.setVisible(true);
	}
	/**
	 * 
	 * Erstellt am: 25.08.2009 / 09:48:30
	 * Author: Ole
	 *
	 * @return
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel()
			{
				private static final long serialVersionUID = 1L;


				public void paintComponent(Graphics g)
				{
					super.paintComponent(g);
				}
			};

		}
		return jContentPane;
	}
}



public class ImagePanel extends JPanel { 
	private BufferedImage image; 

	public void paintComponent(Graphics g) { 
		super.paintComponent(g); 
		if (image == null) return; 
		g.drawImage(image, 0, 0, image.getWidth() , 
				image.getHeight(), null); 
	} 

	public void setImage(BufferedImage image) { 
		this.image = image; 
	}
}
 
Musst du denn mit BufferedImage arbeiten?
Wieso legst du das Icon nicht einfach auf ein JLabel?

Liese sich z.B. so machen:
Code:
import java.awt.*;

import javax.swing.*;
import java.awt.GridBagConstraints;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JLabel;

public class Demo extends JFrame {

	private JPanel dialogContentPane = null;
	private JScrollPane jScrollPane = null;
	private JLabel jLabel = null;

	public Demo() {
		super();
		initialize();
	}

	private void initialize() {
		getContentPane().setLayout(new GridBagLayout());
		setName("Demo");
		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setResizable(true);
		setSize(50, 50);
		setTitle("Demo");
		setContentPane(getDialogContentPane());
	}

	private javax.swing.JPanel getDialogContentPane() {
		if (dialogContentPane == null) {
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.fill = GridBagConstraints.BOTH;
			gridBagConstraints.gridy = 0;
			gridBagConstraints.weightx = 1.0;
			gridBagConstraints.weighty = 1.0;
			gridBagConstraints.gridx = 0;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 1;
			gridBagConstraints1.insets = new Insets(0, 12, 12, 12);
			gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints1.gridy = 6;
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.gridx = 1;
			gridBagConstraints6.insets = new Insets(0, 12, 4, 12);
			gridBagConstraints6.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints6.gridwidth = 1;
			gridBagConstraints6.gridy = 5;
			dialogContentPane = new javax.swing.JPanel();
			dialogContentPane.setName("DialogContentPane");
			dialogContentPane.setLayout(new java.awt.GridBagLayout());
			dialogContentPane.add(getJScrollPane(), gridBagConstraints);
		}
		return dialogContentPane;
	}

	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jLabel = new JLabel();
			jLabel.setText("");
			jLabel.setIcon(new ImageIcon(getClass().getResource("/gifs/totenkopf.gif")));
			jScrollPane = new JScrollPane();
			jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
			jScrollPane.setViewportView(jLabel);
		}
		return jScrollPane;
	}

}
 
Es muss nicht zwangsweise ein BufferedImage sein. Ist nur einfacher, da der User auch die Option hat es abzuspeichern und ich dann das Image gleich verwenden konnte aber ich werde das nachher mal ausprobieren, Schonmal vielen Dank im vorraus :)
 
OK hab es gerade ausprobiert. Klappt wirklich spitzenmäßig. Vielen lieben Dank für die tolle Hilfe!

Gruß,
Ole
 

Neue Beiträge

Zurück