Note that there are some explanatory texts on larger screens.

plurals
  1. POResizing added painted components and strange swing behaviors
    primarykey
    data
    text
    <p>I have this problem bothering me for days. I'm making a special paint program. I made a JPanel, and added custom jComponents which are painted using the paint(..) method. The problem is, whenever I resize the window, all the added components "disappear" (or just don't paint) so I end up with a empty frame.</p> <p>Also i noticed some strange behaviors of swing when using this method. I have added comments to the code describing this problem.</p> <pre><code>package simple; import java.awt.*; import java.awt.event.*; import java.util.ArrayList; import javax.swing.*; public class SimpleFrame extends JFrame { JPanel paintArea; SimpleCanvas c1; SimpleCanvas c2; ArrayList&lt;SimpleCanvas&gt; list; public static void main(String[] args) { SimpleFrame frame = new SimpleFrame(); } public SimpleFrame() { super("Test"); setSize(600,500); setDefaultCloseOperation(EXIT_ON_CLOSE); //The panel to which my SimpleCanvas objects are added paintArea = new JPanel(); paintArea.setPreferredSize(new Dimension(600, 500)); paintArea.addMouseListener(new paintAreaMouseEvents()); getContentPane().add(paintArea, BorderLayout.CENTER); setVisible(true); paintArea.setVisible(true); //A list to hold all the objects together list = new ArrayList&lt;SimpleCanvas&gt;(10); //The same as in class paintAreaMouseEvent, but doesnt work SimpleCanvas c = new SimpleCanvas(); c.setBounds(60, 100, 100, 50); list.add(c); paintArea.add(list.get(list.size() - 1)); paintArea.repaint(); } //When you click the mouse, it makes an oval class paintAreaMouseEvents extends MouseAdapter { @Override //This does work. public void mouseClicked (MouseEvent me) { SimpleCanvas c = new SimpleCanvas(); c.setBounds(me.getX() - 50, me.getY() - 25, 100, 50); list.add(c); paintArea.add(list.get(list.size() - 1)); paintArea.repaint(); } } } </code></pre> <p>And here is the SimpleCanvas class</p> <pre><code>package simple; import java.awt.*; import javax.swing.JComponent; public class SimpleCanvas extends JComponent { public void paint(Graphics g) { super.paint(g); g.setColor(Color.BLUE); g.fillOval(0, 0, 100, 50); } } </code></pre> <p>Thanks :)</p> <p>BTW: Just wanted to say this site is amazing. I came here a lot while using Google, and now I finally decided to make an account.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload