Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Drawing preview rectangle inside MouseListener. How migrate into update()?

843807Jul 8 2009 — edited Jul 10 2009
Hi folks.


I have a little applet that's painting a filled rectangle based on mouse interaction. When pressed and dragged it displays a red outline as preview. When finally the mouse button is released the rectangle is drawn filled. So the code works fine, but I've read that all painting code should be inside update() or paint(). So, how can I get conform (e.g. migrate the painting code into update() ) and have the same application behavior?

But first, here is the code:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;

import javax.swing.event.MouseInputAdapter;


public final class A extends Applet {
	private int x, y, x2, y2, sx, sy;
	private Graphics g;

	private class B extends MouseInputAdapter {
		public void mousePressed(MouseEvent e) {
			x = x2 = sx = e.getX();
			y = y2 = sy = e.getY();
			g.setXORMode(Color.WHITE);
			g.drawRect(x,y,0,0);
			//getAppletContext().showStatus("pressed x "+x+" y "+y);
		}

		public void mouseDragged(MouseEvent e) {
			g.drawRect(sx, sy, Math.abs(x2-x), Math.abs(y2-y));
			x2 = e.getX();
			y2 = e.getY();
			sx = x<x2?x:x2;
			sy = y<y2?y:y2;
			g.drawRect(sx, sy, Math.abs(x2-x), Math.abs(y2-y));
			getAppletContext().showStatus("x-size="+  (x2-x)  +"  y-size="+  (y2-y));
		}

		public void mouseReleased(MouseEvent e) {
			g.setPaintMode();
			x2 = e.getX();
			y2 = e.getY();
			sx = x<x2?x:x2;
			sy = y<y2?y:y2;
                        g.drawRect(sx, sy, Math.abs(x2-x), Math.abs(y2-y));
			g.setColor(Color.YELLOW);
			g.fillRect(sx+1, sy+1, Math.abs(x2-x)-1, Math.abs(y2-y)-1);
			g.setColor(Color.RED);
		}

		public void mouseMoved(MouseEvent e) {
			getAppletContext().showStatus("x="  +e.getX()+  "  y="  +e.getY());
		}
	}

	public void init() {
		setBackground(Color.WHITE);
		B b = new B();
		addMouseListener(b);
		addMouseMotionListener(b);
		g = getGraphics();
		g.setColor(Color.RED);
		setSize(500, 500);
		//System.out.println(getSize().getWidth());
	}
}
To solve I've tried to introduce a status variable, so my first try was:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;

import javax.swing.event.MouseInputAdapter;


public final class A extends Applet {

	private int x, y, x2, y2, sx, sy,t;
	//private Graphics g;


	public void update(Graphics g) {

		g.setColor(Color.RED);
		switch (t) {
		case 1:
			g.setXORMode(Color.WHITE);
			g.drawRect(x,y,0,0);
			break;
		case 2:
			g.drawRect(sx, sy, Math.abs(x2-x), Math.abs(y2-y));
			break;
		case 3:
			g.drawRect(sx, sy, Math.abs(x2-x), Math.abs(y2-y));
			getAppletContext().showStatus("x-size="+  (x2-x)  +"  y-size="+  (y2-y));
			break;
		default:
			g.setPaintMode();
		        g.drawRect(sx, sy, Math.abs(x2-x), Math.abs(y2-y));
		        g.setColor(Color.YELLOW);
		        g.fillRect(sx+1, sy+1, Math.abs(x2-x)-1, Math.abs(y2-y)-1);
		        g.setColor(Color.RED);
		}
	}	

	private class B extends MouseInputAdapter {

		public void mousePressed(MouseEvent e) {

			x = x2 = sx = e.getX();
			y = y2 = sy = e.getY();
			t=1;
			repaint();
                        //getAppletContext().showStatus("pressed x "+x+" y "+y);
		}

		public void mouseDragged(MouseEvent e) {
			t=2;
			repaint();
			x2 = e.getX();
			y2 = e.getY();
			sx = x<x2?x:x2;
			sy = y<y2?y:y2;
			t=3;
			repaint();

		}

		public void mouseReleased(MouseEvent e) {
			x2 = e.getX();
			y2 = e.getY();
			sx = x<x2?x:x2;
			sy = y<y2?y:y2;
			t=4;
			repaint();
		}

        	public void mouseMoved(MouseEvent e) {
			getAppletContext().showStatus("x="+  e.getX()  +"  y="+  e.getY());
		}
	}

	public void init() {
		setBackground(Color.WHITE);
		B b = new B();
		addMouseListener(b);
		addMouseMotionListener(b);
		setSize(500, 500);
		//System.out.println(getSize().getWidth());
	}
}
But that's not the same as initially. I think this has to deal with
"[...] NOTE: If multiple calls to repaint() occur on a component before the initial repaint request is processed, the multiple requests may be collapsed into a single call to update(). [...]" from "Painting in AWT and Swing" Tutorial.

I hope for any suggestions.

Best regards.

Edited by: qwerdenker on Jul 8, 2009 12:05 PM

Edited by: qwerdenker on Jul 8, 2009 12:22 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 7 2009
Added on Jul 8 2009
5 comments
405 views