Results 1 to 11 of 11
- 12-18-2010, 10:58 AM #1
Why is java paintComponents() not working?
okay, here's my code. . .
i'm really stuck on this one. what i want is that when i click the button, another circle would be painted but it doesn't work that way. only one circle is shown right after i run this program, and no more. help. :(Java Code:public class DrawingPanel extends JPanel { ClickButton click; public DrawingPanel() { this.setOpaque(true); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); final Graphics2D g2 = (Graphics2D) g; Ellipse2D oval = new Ellipse2D.Double(5.0, 40.0, 25.0, 25.0); g2.draw(oval); g2.setColor(Color.white); g2.fill(oval); click = new ClickButton(); click.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { g2.draw(anotherCircle()); repaint(); } }); } public Ellipse2D anotherCircle() { Ellipse2D oval = new Ellipse2D.Double(15.0, 40.0, 25.0, 25.0); return oval; } }
-
You would never want to create components within the paintComponent method as you do not have absolute control of when or if that method is called, and the method should be as bare and as fast as possible so as not to slow down the drawing of your GUI.
So create your JButton in the constructor and add it to your JPanel. If you want to be able to display one more circle, then give your class a boolean variable and have that variable set to true on button press and then call repaint(). In the paintComponent method, use an if block that tests this boolean variable, and if true, draw your ellipse.
If you want draw many random ellipses, then have an ArrayList of Ellipse2D objects or something like that, and on button press, add a new Ellipse2D.Double to the ArrayList. Then iterate through the list in the paintComponent method, drawing each one as you get to it.
- 12-19-2010, 01:41 PM #3
okay. i'll try. & thank you for the suggestion! :D
- 12-20-2010, 02:43 AM #4
here's what i did. i created a jbutton so when a user clicks the button, the painting would be done.
Here's my main class:
And here's my painting class:Java Code:public class MyFrame extends JFrame { ClickMe click; PaintPanel painting; public static void main(String[] args) { MyFrame frame = new MyFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public MyFrame() { setTitle("State Machine Simulator"); setSize(700, 425); setLayout(null); setLocationRelativeTo(null); setResizable(false); JPanel panel = new JPanel(); panel.setBounds(5, 40, 530, 323); panel.setBorder(BorderFactory.createLineBorder(Color.black)); panel.setBackground(Color.white); add(panel); click = new ClickMe(); click.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { painting = new PaintPanel(); add(painting); painting.repaint(); //is this repaint() only? } }); add(click.clickMe()); } }
i didn't declare anymore a boolean variable to check if the button is pressed, coz i believe that the button's actionPerformed would do the task. but when i click it, there's no painting shown. is there something that i missed? :confused:Java Code:public class PaintPanel extends JPanel { @Override public void paintComponent(Graphics g) { this.setOpaque(false); super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; Ellipse2D oval = new Ellipse2D.Double(5.0, 40.0, 25.0, 25.0); g2.draw(oval); g2.setColor(Color.white); g2.fill(oval); } }
-
Well, you didn't take our advice about the boolean for one. The drawing JPanel should be added on GUI creation and should be visible, and it should show an oval when the boolean is true.
But to fully help you, we need to know your complete program requirements. What are you trying to do exactly? Show a single oval when a button is pressed and do nothing if it is pressed again? Make it disappear when the button is pressed again? Show a random oval when button is pressed and keep showing them with each button press? The more details you provide the greater we can know what you have right and what you have wrong in your code.
- 12-20-2010, 02:57 AM #6
i'm trying to paint a single oval after the button is clicked. [this is for practice only, so when i already know how to use paint, i'm going to give the user the right to create lots of ovals in different locations that he wants, like when he clicks the button clickMe, he still has to click a certain location where he wants to put the ovals].
- 12-20-2010, 03:31 AM #7
And also. why does paintComponent doesn't work if one still has to click a button. but if it is run directly, it works? :confused:
-
You can find lots of examples of drawing shapes in this forum. For instance in this thread I've posted JDrawRect2 for drawing rectangles on a JPanel through a MouseAdapter (combined MouseListener and MouseMotionListener).
-
paintComponent does work, but you have to code it a bit differently than you're used to.
Here's another example that is similar to your code above and that uses booleans:
Java Code:import java.awt.*; import java.awt.event.*; import java.awt.geom.Ellipse2D; import javax.swing.*; public class DrawOvalOnButtonClick { private JPanel mainPanel = new JPanel(); private PaintPanel paintPanel = new PaintPanel(); private JToggleButton drawOvalButton = new JToggleButton("Draw Oval"); public DrawOvalOnButtonClick() { drawOvalButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { paintPanel.setDrawOval(drawOvalButton.isSelected()); paintPanel.repaint(); } }); JPanel btnPanel = new JPanel(); btnPanel.add(drawOvalButton); paintPanel.setPreferredSize(new Dimension(400, 400)); mainPanel.setLayout(new BorderLayout()); mainPanel.add(paintPanel, BorderLayout.CENTER); mainPanel.add(btnPanel, BorderLayout.SOUTH); } public JPanel getMainPanel() { return mainPanel; } private static void createAndShowUI() { JFrame frame = new JFrame("DrawOvalOnButtonClick"); frame.getContentPane().add(new DrawOvalOnButtonClick().getMainPanel()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } } class PaintPanel extends JPanel { private Ellipse2D oval = new Ellipse2D.Double(50, 50, 300, 300); private boolean drawOval = false; @Override public void paintComponent(Graphics g) { super.paintComponent(g); if (drawOval) { Graphics2D g2 = (Graphics2D) g; g2.setColor(Color.white); g2.fill(oval); g2.setColor(Color.black); g2.draw(oval); } } public boolean isDrawOval() { return drawOval; } public void setDrawOval(boolean drawOval) { this.drawOval = drawOval; } }
- 12-20-2010, 05:45 AM #10
the code you shared works great!!!! :D
but i just have a question, is there a way that when i toggle off the button, the oval would remain on the panel? :o
-
Similar Threads
-
paintComponent vs paintComponents
By alacn in forum New To JavaReplies: 5Last Post: 07-26-2010, 03:33 AM -
Working with Alice and Java
By jaicea in forum New To JavaReplies: 2Last Post: 12-06-2009, 05:57 AM -
I am working on java!
By pawankumarom in forum New To JavaReplies: 33Last Post: 03-06-2009, 11:24 AM -
Java mail problem(working in intranet,but not working in iternet)
By sundarjothi in forum Advanced JavaReplies: 8Last Post: 05-28-2008, 07:00 AM -
working with mp3 in java
By po0oker in forum Advanced JavaReplies: 1Last Post: 10-30-2007, 08:20 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks