Results 1 to 2 of 2

Thread: Canvas question

  1. #1
    christina is offline Member
    Join Date
    Jul 2007
    Posts
    39
    Rep Power
    0

    Default Canvas question

    I have to do an APButton on a canvas that is to say when I click/press any where in the canvas it should create a button(rectangle) "fillRect(0,0,30,30);"

    any ideas?
    how can I do that?

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java 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);
        }
    }

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  2. Calling displayable from a canvas class
    By Snalk in forum CLDC and MIDP
    Replies: 0
    Last Post: 03-28-2008, 11:58 PM
  3. Using SWT Canvas
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-08-2008, 09:06 AM
  4. Canvas rendered over JCombobox menu
    By JSK in forum AWT / Swing
    Replies: 2
    Last Post: 11-19-2007, 08:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •