More JPanel and background image

58 views Asked by At

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) {}

}

0

There are 0 answers