作成 2010.01.11
更新 2010.01.11
更新 2010.01.11
Java - ウィンドウをリサイズしても描画が消えないようにするメモ
コンパイルバージョンは javac 1.6.0_17 です。
ウィンドウのリサイズは ComponentListener で取得する。
Graphics で描画する内容は BufferedImage で保持する。
ウィンドウのリサイズは ComponentListener で取得する。
Graphics で描画する内容は BufferedImage で保持する。
import java.awt.Canvas; import java.awt.Color; import java.awt.BorderLayout; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ComponentEvent; import java.awt.event.ComponentListener; import java.awt.event.KeyEvent; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.image.BufferedImage; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.KeyStroke; import javax.swing.SwingUtilities; class Oresen implements ActionListener,Runnable{ static final String STR_TITLE = "test"; static final String STR_END = "End"; static final String STR_FILE = "File"; static final String STR_SAVE = "Save"; static final String STR_OPEN = "Open"; Oresen(){ } public void run(){ JFrame f = new JFrame(STR_TITLE); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLayout(new BorderLayout()); f.setSize(400,300); JMenuBar mb = new JMenuBar(); JMenu m = null; JMenuItem mi = null; m = new JMenu(STR_FILE); m.setMnemonic(KeyEvent.VK_F); mi = new JMenuItem(STR_OPEN, KeyEvent.VK_O); mi.setMnemonic(KeyEvent.VK_O); mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK)); mi.setActionCommand(STR_OPEN); mi.addActionListener(this); m.add(mi); mi = new JMenuItem(STR_SAVE, KeyEvent.VK_S); mi.setMnemonic(KeyEvent.VK_S); mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK)); mi.setActionCommand(STR_SAVE); mi.addActionListener(this); m.add(mi); m.addSeparator(); mi = new JMenuItem(STR_END, KeyEvent.VK_E); mi.setMnemonic(KeyEvent.VK_E); mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, ActionEvent.ALT_MASK)); mi.setActionCommand(STR_END); mi.addActionListener(this); m.add(mi); mb.add(m); f.setJMenuBar(mb); OresenCanvas oc = new OresenCanvas(); oc.addComponentListener(oc); oc.addMouseListener(oc); f.add(oc, BorderLayout.CENTER); f.setVisible(true); } public void actionPerformed(ActionEvent e){ if(STR_END.equals(e.getActionCommand())){ System.exit(0); } } public static void main(String args[]){ Oresen o = new Oresen(); SwingUtilities.invokeLater(o); } } class OresenCanvas extends Canvas implements ComponentListener,MouseListener{ public static final long serialVersionUID = 1111L; BufferedImage bi = null; Graphics2D g2d = null; Point pp = null; OresenCanvas(){ setBackground(Color.WHITE); } public void mouseClicked(MouseEvent e){ Point p = e.getPoint(); if(pp == null){ g2d.fillOval(p.x, p.y, 1, 1); }else{ g2d.drawLine(pp.x, pp.y, p.x, p.y); } pp = p; paint(getGraphics()); } public void paint(Graphics g){ g.drawImage(bi, 0, 0, this); } public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mousePressed(MouseEvent e){} public void mouseReleased(MouseEvent e){} public void componentHidden(ComponentEvent e){} public void componentMoved(ComponentEvent e){} public void componentResized(ComponentEvent e){ int w = getWidth(); int h = getHeight(); boolean isChange = false; if(bi == null){ isChange = true; }else{ if(w > bi.getWidth()){ isChange = true; }else{ w = bi.getWidth(); } if(h > bi.getHeight()){ isChange = true; }else{ h = bi.getHeight(); } } if(isChange){ BufferedImage nbi = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR); g2d = nbi.createGraphics(); g2d.setBackground(Color.WHITE); g2d.setColor(Color.WHITE); g2d.fill(new Rectangle(0, 0, w, h)); g2d.setColor(Color.RED); if(bi != null){ g2d.drawImage(bi, 0, 0, this); } bi = nbi; } } public void componentShown(ComponentEvent e){} }
タグ: Java