0

Well, I have done the following coding:-

import java.awt.*;
import java.awt.event.*;

public class line extends Component {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int len;
//	private int nx,ny;
	private int relx,rely;
	boolean pressed = false;
	boolean endp = false;
	boolean resized=false;
	ActionListener actionListener = null; 
	
	public line() {
		this(5);
	}
	
	public line(int len) {
		if (len < 0) {
			throw new IllegalArgumentException("Illegal len: " + len);
		}
		this.len = len;
		addMouseListener(new ClickAdapter());
		setSize(getPreferredSize());
	}
	
	public void addActionListener(ActionListener l) {
		actionListener = AWTEventMulticaster.add(actionListener, l);
	}
	
	public void removeActionListener(ActionListener l) {
		actionListener = AWTEventMulticaster.remove(actionListener, l);
	}
	
	private final class ClickAdapter extends MouseAdapter {
		
		
		public void mousePressed(MouseEvent e) {
//			nx=e.getX();
//			ny=e.getY();
//			
			if (isEnabled()) {
				pressed = true;
				
				repaint();
			}
		}
		
		public void mouseReleased(MouseEvent e) {
			
			if (isEnabled()) {
				pressed = false;
				
				relx=e.getX();
				rely=e.getY();
				
				resized=true;
				
				repaint();
				if (actionListener != null) {
					
					
					actionListener.actionPerformed(
							new ActionEvent(line.this,
									ActionEvent.ACTION_PERFORMED, ""));
				}
			}
		}
	}
	
	public Dimension getPreferredSize() {
		int side = len * 10;
		return new Dimension(side, side);
	}
	
	public Dimension getMinimumSize() {
		return getPreferredSize();
	}
	
	public Dimension getMaximumSize() {
		return getPreferredSize();
	}
	
	public void paint(Graphics g) {
		super.paint(g);
		Graphics2D g2d = (Graphics2D)g;
		
		if(resized)
		{
			g2d.clearRect(0,0,len*2,len*2);
			g2d.setStroke(new BasicStroke(2));
			g2d.setColor(Color.BLUE);
			g2d.drawRect(0,0,relx,rely);
			
			System.out.println(relx + " " + rely);

		}
		else{
			if (isEnabled()) {
				if (pressed) {
					g2d.setStroke(new BasicStroke (5));
					g2d.setColor(Color.white);
					g2d.drawRect(0, 0, len*2, len*2);
					g2d.setColor(Color.black);
					g2d.drawRect(0, 0, len*2, len*2);
				}
				else {
					g2d.setColor(Color.black);
					g2d.drawRect(0, 0, len*2, len*2);
				}
			}
			else {
				g2d.setColor(Color.gray);
				g2d.drawRect(0, 0, len*2, len*2);
			}
		}
	}
			
//			g2d.setColor(Color.YELLOW);
//			g2d.drawLine(0,0,relx,rely);
			
			
			public boolean contains(int x, int y) {
				//distance = square root of (deltaX squared + deltaY squared)
				int deltax = x - len;
				int deltay = y - len;
				double distance = Math.sqrt(Math.pow(deltax, 2) + Math.pow(deltay, 2));
				return (distance <= len);
			}
			
			public static void main(String[] args) {
				final line cb1, cb2, cb3, cb4, cb5;
				Frame f = new Frame();
				f.addWindowListener(
						new WindowAdapter() {
							public void windowClosing(WindowEvent e) {
								System.exit(0);
							}
						}
				);
				f.setLayout(new FlowLayout());
				f.add(cb1 = new line(20));
				f.add(cb2 = new line(20));
				f.add(cb3 = new line(20));
				f.add(cb4 = new line(20));
				f.add(cb5 = new line(20));
				ActionListener al = new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						Object o = e.getSource();
						String msg;
						if (o.equals(cb1)) {
							msg = "One";
						}
						else if (o == cb2) {
							msg = "Two";
						}
						else if (o == cb3) {
							msg = "Three";
						}
						else if (o == cb4) {
							msg = "Four";
						}
						else if (o == cb5) {
							msg = "Five";
						}
						else {
							msg = "Huh?" + o;
						}
						System.out.println("Button: " + msg);
					}
				};
				cb1.addActionListener(al);
				cb2.addActionListener(al);
				cb3.addActionListener(al);
				cb4.addActionListener(al);
				cb5.addActionListener(al);
				cb5.setEnabled(false);
				f.pack();
				f.setVisible(true);
//				f.show();
			}
		}

What i want to do is that the newly drawn rectangles i.e. the blue colour ones.....they should also have the property of being redrawn....i.e.perhaps these new rectangles should be the objects of teh existing class...how do i go about it???

2
Contributors
1
Reply
2
Views
11 Years
Discussion Span
Last Post by server_crash
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.