0

I am a bit stuck on my homework in java for this week. The problem sounds like this: "Write a program that lets the user click on the panel to dynamically create points. Initially, the panel is empty. When a panel has two or more points, highlight the pair of closest points. Whenever a new point is created, a new pair of closest points is highlighted. Display the points using small circles and highlight the points using filled circles. (Hint: Store the points in an ArrayList)"

In the code under I have tried to make an ArrayList of the CircleMouse class, but I do not know if this is the "correct" way to do it? Also, how do I display the circles in the panel when the are in the ArrayList?

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;

import javax.swing.*;

class ClosestPairOfPoints extends JFrame {

  /** FRIE VARIABLER **/
  int x, y;

  /*** ARRAYLIST MED ALLE SIRKLENE ***/
  public ArrayList<CircleMouse> arrCircle = new ArrayList <CircleMouse>();

  public ClosestPairOfPoints() {
    /*** PANEL FOR Å HOLDE PÅ SIRKLENE ***/
    JPanel circlePanel = new JPanel();
    circlePanel.setSize(500, 400);
    circlePanel.setLocation(0, 0);
    circlePanel.setBackground(Color.PINK);

    /*** HENTER CIRCLEMOUSE KLASSEN INN ***/
    CircleMouse circleMouse = new CircleMouse();
    circlePanel.addMouseListener(circleMouse);
    circlePanel.addMouseMotionListener(circleMouse);

    /*** LEGGER PANELET INN I FRAMEN ***/
    add(circlePanel);

}

/*** CIRCLEMOTION KLASSEN ***/
class CircleMouse extends JPanel implements MouseListener, MouseMotionListener {

    public void paintComponent (Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.BLUE);
        g.drawOval(x, y, 15, 15);
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseMoved(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseClicked(MouseEvent e) {
        x = e.getX();
        y = e.getY();
        repaint();


    }

    @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) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }

}

public static void main(String[] args) {
    ClosestPairOfPoints frame = new ClosestPairOfPoints();
    frame.setTitle("Øving 1 - Oppgave 1: Closest Pair Of Points");
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 400);
    frame.setVisible(true);
}

}

Roman C
  • 49,761
  • 33
  • 66
  • 176

1 Answers1

0

I have tried to make an ArrayList of the CircleMouse class

Doesn't sound quite right. You should have an ArrayList of Point. Each time mouseClicked is called, you should add the Point from the MouseEvent to it, then your paintComponent method, you should iterate over this list and paint the "dots"

For examples...

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • That sounds more correct to me. Do you have any examples, or references? I will look more into the code and see if I get somewhere with it. Thanks for you suggestion! – FredrikBakken Jan 22 '14 at 19:51