Bitmap入力とJpeg保存


【 課題 】お絵描きソフトを作る。

宿題2件兼用 (試作版につき、バグ・改良の余地あり)

宿題スレPart54 No.20 http://pc8.2ch.net/test/read.cgi/tech/1133778112/20

宿題スレPart53 No.970 http://pc8.2ch.net/test/read.cgi/tech/1123639475/970

	
	//
	// javac Bmp2Jpeg.java
	// java Bmp2Jpeg output.jpg
	//
	
	import java.io.*;
	import java.awt.image.*;
	import javax.imageio.*;
	import java.awt.*;
	import javax.swing.*;
	import java.awt.event.ActionEvent;
	import java.awt.event.ActionListener;
	import java.awt.event.MouseMotionAdapter;
	import java.awt.event.MouseAdapter;
	import java.awt.event.MouseEvent;
	import java.awt.event.WindowEvent;
	import java.awt.event.WindowAdapter;
	import java.awt.event.InputEvent;
	import java.awt.event.KeyEvent;
	import java.awt.event.MouseMotionAdapter;
	
	public class Bmp2Jpeg extends JFrame{
		final static String prod = "★★ Java の宿題ここで答えます Part 54 ★★";
		final static String ver = "1.0.0";
		final static String usage = "Usage: Bmp2Jpeg outfile.jpg";
		final static int rectW = 160;
		final static int rectH = 120;
		Bmp2JpegCanvas canvas;
	
		public static void main(String[] args){
			if(args.length <= 0){
				System.out.println(usage);
			}else if(args[0].length() <= 0){
				System.out.println(usage);
			}else{
				Bmp2Jpeg b2j = new Bmp2Jpeg(args[0]);
				b2j.addWindowListener(
					new WindowAdapter(){
						public void windowClosing(WindowEvent e){
							System.exit(0);
						}
					}
				);
			}
		}
	
		Bmp2Jpeg(String f){
			MenuBar menubar = new MenuBar();
			setJMenuBar(menubar);
			setResizable(false);
			canvas = new Bmp2JpegCanvas(f, rectW, rectH);
			getContentPane().add(canvas);
			pack();
			setBounds(0, 0, rectW + 8, rectH + 50);
			setVisible(true);
		}
	
		class MenuBar extends JMenuBar{
			Listener_Command listener_Command;
			final String Menu_File = "MF";
			final String Menu_Help = "MH";
	
			MenuBar(){
				listener_Command = new Listener_Command();
	
				JMenu menu1 = new JMenu("ファイル");
				JMenuItem item1 = new JMenuItem("保存(Ctrl+Shift+s)");
				item1.setActionCommand(Menu_File);
				item1.addActionListener(listener_Command);
				item1.setAccelerator(KeyStroke.getKeyStroke("ctrl shift S"));
				menu1.add(item1);
				add(menu1);
	
				JMenu menu2 = new JMenu("ヘルプ");
				JMenuItem item2 = new JMenuItem("バージョン情報(Ctrl+Shift+v)");
				item2.setActionCommand(Menu_Help);
				item2.addActionListener(listener_Command);
				item2.setAccelerator(KeyStroke.getKeyStroke("ctrl shift V"));
				menu2.add(item2);
				add(menu2);
			}
	
			class Listener_Command implements ActionListener{
				public void actionPerformed(ActionEvent e){
					String str = e.getActionCommand();
					if(str.compareTo(Menu_File) == 0){
						canvas.Save();
					}else if(str.compareTo(Menu_Help) == 0){
						JOptionPane.showMessageDialog(
							null,
							"Bmp2Jpeg\nProduced by " + prod + "\nversion " + ver,
							"Version情報",
							JOptionPane.INFORMATION_MESSAGE
						);
					}
				}
			}
		}
	}
	
	class Bmp2JpegCanvas extends Canvas{
		String file;
		BufferedImage bi;
		Graphics bg;
		Listener_MouseMotion listener_MouseMotion;
		Listener_MouseClick listener_MouseClick;
		int px = 0, py = 0, nx = 0, ny = 0, clk = 0;
	
		Bmp2JpegCanvas(String f, int w, int h){
			file = f;
			bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_BGR);
			if(bi != null){
				bg = bi.getGraphics();
				bg.setColor(Color.white);
				bg.fillRect(0, 0, w, h);
				listener_MouseMotion = new Listener_MouseMotion();
				listener_MouseClick = new Listener_MouseClick();
				addMouseListener(listener_MouseClick);
				addMouseMotionListener(listener_MouseMotion);
			}
		}
	
		public void Save(){
			try{
				if(bi != null && file != null){
					ImageIO.write(bi, "jpeg", new File(file));
				}
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	
		public void paint(Graphics g){
			Graphics2D g2 = (Graphics2D)g;
			g2.drawImage(bi, 0, 0, this);
		}
	
		class Listener_MouseMotion extends MouseMotionAdapter{
			public void mouseMoved(MouseEvent e){
				if(clk == 1){
					bg.setColor(Color.white);
					bg.drawRect(px, py, nx - px, ny - py);
					nx = e.getX();
					ny = e.getY();
					bg.setColor(Color.green);
					bg.drawRect(px, py, nx - px, ny - py);
					repaint();
				}
			}
		}
	
		class Listener_MouseClick extends MouseAdapter{
			public void mouseClicked(MouseEvent e){
				if(javax.swing.SwingUtilities.isLeftMouseButton(e)){
					if(clk == 0){
						px = e.getX();
						py = e.getY();
						clk = 1;
					}else{
						bg.setColor(Color.blue);
						bg.drawRect(px, py, nx - px, ny - py);
						clk = 0;
					}
					repaint();
				}else if(javax.swing.SwingUtilities.isRightMouseButton(e)){
					Dimension d = getSize();
					bg.setColor(Color.white);
					bg.fillRect(0, 0, d.width, d.height);
					repaint();
				}
			}
		}
	}

CONTENTS

最新の20件

2020-11-14 2005-12-06 2006-11-04 2012-07-15 2009-06-19 2011-03-03 2006-12-13 2007-11-05 2014-07-22 2014-07-19 2014-07-09 2014-01-14 2012-09-03 2012-03-28

今日の20件

人気の30件

  • counter: 9322
  • today: 1
  • yesterday: 1
  • online: 2