Java Graphics_11

package com.han;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.SpringLayout;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

@SuppressWarnings("serial")
public class Graphics_11 extends JFrame {
	
	public Graphics_11() {
		// TODO Auto-generated constructor stub
		Container container = getContentPane();
		SpringLayout springLayout = new SpringLayout();
		container.setLayout(springLayout);
		final JSlider slider = new JSlider();
		JScrollPane scrollPane = new JScrollPane();
		final JLabel label = new JLabel();
		URL imgURL = this.getClass().getResource("/images/Lighthouse.jpg");
		final Image image = Toolkit.getDefaultToolkit().getImage(imgURL);
//		final Image image = Toolkit.getDefaultToolkit().getImage("/images/Lighthouse.jpg"); // can not find the image file ! the string address has to be an complete absolute file path.
		System.out.println(image.getWidth(null) + "\t" + image.getHeight(null));
		ImageIcon icon = new ImageIcon(image);
		label.setIcon(icon);
		System.out.println(icon.getIconWidth() + "\t" + icon.getIconHeight());
		System.out.println(image.getWidth(label) + "\t" + image.getHeight(label));
//		label.setText("here");
		label.setBackground(Color.RED);
//		JTextArea textArea = new JTextArea();
//		JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
//		panel.add(label);
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(label, BorderLayout.CENTER);
		label.setHorizontalAlignment(SwingConstants.CENTER);
//		System.out.println(label.getInsets());
//		label.setBounds(0, 0, 300, 300);
//		scrollPane.getViewport().add(panel); // is equivalent to "scrollPane.setViewportView(panel);" and "scrollPane.getViewport().setView(panel);"
//		scrollPane.setViewportView(panel); // use the JPanel in the JScrollPanel's viewPort is for using the corresponding layout manager
//		scrollPane.setViewportView(label); 
		
		scrollPane.getViewport().setBackground(Color.YELLOW);
//		scrollPane.getViewport().setLayout(new FlowLayout()); // it should not be set
		scrollPane.getViewport().add(panel);
		panel.setBackground(Color.BLUE);
		
		/* use the SpringLayout to layout all children comprised in the container */
		container.add(scrollPane);
//		container.add(label);
		container.add(slider);
		springLayout.putConstraint(SpringLayout.NORTH, scrollPane, 0, SpringLayout.NORTH, container);
		springLayout.putConstraint(SpringLayout.WEST, slider, 0, SpringLayout.WEST, container);
		springLayout.putConstraint(SpringLayout.EAST, slider, 0, SpringLayout.EAST, container);
		springLayout.putConstraint(SpringLayout.WEST, scrollPane, 0, SpringLayout.WEST, container);
		springLayout.putConstraint(SpringLayout.EAST, scrollPane, 0, SpringLayout.EAST, container); //       !
		springLayout.putConstraint(SpringLayout.SOUTH, slider, 0, SpringLayout.SOUTH, container);
		springLayout.putConstraint(SpringLayout.SOUTH, scrollPane, 0, SpringLayout.NORTH, slider);
		
		/* configure JSlider */
		slider.setMinimum(1);
		slider.setMaximum(1000);
		slider.setValue(100);
		slider.addChangeListener(new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent e) {
				// TODO Auto-generated method stub
				int currentValue = slider.getValue();
				int newWidth = image.getWidth(label) * currentValue / 100;
				int newHeight = image.getHeight(label) * currentValue / 100;
				Image imageScaled = image.getScaledInstance(newWidth, 
						newHeight, Image.SCALE_FAST);
//				label.setBounds(0, 0, newWidth, newHeight); // null           setBounds()    
//				label.setLocation(0, 0);
				label.setIcon(new ImageIcon(imageScaled));
			}
			
		});
		
		/* initialize the frame */
		setTitle("    ");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(
				getGraphicsConfiguration());
		setBounds(0, 0, d.width, d.height - insets.bottom);
		setVisible(true);
		System.out.println(panel.getSize());
		System.out.println(slider.getSize());
		System.out.println(container.getSize());
		System.out.println(scrollPane.getSize());
		System.out.println(label.getSize());
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new Graphics_11();
	}

}

좋은 웹페이지 즐겨찾기