JAVA图形图像

1.AWT继承体系

在这里插入图片描述

1.Frame/JFrame–窗口(容器)
所有容器都依附于Window

//创建窗口对象
Frame frame=new Frame("这是个窗口");
//设置窗口位置,单位px
frame.setLocation(100, 100);
//设置窗口大小
frame.setSize(20, 20);
//设置窗口可见
frame.setVisible(true);

2.Panel(容器)–

1.创建window对象-frame
2.创建Panel对象-p
3.添加文本框
p.add(new TextField("xxxxxxxxx");
4.添加按钮
p.add(new Button("dddd")
5.Panle放到window中
frame.add(p)

3.ScrollPane-滚动条

1.创建window对象-frame
2.创建ScroPane对象-p
3.添加文本框
p.add(new TextField("xxxxxxxxx");
4.添加按钮
p.add(new Button("dddd")
5.ScroPane放到window中
frame.add(p)

4 .Box(容器)
在这里插入图片描述

 Frame frame=new Frame("dddd");
        Box hBox = Box.createHorizontalBox();
        hBox.add(new Button("水平1"));
        hBox.add(new Button("水平2"));
        Box vBox = Box.createVerticalBox();
        vBox.add(new Button("垂直1"));
        vBox.add(new Button("垂直2"));
        frame.add(vBox,BorderLayout.NORTH);
        frame.add(hBox);
        frame.pack();
        frame.setVisible(true);

增加间隔:
在这里插入图片描述

5.LayoutManager布局管理器
在这里插入图片描述

  1. FlowLayout–流式布局
1.创建window对象-frame

2.通过setLayout方法设置容器的布局管理器,垂直水平20px

frame.setLayout(new FlowLayout(FlowLayout.LEFT,20,20));
frame.add(p)

3.设置最佳位置
frame.pack();
  1. BorderLayout
    在这里插入图片描述
1.创建window对象-frame

//给frame设置Borderlayout
	frame.setLayout(new BorderLayout(30,30));
	frame.add(new Button("北侧"),BorderLayout.NORTH);
	frame.add(new Button("南侧"),BorderLayout.SOUTH);
	frame.add(new Button("东侧"),BorderLayout.EAST);
	frame.add(new Button("西侧"),BorderLayout.WEST);
	frame.add(new Button("中侧"),BorderLayout.CENTER);
	

3.设置最佳位置
frame.pack();
  1. BoxLayout–盒式布局管理器
 Frame jFrame=new Frame("xxxx");
        BoxLayout b=new BoxLayout(jFrame,BoxLayout.Y_AXIS);
        jFrame.setLayout(b);
        jFrame.add(new Button("这是第一个按钮"));
        jFrame.add(new Button("这是第二个按钮"));
        jFrame.pack();
        jFrame.setVisible(true);
ImageComparerUI——基于Java语言实现的相似图像识别,基于直方图比较算法。 import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.MediaTracker; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JPanel; public class ImageComparerUI extends JComponent implements ActionListener { /** * */ private static final long serialVersionUID = 1L; private JButton browseBtn; private JButton histogramBtn; private JButton compareBtn; private Dimension mySize; // image operator private MediaTracker tracker; private BufferedImage sourceImage; private BufferedImage candidateImage; private double simility; // command constants public final static String BROWSE_CMD = "Browse..."; public final static String HISTOGRAM_CMD = "Histogram Bins"; public final static String COMPARE_CMD = "Compare Result"; public ImageComparerUI() { JPanel btnPanel = new JPanel(); btnPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); browseBtn = new JButton("Browse..."); histogramBtn = new JButton("Histogram Bins"); compareBtn = new JButton("Compare Result"); // buttons btnPanel.add(browseBtn); btnPanel.add(histogramBtn); btnPanel.add(compareBtn); // setup listener... browseBtn.addActionListener(this); histogramBtn.addActionListener(this); compareBtn.addActionListener(this); mySize = new Dimension(620, 500); JFrame demoUI = new JFrame("Similiar Image Finder"); demoUI.getContentPane().setLayout(new BorderLayout()); demoUI.getContentPane().add(this, BorderLayout.CENTER); demoUI.getContentPane().add(btnPanel, BorderLayout.SOUTH); demoUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); demoUI.pack(); demoUI.setVisible(true); } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; if(sourceImage != null) { Image scaledImage = sourceImage.getScaledInstance(300, 300, Image.SCALE_FAST); g2.drawImage(scaledImage, 0, 0, 300, 300, null); } if(candidateImage != null) { Image scaledImage = candidateImage.getScaledInstance(300, 330, Image.SCALE_FAST); g2.drawImage(scaledImage, 310, 0, 300, 300, null); } // display compare result info here Font myFont = new Font("Serif", Font.BOLD, 16); g2.setFont(myFont); g2.setPaint(Color.RED); g2.drawString("The degree of similarity : " + simility, 50, 350); } public void actionPerformed(ActionEvent e) { if(BROWSE_CMD.equals(e.getActionCommand())) { JFileChooser chooser = new JFileChooser(); chooser.showOpenDialog(null); File f = chooser.getSelectedFile(); BufferedImage bImage = null; if(f == null) return; try { bImage = ImageIO.read(f); } catch (IOException e1) { e1.printStackTrace(); } tracker = new MediaTracker(this); tracker.addImage(bImage, 1); // blocked 10 seconds to load the image data try { if (!tracker.waitForID(1, 10000)) { System.out.println("Load error."); System.exit(1); }// end if } catch (InterruptedException ine) { ine.printStackTrace(); System.exit(1); } // end catch if(sourceImage == null) { sourceImage = bImage; }else if(candidateImage == null) { candidateImage = bImage; } else { sourceImage = null; candidateImage = null; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值