1

I've done days of searching for a way to draw pixels to a window in java with mouse capture. I'm looking for some framework I can just plug in. It seems like it would be so simple... Any help will be greatly appreciated.

(EDIT) Here is some non-working code.

public class Base extends JPanel implements MouseMotionListener {

    public static void main(String[] args) {
        new Base();
    }

    final static int width = 800;
    final static int height = 600;
    BufferedImage img;
    Base() {
        img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB_PRE);
        JFrame frame = new JFrame();
        frame.addMouseMotionListener(this);
        frame.add(this);
        frame.setSize(width, height);
        frame.setEnabled(true);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @Override
    public void mouseDragged(MouseEvent e) {
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        Graphics g = img.getGraphics();
        g.drawRect(1, 1, width - 2, height - 2);
        g.dispose();
        repaint();
    }
    @Override 
    public void paintComponent(Graphics g) {
         g.drawImage(img, 0, 0, null);
    }

}
motoku
  • 1,571
  • 1
  • 21
  • 49
  • You might wish to give more information. For instance what graphics and GUI libraries are you using? Swing? SWT? Other? Have you tried anything? – Hovercraft Full Of Eels Jul 01 '11 at 22:38
  • 1
    It's also not that hard to do on your own - give it a whirl, see what happens. – Ken Wayne VanderLinde Jul 01 '11 at 22:39
  • Part of my problem is I don't know where to start. – motoku Jul 01 '11 at 22:44
  • @Sean Pedersen: *"..I don't know where to start"* 1) Your [first cup of Java](http://download.oracle.com/javase/tutorial/getStarted/cupojava/index.html). 2) Then maybe [Creating a GUI With JFC/Swing](http://download.oracle.com/javase/tutorial/uiswing/) 3) Then [Performing Custom Painting](http://download.oracle.com/javase/tutorial/uiswing/painting/) 4) And perhaps [How To Ask Questions The Smart Way](http://www.catb.org/~esr/faqs/smart-questions.html). – Andrew Thompson Jul 01 '11 at 22:51
  • See also [`LinePanel`](http://stackoverflow.com/questions/5797862/draw-a-line-in-a-jpanel-with-button-click-in-java/5797965#5797965). – trashgod Jul 02 '11 at 17:27

2 Answers2

3

enter image description here

See comments in the code.

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;

public class Base extends JPanel implements MouseMotionListener {

    public static void main(String[] args) {
        new Base();
    }

    final static int width = 400;
    final static int height = 300;
    BufferedImage img;
    Base() {

        img = new BufferedImage(width, height, 
            BufferedImage.TYPE_INT_ARGB_PRE);
        // do in preference to setting the frame size..
        setPreferredSize(new Dimension(width, height));
        JFrame frame = new JFrame();
        this.addMouseMotionListener(this); // INSTEAD OF THE FRAME
        frame.add(this);
        //frame.setSize(width, height);  DO INSTEAD...
        frame.pack();
        //frame.setEnabled(true);  REDUNDANT
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  // good call!
    }

    @Override
    public void mouseDragged(MouseEvent e) {
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        Graphics g = img.getGraphics();
        g.setColor(Color.RED);  // SET A COLOR
        g.drawRect(1, 1, width - 2, height - 2);

        // DO SOMETHING UGLY
        g.setColor(Color.blue);
        Point p = e.getPoint();
        g.fillOval(p.x,p.y,5,5);

        g.dispose();
        repaint();
    }
    @Override
    public void paintComponent(Graphics g) {
         g.drawImage(img, 0, 0, null);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

Use a local BufferedImage on which you want to draw. Add a MouseMotionListener and implement the mouseDragged(MouseMotionEvent evt) method. In that method draw onto the BufferedImage by doing something like this:

// Assume img is your BufferedImage
Graphics g = img.getGraphics();
g.drawRect(evt.getX()-1, evt.getY()-1, 3, 3);
g.dispose();
// repaint your swing component
repaint();

And in your overrided paintComponent(Graphics g) method, draw like this:

g.drawImage(img, 0, 0, null);

Initialize your BufferedImage like this:

img = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_ARGB_PRE);
// Assuming `this` is your swing component
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287