重写Jlabel类的paint()方法实现标签图片的倾斜显示- import java.awt.BorderLayout;
- import java.awt.Component;
- import java.awt.EventQueue;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Image;
- import java.util.Dictionary;
- import java.util.Hashtable;
- import javax.swing.ImageIcon;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JSlider;
- import javax.swing.UIManager;
- import javax.swing.border.EmptyBorder;
- import javax.swing.event.ChangeEvent;
- import javax.swing.event.ChangeListener;
- public class ShearImageDemo extends JFrame {
-
- /**
- *
- */
- private static final long serialVersionUID = 3405356852103214547L;
- private JPanel contentPane;
- private JLabel label;
-
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- try {
- UIManager
- .setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
- } catch (Throwable e) {
- e.printStackTrace();
- }
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- ShearImageDemo frame = new ShearImageDemo();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
-
- /**
- * Create the frame.
- */
- public ShearImageDemo() {
- setTitle("倾斜图像");// 设置窗体的标题
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置窗体退出时操作
- setBounds(100, 100, 450, 300);// 设置窗体位置和大小
- contentPane = new JPanel();// 创建内容面板
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));// 设置面板的边框
- contentPane.setLayout(new BorderLayout(0, 0));// 设置内容面板为边界布局
- setContentPane(contentPane);// 应用内容面板
-
- JPanel panel = new JPanel();// 创建面板
- contentPane.add(panel, BorderLayout.SOUTH);// 将新面板放置在内容面板南面
-
- final JSlider slider = new JSlider();
- slider.setSnapToTicks(true);// 设置滑块自动靠近最近的刻度值
- slider.setMajorTickSpacing(1);// 将主刻度间隔设置成1
- slider.setPaintLabels(true);// 显示滑块标签
- slider.setPaintTicks(true);// 显示滑块刻度值
- slider.setMinimum(0);// 设置滑块的最小值为0
- slider.setMaximum(10);// 设置滑块的最大值为10
- Dictionary<Integer, Component> sliderLabel = new Hashtable<Integer, Component>();// 创建滑块标签集合
- sliderLabel.put(0, new JLabel("0.0"));// 为0刻度增加标签0.0
- sliderLabel.put(1, new JLabel("0.1"));// 为1刻度增加标签0.1
- sliderLabel.put(2, new JLabel("0.2"));// 为2刻度增加标签0.2
- sliderLabel.put(3, new JLabel("0.3"));// 为3刻度增加标签0.3
- sliderLabel.put(4, new JLabel("0.4"));// 为4刻度增加标签0.4
- sliderLabel.put(5, new JLabel("0.5"));// 为0刻度增加标签0.5
- sliderLabel.put(6, new JLabel("0.6"));// 为1刻度增加标签0.6
- sliderLabel.put(7, new JLabel("0.7"));// 为2刻度增加标签0.7
- sliderLabel.put(8, new JLabel("0.8"));// 为3刻度增加标签0.8
- sliderLabel.put(9, new JLabel("0.9"));// 为4刻度增加标签0.9
- sliderLabel.put(10, new JLabel("1.0"));// 为4刻度增加标签0.9
- slider.setLabelTable(sliderLabel);// 应用滑块标签
- slider.addChangeListener(new ChangeListener() {// 为滑块增加事件监听器
- public void stateChanged(ChangeEvent e) {
- do_slider_stateChanged(e);
- }
- });
- panel.setLayout(new BorderLayout(0, 0));// 设置新面板的布局为边界布局
- panel.add(slider);// 在新面板上增加滑块
-
- label = new JLabel("") {
-
- /**
- *
- */
- private static final long serialVersionUID = 8885169965910681721L;
-
- @Override
- public void paint(Graphics g) {
- ImageIcon icon = new ImageIcon("src/images/image.jpg");// 获得图标
- Image image = icon.getImage();// 获得图像
- Graphics2D g2 = (Graphics2D) g;// 获得Graphics2D对象
- int value = slider.getValue();// 获得滑块值
- g2.shear((double) value / 10, 0);// 将图片向水平方向倾斜
- g2.drawImage(image, 0, 0, null);// 绘制图像
-
- }
- };
- contentPane.add(label, BorderLayout.CENTER);// 在内容面板中央增加标签
- }
-
- protected void do_slider_stateChanged(ChangeEvent e) {
- label.repaint(); // 滑动滑块时重新绘制标签
-
- }
-
- }
复制代码 |