i have the following problem. i want to paint something on a custom jpanel and then set custom buttons on a absolute positions. these buttons should listen on clicking and then provide their position. but everything i tried is not working. the buttons are not displaying when i use add();
public class IslePanel extends JPanel {
private static final long serialVersionUID = 1L;
private Color backgroundColor = new Color(90, 74, 66);
private Color strokeColor = new Color(254, 254, 254);
private Color lightGrayColor = new Color(220, 220, 220);
/**
* Constructor of the GameView
*
* @param game
* @param controller
*/
public IslePanel() {
setBackground(backgroundColor);
SiteButton sb = new SiteButton();
sb.setXPos(100);
sb.setYPos(100);
sb.setSiteRadius(20);
add(sb);
//example: i plan to draw a circle formed button on absolute Position (100,100) radius 20
}
/**
* paintComponent-method for ALL drawing functions
*
* @param Graphics
* g
*
**/
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
try {
drawTile(g, tile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
sitebutton superclass:
public abstract class CustomButton extends JPanel implements MouseListener {
private Vector listeners = null;
boolean hit = false;
public CustomButton(String title) {
super();
this.title = title;
listeners = new Vector();
addMouseListener(this);
}
// public Dimension getPreferredSize() {
// return new Dimension(120, 80);
// }
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
fireEvent(new ActionEvent(this, 0, title));
System.out.println("bauen");
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void addActionListener(ActionListener listener) {
listeners.addElement(listener);
}
public void removeActionListener(ActionListener listener) {
listeners.removeElement(listener);
}
private void fireEvent(ActionEvent event) {
for (int i = 0; i < listeners.size(); i++) {
ActionListener listener = (ActionListener) listeners.elementAt(i);
listener.actionPerformed(event);
}
;
}
}
the button class:
public class SiteButton extends CustomButton {
/**
*
*/
private static final long serialVersionUID = 1L;
private double xpos,ypos;
private Site site;
private int siteRadius;
@Override
public void paintComponent(Graphics g) {
// super.paintComponent(g);
Graphics2D g2D = (Graphics2D) g;
g2D.setColor(Color.BLACK);
g2D.fillOval((int)xpos-siteRadius/2, (int)ypos-siteRadius/2, siteRadius, siteRadius);
}
@Override
public void mouseClicked(MouseEvent e) {
System.out.println(""+xpos + ypos);
}