Method that creates rectangles on a frames but doesn't show only the last one.
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Stunt_Master
Posted On:   Tuesday, December 7, 2004 03:16 AM

How can I create a paint method in a class (MyComponent... for example) that creates a rectangle in paint method and this class(through it's constructor) to be instantiated in another class with main method and create diferent rectangles for every instance but to show all rectangles created, not only the last one.

Maybe it should be something like a buffer to save the existing image of the frame and to repaint it with the new instance( new rectangle created)

Re: Method that creates rectangles on a frames but doesn't show only the last one.

Posted By:   Craig_Wood  
Posted On:   Wednesday, December 15, 2004 11:53 PM


import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.List;
import java.util.*;
import javax.swing.event.*;

public class DrawingRectangles
{
public static void main(String[] args)
{
Frame f = new Frame();
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
f.add(new RectangleDrawingPanel());
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}

class RectangleDrawingPanel extends Panel
{
List shapes;
Rectangle newRect;
int minX, minY, maxX, maxY;
Point offscreen;
boolean showClip;
RectangleMaker maker;

public RectangleDrawingPanel()
{
shapes = new ArrayList();
newRect = new Rectangle();
offscreen = new Point(-1, -1);
resetClipBounds(offscreen);
showClip = false;
maker = new RectangleMaker(this);
addMouseListener(maker);
addMouseMotionListener(maker);
}

public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
for(Shape s : shapes)
g2.draw(s);
if(maker.dragging)
g2.draw(newRect);
if(showClip)
{
g2.setPaint(Color.red);
g2.fillOval(minX - 2, minY - 2, 4, 4);
g2.fillOval(minX - 2, maxY - 2, 4, 4);
g2.fillOval(maxX - 2, minY - 2, 4, 4);
g2.fillOval(maxX - 2, maxY - 2, 4, 4);
}
}

public void addShape()
{
Rectangle r = (Rectangle)newRect.clone();
shapes.add(r);
resetClipBounds(offscreen);
repaint();
}

public void clear()
{
shapes.clear();
newRect.setFrame(0, 0, 0, 0);
resetClipBounds(offscreen);
repaint();
}

public void setRectangle(Point p1, Point p2)
{
newRect.setFrameFromDiagonal(p1, p2);
int x = p2.x >= p1.x ? p1.x - 1 : p2.x - 1;
int y = p2.y >= p1.y ? p1.y - 1 : p2.y - 1;
int w = Math.abs(p1.x - p2.x) + 2;
int h = Math.abs(p1.y - p2.y) + 2;
updateClipBounds(x, y, w, h);
repaint(minX, minY, maxX, maxY);
}

private void updateClipBounds(int x, int y, int w, int h)
{
if(x < minX) minX = x;
if(y < minY) minY = y;
if(x + w > maxX) maxX = x + w;
if(y + h > maxY) maxY = y + h;
}

protected void resetClipBounds(Point p)
{
minX = maxX = p.x;
minY = maxY = p.y;
}
}

class RectangleMaker extends MouseInputAdapter
{
RectangleDrawingPanel drawPanel;
Point start;
boolean dragging;

public RectangleMaker(RectangleDrawingPanel rdp)
{
drawPanel = rdp;
dragging = false;
}

public void mousePressed(MouseEvent e)
{
// double click clears the drawing surface
if(e.getClickCount() >= 2)
{
dragging = false;
drawPanel.clear();
return;
}
Point p = e.getPoint();
start = p;
drawPanel.resetClipBounds(p); // start new clip
dragging = true;
}

public void mouseReleased(MouseEvent e)
{
dragging = false;
if(start.distance(e.getPoint()) > 5)
drawPanel.addShape();
}

public void mouseDragged(MouseEvent e)
{
if(dragging)
drawPanel.setRectangle(start, e.getPoint());
}
}
About | Sitemap | Contact