Results 1 to 7 of 7
Thread: Clicking on an oval in JPanel
- 03-16-2010, 08:41 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 26
- Rep Power
- 0
Clicking on an oval in JPanel
I am making a paint applet, which draws shapes on a 'canvas' by storing shapes into an arraylist (using the Graphics class).
I need to be able te erase complete shapes by clicking them with the eraser tool.
I managed to make this work for rectangles, filled rectangles and lines. I am not managing to do this with ovals. This because the radius is not equal along the entire circumference. How can I calculate whether a user clicked withing the oval, or on the (variable) stroke of an open oval?
For an example this is what the code for the filled rectangle:
Java Code:// p1 and p2 are startpoint and endpoint (class vars). // p is the point clicked by the user // iMargin is the amount of pixel the user is allowed to miss the shape by (class var). private boolean fillRectRaak(Point p) { if(p.x <= this.p2.x + iStroke + iMargin && p.x >= this.p1.x - iStroke - iMargin) { if(p.y <= this.p2.y + iStroke + iMargin && p.y >= this.p1.y - iStroke - iMargin) { return true; } } return false; }
- 03-16-2010, 09:10 PM #2
http://java.sun.com/javase/6/docs/ap...Ellipse2D.html
the method "contains(...)"My Hobby Project: LegacyClone
- 03-16-2010, 09:51 PM #3
Member
- Join Date
- Nov 2009
- Posts
- 26
- Rep Power
- 0
I am not using the java.awt.geom. I am using the Graphics class. When trying to use a Ellipse2D.Double it tells me that the method Double(double, double, double, double) is undefined for the type Ellipse2D.
Is there a way to continue using the Graphics class or can someone help me implement the Ellipse2D class?
-
If you can, you should post the code that doesn't work, that's throwing the errors, and we can then help point out what's wrong. Also, if you can post a small compilable (or attempt at a compilable) program that we can compile, test and possibly run, then we could likely get you focused help a lot quicker. Much luck!
- 03-16-2010, 10:21 PM #5
Member
- Join Date
- Nov 2009
- Posts
- 26
- Rep Power
- 0
That is the part that doesn't work.Java Code:switch (iToolID) { case PEN_TOOL: ; break; case LINE_TOOL: g.drawLine(p1.x, p1.y, p2.x, p2.y) ; break; case RECT_TOOL: g.drawRect(xy.x, xy.y, wh.width, wh.height) ; break; case FILL_RECT_TOOL: g.fillRect(xy.x, xy.y, wh.width, wh.height) ; break; //case OVAL_TOOL: g.drawOval(xy.x, xy.y, wh.width, wh.height) ; break; case OVAL_TOOL: Ellipse2D.Double(2.0,2.0,2.0,2.0) ; break; case FILL_OVAL_TOOL: g.fillOval(xy.x, xy.y, wh.width, wh.height) ; break; case TEXT_TOOL: g.drawString(s, xy.x, xy.y) ; break; }
A link to my complete project till now:
Download Schets.rar
The class that creates an object of a shape is "SchetsElementen". Most of the variable names are in Dutch, I can translate any words that give problems.
-
You need to add a new Ellipse2D into a List<Shape> and then iterate through this list in the paintComponent method drawing each Shape as you go. For instance, here's a simple example that draws an Ellipse2D with the left mouse button and erases the most recent shape with the right button:
Java Code:import java.awt.BasicStroke; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point; import java.awt.RenderingHints; import java.awt.Shape; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.geom.Ellipse2D; import java.util.ArrayList; import java.util.List; import javax.swing.*; @SuppressWarnings("serial") public class ShapeEg extends JPanel { private List<Shape> shapeList = new ArrayList<Shape>(); private Shape tempShape = null; public ShapeEg() { setBackground(Color.white); setPreferredSize(new Dimension(500, 500)); MouseAdapter adapter = new MyMouseAdapter(); addMouseListener(adapter); addMouseMotionListener(adapter); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(Color.lightGray); if (tempShape != null) { g2.draw(tempShape); } g2.setColor(Color.blue); g2.setStroke(new BasicStroke(4)); for (Shape s : shapeList) { g2.draw(s); } } private class MyMouseAdapter extends MouseAdapter { Point start = null; Point end = null; @Override public void mousePressed(MouseEvent me) { if (me.getButton() == MouseEvent.BUTTON1) { start = me.getPoint(); } // else if right mouse button pressed and there are shapes // held in our shape list -- delete a shape if the button presses // on top of one. else if (me.getButton() == MouseEvent.BUTTON3 && shapeList.size() > 0) { // iterate backwards to delete most recent shape for (int i = shapeList.size() - 1; i >= 0; i--) { if (shapeList.get(i).contains(me.getPoint())) { shapeList.remove(i); repaint(); break; } } } } @Override public void mouseDragged(MouseEvent me) { if (start == null) { return; } end = me.getPoint(); int x = Math.min(start.x, end.x); int y = Math.min(start.y, end.y); int w = Math.abs(start.x - end.x); int h = Math.abs(start.y - end.y); tempShape = new Ellipse2D.Double(x, y, w, h); repaint(); } @Override public void mouseReleased(MouseEvent me) { if (me.getButton() == MouseEvent.BUTTON1) { end = me.getPoint(); int x = Math.min(start.x, end.x); int y = Math.min(start.y, end.y); int w = Math.abs(start.x - end.x); int h = Math.abs(start.y - end.y); shapeList.add(new Ellipse2D.Double(x, y, w, h)); start = null; end = null; tempShape = null; repaint(); } } } private static void createAndShowGUI() { JFrame frame = new JFrame("ShapeEg Application"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new ShapeEg()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }Last edited by Fubarable; 03-16-2010 at 11:26 PM.
- 03-17-2010, 12:08 AM #7
Member
- Join Date
- Nov 2009
- Posts
- 26
- Rep Power
- 0
Similar Threads
-
Double-clicking .jar files?
By StokedOnMe in forum New To JavaReplies: 9Last Post: 04-13-2011, 04:06 AM -
Auto Clicking Application
By dpedroia15 in forum New To JavaReplies: 6Last Post: 03-02-2010, 09:35 PM -
How to alternate oval drawn by Mouse click?
By ntagrafix in forum New To JavaReplies: 3Last Post: 12-04-2009, 05:06 AM -
Java3D: Clicking and getting coordinates?
By seabhcan in forum Advanced JavaReplies: 0Last Post: 01-11-2008, 02:46 PM -
OVal 1.10
By JavaBean in forum Java SoftwareReplies: 0Last Post: 11-03-2007, 06:00 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks