i cant get this problem solved i need help the question is to write a program th
ID: 3624676 • Letter: I
Question
i cant get this problem solvedi need help
the question is to write a program that draws a fixed circle centred at (100,60) with radius 50. whenever a mouse is moved, display the message indicating whether the mouse point is inside the circle, as shown in this link
(http://books.google.ca/books?id=4APjw-5wxL4C&pg=PA565&lpg=PA565&dq=write+a+program+that+draws+a+fixed+circle+centred+at+(100,60)+with+radius+50.+whenever+a+mouse+is+moved,+display+the+message+indicating+whether+the+mouse+point++is+inside+the+circle.&source=bl&ots=oY3huebRyl&sig=l47nq5wn8o8lYJOz9X32P4LSJP8&hl=en&ei=FIqSTeGUOrOM0QG15vDMBw&sa=X&oi=book_result&ct=result&resnum=1&ved=0CBYQ6AEwAA#v=onepage&q&f=false)
this what i wrote:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
public class HW16_19 extends JFrame
{
PaintPanel paintPanel = new PaintPanel();
public HW16_19()
{
add(paintPanel);
}
static class PaintPanel extends JPanel
{
Circle1 c1 = new Circle1(100,60,50);
boolean isInside = false;
Point mousePoint = new Point(0, 0);
public PaintPanel()
{
this.addMouseMotionListener(new MouseAdapter()
{
public void mouseMoved(MouseEvent e)
{
if(c1.contains())
isInside = true;
else
isInside = false;
mousePoint.x = e.getX();
mousePoint.y = e.getY();
repaint();
}
});
}
/** Paint message */
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawOval(160,60,100,100);
g.drawString(isInside ?"Mouse point is in the circle":"Mouse point is not in the circle",mousePoint.x, mousePoint.y);
}
}
public static void main(String[] args) {
JFrame frame = new HW16_19();
frame.setTitle("t");
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Explanation / Answer
I took the liberty of changing the font color when the mouse is inside the circle.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
class HW16_19 extends JFrame
{
PaintPanel paintPanel = new PaintPanel();
public HW16_19()
{
add(paintPanel);
}
static class PaintPanel extends JPanel
{
private int mousePointX, mousePointY;
private boolean isInside;
public PaintPanel()
{
this.addMouseMotionListener(new MouseAdapter()
{
public void mouseMoved(MouseEvent e)
{
mousePointX = e.getX();
mousePointY = e.getY();
// check if within circle
isInside = ((mousePointX-210)*(mousePointX-210) + (mousePointY-110)*(mousePointY-110) <= 50*50);
repaint();
}
});
}
/** Paint message */
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawOval(160,60,100,100);
g.setColor(isInside? Color.red : Color.black);
g.drawString(isInside ?"Mouse point is in the circle":"Mouse point is not in the circle",mousePointX, mousePointY);
}
}
public static void main(String[] args) {
JFrame frame = new HW16_19();
frame.setTitle("t");
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.