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();
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.