Results 1 to 10 of 10
- 09-12-2014, 07:11 PM #1
Initial repaint() fails to work, have to resize window to kickstart things?
Hello,
I am learning drawing, so I have a very basic program (see below) wherein a button click adds a rectangle to an array that is looped in the paintComponent() method of the custom JPanel.
The issue I'm having is that this morning (oh yes) somehow clicking on the button doesn't draw it (repaint doesn't work it seems) but if I resize the main application window then it starts kicking in, the drawing resumes, and any subsequent button click promptly results in another rectangle being drawn.
You'll see that I added repaint() in places where they're probably not necessary, sorry about that, I was shooting in the dark
You probably encountered this, its sort of a gotcha I'm not privy to yet, I guess...
Thanks!
Java Code:package views; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Rectangle; import java.util.ArrayList; import java.util.Iterator; import javax.swing.*; import java.awt.Toolkit; public class appMainWindow extends JFrame { class PdfLocation { public double xPos; public double yPos; } class DrawCanvas extends JPanel { @Override public void paintComponent(Graphics g) { super.paintComponent(g); for (PdfLocation p : PdfLocations) { g.drawRect((int)p.xPos, (int)p.yPos, 35, 20); repaint(); } } } public void AddRectangle() { PdfImagesCount++; lblPdfcount.setText(Integer.toString(PdfImagesCount)); PdfLocation rect = new PdfLocation(); if (PdfLocations.isEmpty() == false) { PdfLocation spot = PdfLocations.get(PdfLocations.size() - 1); rect.xPos = spot.xPos + 45; rect.yPos = 10; } else { rect.xPos = 10; rect.yPos = 10; } PdfLocations.add(rect); repaint(); } private JFrame frame; public ArrayList<PdfLocation> PdfLocations = new ArrayList<PdfLocation>(); public int PdfImagesCount = 0; public static final int CANVAS_HEIGHT = 700; public static final int CANVAS_WIDTH = 1000; private DrawCanvas canvas; private JLabel lblPdfcount; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { appMainWindow window = new appMainWindow(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public appMainWindow() { // Set up a custom drawing JPanel canvas = new DrawCanvas(); canvas.setBackground(Color.WHITE); canvas.setBounds(150, 25, CANVAS_WIDTH, CANVAS_HEIGHT); initialize(); } private void initialize() { frame = new JFrame(); frame.setIconImage(Toolkit.getDefaultToolkit().getImage(appMainWindow.class.getResource("/icons/appIcon2.png"))); frame.setBounds(100, 100, 1200, 728); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(true); frame.setTitle("Shaareable Apps Presents ~ Pdf Imposition Monster"); frame.getContentPane().setLayout(null); JButton btnAddARectangle = new JButton("Add a rectangle"); btnAddARectangle.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { AddRectangle(); repaint(); } }); btnAddARectangle.setBounds(6, 219, 151, 29); frame.getContentPane().add(btnAddARectangle); frame.getContentPane().add(canvas); lblPdfcount = new JLabel("PdfCount"); lblPdfcount.setBounds(27, 260, 61, 16); frame.getContentPane().add(lblPdfcount); } }
- 09-12-2014, 07:15 PM #2
Re: Initial repaint() fails to work, have to resize window to kickstart things?
On a side note, in eclipse, I also lost the Design button that shows me the ui preview (using WindowsBuilder) anyone knows how to get it back?
- 09-12-2014, 07:15 PM #3
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Initial repaint() fails to work, have to resize window to kickstart things?
I haven't looked at this in depth but you are extending JFrame (which you shouldn't do)
and also creating a JFrame (which you should do). Don't extend JFrame, but extend JPanel.
Add the panel instance (via this) to your JFrame. Other tweaks may be required.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 09-13-2014, 11:31 PM #4
Re: Initial repaint() fails to work, have to resize window to kickstart things?
Hello Jim,
Thank you for your suggestion. I changed it. Unless I resize the whole window, the custom JPanel doesn't do its repaint(). Once I resize the window, it starts working, i.e, everytime I click the button, a rectangle is added to the custom JPanel. But when the application loads, clicking the button doesn't repaint(). Say I click 5 five times, nothing changes, then I resize the window, then I'll see right away the five rectangles, and from then on, each clicks generates a rectangle as intended.
here's the code:
Java Code:package views; import java.awt.EventQueue; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.Color; import javax.swing.JButton; import views.appMainWindow.PdfLocation; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.util.ArrayList; import javax.swing.JLabel; public class Sandbox extends JPanel { public ArrayList<PdfLocation> PdfLocations = new ArrayList<PdfLocation>(); public int PdfImagesCount = 0; class PdfLocation { public double xPos; public double yPos; } class DrawingCanvas extends JPanel { @Override public void paintComponent(Graphics g) { super.paintComponent(g); for (PdfLocation p : PdfLocations) { g.drawRect((int)p.xPos, (int)p.yPos, 35, 20); repaint(); } } } public void AddRectangle() { PdfImagesCount++; lblPdfcount.setText(Integer.toString(PdfImagesCount)); PdfLocation rect = new PdfLocation(); if (PdfLocations.isEmpty() == false) { PdfLocation spot = PdfLocations.get(PdfLocations.size() - 1); rect.xPos = spot.xPos + 45; rect.yPos = 10; } else { rect.xPos = 10; rect.yPos = 10; } PdfLocations.add(rect); repaint(); } private JFrame frame; private JLabel lblPdfcount; private JButton btnAdd; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Sandbox window = new Sandbox(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public Sandbox() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { frame = new JFrame(); frame.setBounds(100, 100, 826, 301); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); DrawingCanvas canvas = new DrawingCanvas(); canvas.setBackground(Color.WHITE); canvas.setBounds(109, 11, 691, 240); frame.getContentPane().add(canvas); btnAdd = new JButton("Add"); btnAdd.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { AddRectangle(); repaint(); } }); btnAdd.setBounds(10, 11, 89, 23); frame.getContentPane().add(btnAdd); lblPdfcount = new JLabel("PdfCount"); lblPdfcount.setBounds(10, 45, 46, 14); frame.getContentPane().add(lblPdfcount); } }
- 09-13-2014, 11:39 PM #5
Re: Initial repaint() fails to work, have to resize window to kickstart things?
I fixed it by adding frame.repaint() after calling AddRectangle() in my button event.
- 09-13-2014, 11:49 PM #6
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Initial repaint() fails to work, have to resize window to kickstart things?
Like I said, I didn't look that closely at it and it needed to be tweaked. So since you are doing your painting on a canvas,
no need for Sandbox to extend JPanel (mea culpa). However, you need to remove the repaint from within paintComponent()
otherwise, it will never end and overflow the call stack.
And change repaint() in the addRectangle to frame.repaint().
Also, here is a tip. JFrame.add() method is a helper method that adds the component to the content pane. So you can do it that
way (or your way if you want).
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 09-15-2014, 06:19 PM #7
Re: Initial repaint() fails to work, have to resize window to kickstart things?
Hi Jim and thanks for the tip. Would you know by any chance if Eclipse/Java EE/WindowsBuilder has a way to see in the design view our custom components?
For example in this code the DrawingCanvas extends a JPanel, but for an unknown(to me) reason I can't see it in the design view.
I wish I could, it'd be much easier to position etc....
Thank you again Jim
- 09-15-2014, 06:33 PM #8
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Initial repaint() fails to work, have to resize window to kickstart things?
I use Eclipse but I am unfamiliar with what you refer to as the design view. If you are talking about a GUI designer, for placement
of components then I recommend you use layout managers instead. GUI designers tend to use absolute positioning which
can make your app less flexible.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 09-15-2014, 06:48 PM #9
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: Initial repaint() fails to work, have to resize window to kickstart things?
"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 09-16-2014, 09:31 PM #10
Similar Threads
-
gridbaglayout does not resize with the window
By Marcoita_88 in forum AWT / SwingReplies: 5Last Post: 08-06-2011, 11:31 AM -
Window resize icon...
By pele in forum SWT / JFaceReplies: 3Last Post: 06-09-2008, 09:31 AM -
Problems with gridBaglayout when I resize the window
By Iyengar in forum AWT / SwingReplies: 1Last Post: 02-17-2008, 12:43 AM -
Repaint fails when using threads
By rjevans2000 in forum Threads and SynchronizationReplies: 1Last Post: 09-22-2007, 12:22 AM -
Repaint fails when using threads
By rjevans2000 in forum AWT / SwingReplies: 3Last Post: 08-15-2007, 06:42 PM
Bookmarks