Java中scroll pane的使用(二)

package com.han;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.Toolkit;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.font.LineMetrics;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToggleButton;
import javax.swing.ScrollPaneConstants;
import javax.swing.Scrollable;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

/**
 * @author HAN
 * 
 */
@SuppressWarnings("serial")
public class ScrollDemo extends JPanel implements ItemListener {
	private JScrollPane scrollPane;
	private Rule hRule;
	private Rule vRule;

	/**
	 * Create a placeholder icon, which consists in a white box with a black
	 * border and a red x inside. It's used to display something when there are
	 * issues loading an icon from an external location.
	 * 
	 * @author HAN
	 * 
	 */
	class MissingIcon implements Icon {
		private int width = 32;
		private int height = 32;

		@Override
		public void paintIcon(Component c, Graphics g, int x, int y) {
			// TODO Auto-generated method stub
			Graphics2D g2 = (Graphics2D) g;
			Shape rect = new Rectangle2D.Double(x + 1, y + 1, width - 2,
					height - 2);
			g2.setColor(Color.WHITE);
			g2.fill(rect);
			g2.setColor(Color.BLACK);
			g2.draw(rect);// By default, the stroke is 1.0f solid line.

			g2.setColor(Color.RED);
			BasicStroke stroke = new BasicStroke(4.0f);
			g2.setStroke(stroke);
			g2.draw(new Line2D.Double(x + 10, y + 10, x + width - 10, y
					+ height - 10));
			g2.draw(new Line2D.Double(x + 10, y + height - 10, x + width - 10,
					y + 10));
		}

		@Override
		public int getIconWidth() {
			// TODO Auto-generated method stub
			return width;
		}

		@Override
		public int getIconHeight() {
			// TODO Auto-generated method stub
			return height;
		}
	}

	public class Corner extends JComponent {
		@Override
		protected void paintComponent(Graphics g) {
			g.setColor(new Color(230, 163, 4));
			g.fillRect(0, 0, getWidth(), getHeight());
		}
	}

	/**
	 * The enum type is more powerful than the traditional constants defining by
	 * encapsulate them in an interface.
	 * 
	 * @author HAN
	 * 
	 */
	enum RuleConstants {
		HORIZONTAL, VERTICAL
	}

	public class Rule extends JComponent {
		private RuleConstants direction;
		private String mode;

		// Define the distance on screen for an inch and a centimeter.
		final int INCH = Toolkit.getDefaultToolkit().getScreenResolution();
		final int CM = (int) (INCH / 2.54);

		private int preferredWidth;
		private int preferredHeight;

		private static final int SIZE = 35;

		Rule(RuleConstants direction, String mode) {
			this.direction = direction;
			this.mode = mode;
		}

		private void setUnitMode(String mode) {
			this.mode = mode;
		}

		private int getIncrement() {
			if (mode.equals("cm")) {
				return CM;
			} else if (mode.equals("inch")) {
				return IN
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值