I want to program an application that lets you draw circles with a mouse click on the left side of a JFrame
, and all the points are getting "mirrored" to the right side. The first problem I encountered was that when I try to implement this draw-mechanic in my frame, no circles appear.
public class Application{
int x,y;
private JPanel container;
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Application().gui();
}
});
}
public void gui()
{
int height = 250;
int width = 700;
JFrame jframe = new JFrame();
container = new JPanel();
container.setLayout(new BorderLayout());
container.add(new DrawCircle(), BorderLayout.WEST);
container.setVisible(true);
jframe.add(container);
//jframe.add(new DrawCircle());
jframe.setSize(500,700);
jframe.setVisible(true);
jframe.setTitle("Title");
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setResizable(false);
}
}
It works (on the whole frame) when I use container.add(new DrawCircle)
but if I want to add constraints, it doesn't.
Here is the circle class:
public class DrawCircle extends JPanel implements MouseListener
{
ArrayList<Point> p = new ArrayList<Point>();
public DrawCircle()
{
addMouseListener(this);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
for(Point point : p)
{
g.fillOval(point.x,point.y,30,30);
}
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
p.add(new Point(e.getY(), e.getX()));
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseDragged(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent arg0) {
}
}