欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

在窗口中设置三个“圆形”“扇形”“矩形”按钮,点击按钮,将图形变为相应的形状(实心),设置三个“红色”“绿色”“蓝色”滚动条,滑动滚动条变换图形的颜色,设置一个控制左右移动的滚动条,点击滚动条变换图形

程序员文章站 2022-05-30 18:29:48
...

在窗口中设置三个“圆形”“扇形”“矩形”按钮,点击按钮,将图形变为相应的形状(实心),设置三个“红色”“绿色”“蓝色”滚动条,滑动滚动条变换图形的颜色,设置一个控制左右移动的滚动条,点击滚动条变换图形

版权声明:本文为博主原创文章,转载请注明出处

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Dictionary;
import java.util.Hashtable;

public class Main {
	

    public static void main(String[] args) {
        /*
         * 在 AWT 的事件队列线程中创建窗口和组件, 确保线程安全,
         * 即 组件创建、绘制、事件响应 需要处于同一线程。
         */
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                // 创建窗口对象
                MyFrame frame = new MyFrame();
                // 显示窗口
                frame.setVisible(true);
            }
        });
    }

    /**
     * 窗口
     */
    public static class MyFrame extends JFrame {

        public static final String TITLE = "Java图形绘制";

        public static final int WIDTH = 500;
        public static final int HEIGHT = 400;

        public MyFrame() {
            super();
            initFrame();
        }

        private void initFrame() {
            // 设置 窗口标题 和 窗口大小
            setTitle(TITLE);
            setSize(WIDTH, HEIGHT);

            // 设置窗口关闭按钮的默认操作(点击关闭时退出进程)
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

            // 把窗口位置设置到屏幕的中心
            setLocationRelativeTo(null);
            
            
            JPanel main = new JPanel(new BorderLayout());
            final MyPanel pic = new MyPanel(Color.BLACK,80,-1,0);
            main.add(pic,BorderLayout.CENTER);
            
            JPanel btn_panel = new JPanel(new GridLayout(3, 3));
            JButton btn_circle = new JButton("圆形");
            btn_circle.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					pic.setType(0);
					pic.updateUI();
				}
			});
            JButton btn_sx = new JButton("扇形");
            btn_sx.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					System.out.println("扇形");
					pic.setType(1);
					pic.updateUI();
				}
			});
            JButton btn_rec = new JButton("矩形");
            btn_rec.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					pic.setType(2);
					pic.updateUI();
				}
			});
            
            JButton btn_red = new JButton("红色");
            btn_red.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					pic.setColor(Color.RED);
					pic.updateUI();
				}
			});
            
            JButton btn_green = new JButton("绿色");
            btn_green.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					pic.setColor(Color.GREEN);
					pic.updateUI();
				}
			});
            
            JButton btn_blue = new JButton("蓝色");
            btn_blue.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					pic.setColor(Color.BLUE);
					pic.updateUI();
				}
			});
            JButton btn_plus = new JButton("变大");
            btn_plus.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					pic.setSize(150);
					pic.updateUI();
				}
			});
            JButton btn_small = new JButton("变小");
            btn_small.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					pic.setSize(50);
					pic.updateUI();
				}
			});
            
            btn_panel.add(btn_circle);
            btn_panel.add(btn_sx);
            btn_panel.add(btn_rec);
            btn_panel.add(btn_red);
            btn_panel.add(btn_green);
            btn_panel.add(btn_blue);
            btn_panel.add(btn_plus);
            btn_panel.add(btn_small);
            main.add(btn_panel,BorderLayout.NORTH);
            
            JSlider slider = new JSlider(JSlider.VERTICAL,0,2,1);
            slider.setPaintTicks(true);
            slider.setSnapToTicks(true);// 设置滚动条只停留在有刻度的地方
            slider.setMinorTickSpacing(1);
            slider.setMajorTickSpacing(1);
            slider.setPaintLabels(true);
            slider.setInverted(true);
            Dictionary<Integer, Component> labels = new Hashtable<Integer, Component>();
            labels.put(0, new JLabel("红色"));
            labels.put(1, new JLabel("绿色"));
            labels.put(2, new JLabel("蓝色"));
            slider.setLabelTable(labels);
            slider.addChangeListener(new ChangeListener() {
    			@Override
    			public void stateChanged(ChangeEvent e) {
    				JSlider source = (JSlider)e.getSource();
    				switch(source.getValue()){
    				case 0:pic.setColor(Color.RED);break;
    				case 1:pic.setColor(Color.GREEN);break;
    				case 2:pic.setColor(Color.BLUE);break;
    				}
    				pic.updateUI();
    			}
    		});
            main.add(slider,BorderLayout.EAST);
            
            
            JSlider slider2 = new JSlider(JSlider.HORIZONTAL,0,250,1);
            slider2.setPaintTicks(true);
            slider2.setSnapToTicks(true);// 设置滚动条只停留在有刻度的地方
            slider2.setPaintLabels(true);
            slider2.addChangeListener(new ChangeListener() {
    			@Override
    			public void stateChanged(ChangeEvent e) {
    				JSlider source = (JSlider)e.getSource();
    				int sub = source.getValue();
    				pic.setSub(sub);
    				pic.updateUI();
    			}
    		});
            main.add(slider2,BorderLayout.SOUTH);
            setContentPane(main);
        }

    }

    /**
     * 内容面板
     */
    public static class MyPanel extends JPanel {
    	
    	private Color color;
    	private int size;
    	private int type;
    	private int sub;
    	
        public MyPanel(Color color,int size,int type,int sub) {
        	this.color = color;
        	this.size = size;
        	this.type = type;
        	this.sub = sub;
        }
        
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            draw(g,type);
        }

        
        private void draw(Graphics g,int type){
        	if(type == 0){
        		drawCircle(g, color);
        	}else if(type == 1){
        		drawArc(g, color);
        	}else if(type == 2){
        		drawRect(g, color);
        	}
        }
        
        private void drawCircle(Graphics g,Color color){
        	  Graphics2D g2d = (Graphics2D) g.create();
              g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
              g2d.setColor(color);
              // 2. 绘制一个圆: 圆的外切矩形 左上角坐标为(120, 20), 宽高为100
              g2d.fillArc(30+ sub, 20, size, size, 0, 360);
              g2d.dispose();
        }
        
        /**
         * 2. 矩形 / 多边形
         */
        private void drawRect(Graphics g,Color color) {
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setColor(color);
            g2d.fillRect(30+ sub, 20, size, size+20);
            g2d.dispose();
        }

        /**
         * 3. 圆弧 / 扇形
         */
        private void drawArc(Graphics g,Color color) {
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setColor(color);
            // 3. 填充一个扇形
            g2d.fillArc(30 + sub, 20, size, size, 90, 170);
            g2d.dispose();
        }
        
		public void setColor(Color color) {
			this.color = color;
		}

		public void setSize(int size) {
			this.size = size;
		}

		public void setType(int type) {
			this.type = type;
		}
		
		public void setSub(int sub){
			this.sub = sub;
		}
        
    }

}