import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Exer02 { private JFrame frame = new JFrame("tenQ["); private JPanel pane = (JPanel)frame.getContentPane(); private int w = 400, h = 400; private int bx = 170; private Canvas canvas = new Canvas(){ @Override public void update(Graphics g){ paint(g); } @Override public void paint(Graphics g){ Rectangle rec = g.getClipBounds(); w = rec.width; h = rec.height; Image img = createImage(w, h); Graphics ofg = img.getGraphics(); ofg.setColor(Color.BLUE); ofg.fillRect(bx - 30, h - 50, 60, 20); g.drawImage(img, 0, 0, this); try{Thread.sleep(20);}catch(Exception ex){} repaint(); } }; public Exer02(){ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); canvas.setBackground(Color.WHITE); canvas.setSize(w, h); canvas.addMouseMotionListener(new MouseMotionAdapter(){ @Override public void mouseMoved(MouseEvent ev){ bx = ev.getX(); if(bx > w - 30) bx = w - 30; else if(bx < 30) bx = 30; } }); pane.add(canvas); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { G106WinLF.set(); new Exer02(); } }