0

I have two objects like this (two balls),but they are in two different JFrame.

How can I combine them in a single JFrame ?

I tried to put the other object in this class but the repaint() method give me some problems.

public class palla extends JFrame implements ActionListener
{

public JPanel contentPane;
private Image bufferVirtuale;
private Graphics offScreen;
private Timer tim = null;
private int xPallina = 0;
private int yPallina = 0;
private int diametroPallina = 40;
private int spostamento = 2;
private int Delay = 10;
private boolean destra,basso;

public palla()
{
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(500, 200, 500, 400);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);
    destra = true;
    basso = true;
    tim = new Timer(Delay,this);
    tim.start();
}

public void update(Graphics g)
{
    paint(g);
}

public void paint(Graphics g)
{
    bufferVirtuale = createImage(getWidth(),getHeight());
    offScreen = bufferVirtuale.getGraphics();
    Graphics2D screen = (Graphics2D) g;
    offScreen.setColor(new Color(254,138,22));
    offScreen.fillOval(xPallina,yPallina,diametroPallina,diametroPallina);
    screen.drawImage(bufferVirtuale,0,0,this);
    offScreen.dispose();
}

public void actionPerformed(ActionEvent e)
{

        if(basso)
        {
            if(yPallina > (this.getHeight()-45))
            {
                basso = false;
                yPallina -= spostamento;
            }
            else
            {
                yPallina += spostamento;
            }
        }
        else
        {
            if(yPallina < 25)
            {
                basso = true;
                yPallina += spostamento;
            }
            else
            {
                yPallina -= spostamento;
            }
        }
        //il controllo differente avviene a differenza del pannello server,se la pallina colpisce il margine destro. Mentre nel 
        //pannello server se la pallina colpiva il margine destro avveniva il trasferimento,qui invece la pallina deve rimbalzare
        if(!destra)//perciò se la pallina va verso sinistra
        {
            if(xPallina < 5)//se la pallina raggiunge il margine sinistro e arrivo comunicato è falso
            {
                destra = true;
                xPallina += spostamento;
            }
            else//se invece la pallina sta andando verso sinistra ma ancora non ha colpito il margine
            {
                xPallina -= spostamento;//viene diminuita la sua posizione di "spostamento"-pixel
                if(xPallina < 0)//se però raggiunge il margine sinistro
                {
                    destra = true;
                    xPallina += spostamento;
                }
            }
        }
        else//se invece la pallina va a destra
        {
            if(xPallina > (this.getWidth()-45))//si controlla se colpisce il margine destro
            {
                destra = false;//si fa cambiare direzione,va a sinistra
                xPallina -= spostamento;//e si diminuisce le coordinate delle X
            }
            else//altrimenti se non ha colpito ancora il margine si aumento le coordinate
            {
                xPallina += spostamento;
            }
        }

    repaint();
}

}

Omar Said
  • 9
  • 1
  • 2
    1. It is a bit unclear what you are asking (you mention combining two things but it seems only one is posted) 2. `but the repaint() method give me some problems.` What problems? 3. I highly recommend drawing to a lightweight component (eg `JPanel`) and override it's `paintComponent` method. – copeg Sep 16 '16 at 18:36
  • The two objects are the same. When mouseMoved i want that only one object is repaint while when i call the repaint method at mouseMoved it repaint the two objects. I don't know if is clear now. – Omar Said Sep 16 '16 at 18:41
  • `I don't know if is clear now` Sorry, but not really. A bit more confused now with the mention of `mouseMoved`, as nowhere in your code is a `MouseListener`. – copeg Sep 16 '16 at 18:55
  • Final question: how draw two different objects in a jframe – Omar Said Sep 16 '16 at 18:58
  • 1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 2) See [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831) for a problem I could no longer be bothered fixing. – Andrew Thompson Sep 17 '16 at 00:30
  • *"Final question:"* SO is a Q&A site, not a help desk. Other questions should be asked on other (appropriately titled) question threads. – Andrew Thompson Sep 17 '16 at 00:31

1 Answers1

2

Don't override update() and don't override paint() on a JFrame. That is not the way custom painting is done.

how draw two different objects in a jframe

Custom painting is done by overriding the paintCoponent() method of a JPanel and then you add the panel to the frame. Read the section from the Swing tutorial on Custom Painting for working examples to get you started.

To paint multiple objects on the same panel you can check out Custom Painting Approaches for the two common ways to do this.

camickr
  • 321,443
  • 19
  • 166
  • 288