Thread: Canvas question
View Single Post
  #2 (permalink)  
Old 08-03-2007, 08:21 PM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,146
hardwired is on a distinguished road
Code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class PaintHere extends JPanel { int x = -30; int y = -30; public PaintHere() { addMouseListener(clicker); } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.drawRect(x, y, 30, 30); } private MouseListener clicker = new MouseAdapter() { public void mousePressed(MouseEvent e) { x = e.getX(); y = e.getY(); repaint(); } }; public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(new PaintHere()); f.setSize(300,300); f.setLocation(200,200); f.setVisible(true); } }
Reply With Quote