Hi there!
I have a problem with programming a task, where user clicks on a button in one frame. And that event triggers that an image is drawn on JPanel, which is located in other frame.
Here is the code for class where I extend JPanel. This class is used to create panel in main JFrame:
class RSlika extends JPanel { private Image img; private int x, y; public RSlika(Image img, int x, int y) { this.img = img; this.x = x; this.y = y; } public void paintComponent(Graphics g) { super.paintComponent(g); Dimension d = getSize(); Insets i = getInsets(); g.drawImage(img, i.left + 2, i.top + 2, d.width - i.left - i.right - 4, d.height - i.top - i.bottom - 4, this); } }
In my main class StormAlarm that extends JFrame I create an object for upper class:
RSlika leva = new RSlika(img); leva.setPreferredSize(new Dimension(700, 0)); leva.setBorder(zamik1); leva.setLayout(new BoxLayout(leva, BoxLayout.X_AXIS)); addBorder1(barvaDebelina, leva); getContentPane().add(leva, BorderLayout.WEST);
Now I have another class that extends JFrame, this class is named selectingPicture and is used to create another frame, where user than has an option to select button which triggers an event, where image is changed in my first frame. I will post actionPreformed function where the main problem arises:
public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if(source == izhod) { this.dispose(); } else if(source == radarCRO) { StormAlarm objekt2 = new StormAlarm(); objekt2.getImageCRO(); objekt2.changePicture(); } } }
Here I create an object from class StormAlarm, then I call function getImage, where I get an image from URL adress. That is all working perfectly, but when I am calling function changePicture, which is defined in StormAlarm:
public void changePicture() { RSlika leva = new RSlika(img); leva.setPreferredSize(new Dimension(700, 0)); leva.setBorder(zamik1); leva.setLayout(new BoxLayout(leva, BoxLayout.X_AXIS)); addBorder1(barvaDebelina, leva); getContentPane().add(leva, BorderLayout.WEST); }
Where as you can see I create again an object RSlika, but this time parameter is other picture. So now the new picture should be painted over old one, but guess what, when I click the button nothing happens. Now I have doubts in myself using object StormAlarm objekt2 properly?