Results 1 to 8 of 8
  1. #1
    ddd1600 is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default paintComponent() + main() issues...

    OK, so I'm following the instructions in a Java beginner's handbook and come across this bit on painting. Clearly, the book hasn't written the entire code, and I know that this bit here is an object, but I can't seem to instantiate it and get it implemented, so to speak.

    I've tried this three ways and all have failed.

    ATTEMPT 1----would not compile, "illegal start of expression" on the method
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class ColorfulCircle extends JPanel {
        
        public static void main(String[] args) {
    
            public void paintComponent(Graphics g) {
                Graphics2D g2d = (Graphics2D) g;
    
                int red = (int) (Math.random() * 256);
                int green = (int) (Math.random() * 256);
                int blue = (int) (Math.random() * 256);
                Color startColor = new Color(red, green, blue);
    
                red = (int) (Math.random() * 256);
                green = (int) (Math.random() * 256);
                blue = (int) (Math.random() * 256);
                Color endColor = new Color(red, green, blue);
    
                GradientPaint gradient = new GradientPaint(70,70,startColor, 150,150,endColor);
                g2d.setPaint(gradient);
                g2d.fillOval(70,70,100,100);
               
    
    }
    }
    ATTEMPT 2: It doesn't recognize the method "paintComponent"
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class ColorfulCircle extends JPanel {
    
            public void paintComponent(Graphics g) {
                Graphics2D g2d = (Graphics2D) g;
    
                int red = (int) (Math.random() * 256);
                int green = (int) (Math.random() * 256);
                int blue = (int) (Math.random() * 256);
                Color startColor = new Color(red, green, blue);
    
                red = (int) (Math.random() * 256);
                green = (int) (Math.random() * 256);
                blue = (int) (Math.random() * 256);
                Color endColor = new Color(red, green, blue);
    
                GradientPaint gradient = new GradientPaint(70,70,startColor, 150,150,endColor);
                g2d.setPaint(gradient);
                g2d.fillOval(70,70,100,100);
    }
            public static void main(String[] args) {
                ColorfulCircle c = new ColorfulCircle();
                c.paintComponent();
            }
    }
    ATTEMPT 3:
    So I tried to just save the public class and method in its own class file, opened a new empty page and tried this one, similar to the latter. Only this one runs, but nothing comes up but an empty frame which vanishes in a flash.
    Java Code:
    class HelpMe {
    public static void main(String[] args) {
    ColorfulCircle c = new ColorfulCircle();
    c.paintComponent();
    }}
    :mad:
    Last edited by ddd1600; 11-22-2010 at 01:36 AM.

  2. #2
    ddd1600 is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    I've managed to recycle some old code to get it working but there were alot of errors and I feel like this isn't the way to do it...it does work, technically, but if I were to learn this way it would be learning bad habits...
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class ColorfulCircle {
    
        public static class PaintDisplay extends JPanel {
            public void paintComponent(Graphics g) {
                Graphics2D g2d = (Graphics2D) g;
                int red = (int) (Math.random() * 256);
                int green = (int) (Math.random() * 256);
                int blue = (int) (Math.random() * 256);
                Color startColor = new Color(red, green, blue);
    
                red = (int) (Math.random() * 256);
                green = (int) (Math.random() * 256);
                blue = (int) (Math.random() * 256);
                Color endColor = new Color(red, green, blue);
    
                GradientPaint gradient = new GradientPaint(70,70,startColor, 150,150,endColor);
                g2d.setPaint(gradient);
                g2d.fillOval(70,70,100,100);
    }
            public static void main(String[] args) {
                PaintDisplay displayPanel = new PaintDisplay();
                JPanel content = new JPanel();
                content.setLayout(new BorderLayout());
                content.add(displayPanel);
                JFrame window = new JFrame("I hope this works");
                window.setContentPane(content);
                window.setVisible(true);
    
            }
    }
    }
    Last edited by ddd1600; 11-22-2010 at 01:37 AM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ddd1600 View Post
    I've managed to recycle some old code to get it working but there were alot of errors and I feel like this isn't the way to do it...it does work, technically, but if I were to learn this way it would be learning bad habits...
    I see no errors in your latest attempt, though you'll probably want to set the preferred size of one of the displayed components so the window doesn't have to be dragged open to see the circle. This last attempt of yours is a lot better than the previous ones.


    Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:

    Java Code:
    [code]
      // your code goes here
      // notice how the top and bottom tags are different
    [/code]
    Best of luck!

  4. #4
    ddd1600 is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    Thanks

    a few questions for those who feel like helping me understand gui basics:
    - isn't there a less cumbersome way to display a simple 'painting'? Is simultaneously instantiating JPanel, JFrame and PaintDisplay the most 'barebones' way to do it? I'm trying to learn the fundamentals behind displaying any 'painted' (gui-type) object...and I'm consulting two books and an online guide so you can't say I'm not trying...

    - could either of the first two be made to work (display it) with a little bit of tinkering?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ddd1600 View Post
    a few questions for those who feel like helping me understand gui basics:
    - isn't there a less cumbersome way to display a simple 'painting'? Is simultaneously instantiating JPanel, JFrame and PaintDisplay the most 'barebones' way to do it? I'm trying to learn the fundamentals behind displaying any 'painted' (gui-type) object...and I'm consulting two books and an online guide so you can't say I'm not trying...
    You need a top-level container in order to display any Swing GUI, and that's usually a JFrame or JDialog, but you could also use a JOptionPane if you wanted to for this (which actually is a variation on a JDialog). You also need a JComponent to draw on, and that would be your class that extends JPanel. So for a simple Swing app of this type, I'd use a JFrame and a class that extends JPanel and would simply add my drawing JPanel to the JFrame's contentPane.

    - could either of the first two be made to work (display it) with a little bit of tinkering?
    Your first attempt tries to place a method inside of a method, and this won't compile much less work, so delete it. The second attempt tries to display a JComponent without a top-level container, and as discussed above, this won't work either.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default

    Also, painting method overrides shouldn't contain business logic as you have no control over how often or how many times the method will be called.

    In your example, the colors of the gradient paint will change at every repainting.

    db

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Darryl.Burke View Post
    ...
    In your example, the colors of the gradient paint will change at every repainting.
    Agree, but it results in a kind of neat effect when you resize the GUI.

  8. #8
    ddd1600 is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    accidental, but yes.

    thanks for the note about the hole in the logic, my objective reasoning for learning java has a lot to do with business logic, both literally and figuratively---
    :cool:

Similar Threads

  1. paintComponent vs paintComponents
    By alacn in forum New To Java
    Replies: 5
    Last Post: 07-26-2010, 03:33 AM
  2. Trouble with paintComponent()
    By ShirlyPunk in forum AWT / Swing
    Replies: 17
    Last Post: 03-13-2010, 04:27 PM
  3. Working around paintcomponent
    By sahhhm in forum New To Java
    Replies: 2
    Last Post: 05-16-2008, 02:43 AM
  4. Problem going outside paintComponent
    By Thez in forum Java 2D
    Replies: 9
    Last Post: 12-08-2007, 04:59 PM
  5. Drawing outside paintComponent()
    By DarkSide1 in forum Java 2D
    Replies: 2
    Last Post: 11-08-2007, 10:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •