본문 바로가기
자바

자바 소스코드: 명품자바프로그래밍 오픈첼린지 12(클리핑 적용)

by 드린 2016. 11. 12.

목차

    반응형
    package javaapplication18;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    
    class ImagePanel extends JFrame{
        
        final int MOVE=10;
        ImagePanel(){
            this.setTitle("Open Challenge 12");
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            MyPanel panel = new MyPanel();
            this.add(panel, BorderLayout.CENTER);
            this.setLocationRelativeTo(null);
            this.setSize(400,400);
            this.setVisible(true);
            panel.requestFocus(true);
        }
        class MyPanel extends JPanel{
            int clipX=50, clipY=50;
            ImageIcon icon = new ImageIcon("back.jpg");
            Image img = icon.getImage();
            MyPanel(){
                this.addKeyListener(new KeyListener(){
                    @Override
                    public void keyTyped(KeyEvent ke) {
                    }
    
                    @Override
                    public void keyPressed(KeyEvent ke) {
                        if(ke.VK_UP==ke.getKeyCode()){
                            clipY-=MOVE;
                            repaint();
                        }
                        if(ke.VK_DOWN==ke.getKeyCode()){
                            clipY+=MOVE;
                            repaint();
                        }
                        if(ke.VK_RIGHT==ke.getKeyCode()){
                            clipX+=MOVE;
                            repaint();
                        }
                        if(ke.VK_LEFT==ke.getKeyCode()){
                            clipX-=MOVE;
                            repaint();
                        }
                        
                    }
    
                    @Override
                    public void keyReleased(KeyEvent ke) {
                    }
                });
            }
            public void paintComponent(Graphics g){
                super.paintComponent(g);
                g.clipRect(clipX, clipY, 50, 50);
                g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(),this);
            }
        }
    }
    
    public class JavaApplication18 {
        public static void main(String[] args) {
            new ImagePanel();
        }
    }
    
    

    <결과>

    2016/11/12 - [자바] - 자바 소스코드: 새로운 버튼 만들기(paintComponent 적용)

    2016/11/12 - [자바] - 자바 소스코드: Drawing Line by Mouse 예제(repaint)

    2016/11/12 - [자바] - 자바 소스코드: Clip 사용 예제(클리핑)

    #자바 #자바 소스코드 #명품 자바 프로그래밍 오픈 첼린지 12 #클리핑 적용 #실습 예제

    반응형