0

Should I set a background with a central start button. When click on button "start" you must load a "bersaglio" made in another class. When I run it does not appear the background image but only the start button, I also tried to change the path of the image. Also, when I click on the button shows a "bersaglio". Where am I wrong?

Home class

public class Home extends JFrame implements ActionListener{
    JFrame frame;
    JButton b1;
    public Home(){
        frame= new JFrame();
        frame.setSize(200, 200);
        frame.setTitle("Bersaglio");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //aggiungo lo sfondo e il bottone
        b1=new JButton("START");
        Sfondo sfondo=new Sfondo();

        JPanel panelsecondo=new JPanel();
        panelsecondo.add(b1,BorderLayout.CENTER);

        sfondo.add(panelsecondo);
        frame.getContentPane().add(sfondo); 

        b1.addActionListener(this); //aggiungo ascoltatore

        frame.pack();
        frame.setVisible(true);
    }
    public void actionPerformed(ActionEvent e) {
        System.out.println("entra");
        //bersaglio
        Bersaglio bersaglio = new Bersaglio();
        frame.add(bersaglio);
        repaint();
    }
}   

Sfondo class

class Sfondo extends JPanel{
    Image img;
    public Sfondo(){
        img = Toolkit.getDefaultToolkit().createImage("\\Esdicembre\\EsVacanze\\sfondo");
        loadImage(img);

    }
    private void loadImage(Image img) {
        try {
            MediaTracker mt = new MediaTracker(this);
            mt.addImage(img, 0);
            mt.waitForID(0);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    protected void paintComponent(Graphics g) {
        //setOpaque(false);
        g.drawImage(img, 0, 0, null);
        super.paintComponent(g);
    }
}

class Bersaglio

public class Bersaglio extends JComponent implements MouseListener  {

Ellipse2D.Double circle;
Ellipse2D.Double circle1;
Ellipse2D.Double circle2;
Ellipse2D.Double circle3;
/*public void setup(){
    x=0;
    y=0;        
}
/*Color c;
int hight;
int weight;


public Circle() {
    super();
}
public Circle(int x,int y,int hight,int weight) {
    this.x=x;
    this.y=y;
    this.hight=hight;
    this.weight=weight;
} */
public void paintComponent(Graphics g){
    Graphics2D g2;
    g2 = (Graphics2D) g;
    g2.setColor(Color.BLUE);
    circle1 = new Ellipse2D.Double(30, 30, 100, 100);
    g2.fill(circle1);
    //c1.fillOval(30,30,100,100);

    //secondo cerchio medio
    g2.setColor(Color.RED);
    circle2 = new Ellipse2D.Double(50,50,60,60);
    g2.fill(circle2);
    //c1.fillOval(50,50,60,60);

    //terzo cerchio piccolo
    g2.setColor(Color.BLACK);
    circle3 = new Ellipse2D.Double(70,70,20,20);
    g2.fill(circle3);
    //c1.fillOval(70,70,20,20);
    addMouseListener(this);

    //scrivere qualcosa
    //g2.drawString("Ciao", 50, 100);
}

public void mouseClicked (MouseEvent e) {
    //x = e.getX();
    //y = e.getY();
     Point p = e.getPoint();
     if(circle3.contains(p)) {
         System.out.println("Circle3 contains point");
     }else{ 
        if(circle2.contains(p)) { 
            System.out.println("Circle2 contains point");
        }else {
            if(circle1.contains(p)) {
                 System.out.println("Circle1 contains point");
            }
        }
     }
     Graphics g = getGraphics();
     Graphics2D g2 = (Graphics2D) g;
     circle = new Ellipse2D.Double(p.getX(),p.getY(),10,10);
     g2.fill(circle);
     revalidate();

} 
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}

}

Edoardo
  • 31
  • 5
  • how the `Bersaglio ` look like? – Youcef LAIDANI Jan 08 '17 at 10:44
  • 2
    What do you expect to happen? Why? What actually happens? - For better help, post a [mcve]. Hot link to images in [this question](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Jan 08 '17 at 10:44
  • 2
    General tips: 1) `public class Home extends JFrame implements ActionListener{ JFrame frame;` should be `public class Home implements ActionListener{ JFrame frame;` 2) `g.drawImage(img, 0, 0, null);` would best be `g.drawImage(img, 0, 0, this);`, then the entire `loadImage` method is unnecessary. 3) `frame.pack();` makes `frame.setSize(200, 200);` redundant. **4) Use a `CardLayout`. At the moment the code is trying to add `Bersaglio bersaglio` in the same position as `sfondo`** 5) *"I need help."* Intuitively obvious. *"I am in the fourth year of high school."* Irrelevant. – Andrew Thompson Jan 08 '17 at 10:48
  • i have update the question – Edoardo Jan 08 '17 at 11:09
  • @AndrewThompson , yes, i would like to add and overwrite "bersaglio" in the same position as "sfondo" . – Edoardo Jan 08 '17 at 11:16
  • 1
    *'yes, i would like to add and overwrite "bersaglio" in the same position as "sfondo"'* So obviously you've researched the **bold** sentence immediately before the one pointing that out. **It is the solution.** ***Edit:*** No.. cancel that, I was too quick to comment & got it wrong. So the immediate problem is that the button covers the entire panel? Change the layout to a `new GridBagLayout()`.. But if you have further problems, look more closely into posting something others can work with - that MCVE that hot links to an image. – Andrew Thompson Jan 08 '17 at 11:40
  • I change the layout, and now my layout is CardLayout, but i don't see the wallpaper. I don't understand this: "But if you have further problems, look more closely into posting something others can work with - that MCVE that hot links to an image." @AndrewThompson – Edoardo Jan 16 '17 at 16:43
  • *"I don't understand this:"* Be specific about what you don't understand. I'm not explaining every single word. – Andrew Thompson Jan 17 '17 at 00:03
  • The wallpaper of class "Sfondo" I don't know how to apply into the frame. I did so: frame.setContentPane(new sfondo()); . But i don't see the wallpaper. @AndrewThompson – Edoardo Jan 17 '17 at 17:37

0 Answers0