Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Problem printing onto JPanel

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem printing onto JPanel

    I am trying to draw at my mouse a certain shape that I have set. I defined some shapes where they extend shape and draw circles and stuff. But when I click on panel it seems the paint doesnt put anything there. Debugger tells me shapes are saved though.

      public void mouseClicked(MouseEvent e) {
            currentX = e.getX();
     
            currentY = e.getY();
     
            Shape newShape = owner.currentBrush.clone();
            picture.add(newShape); 
            repaint();
        }
        public void paint(Graphics g){
     
                super.paint(g);
                for( int i = 0; i < myShapes.size(); i++ ){
                    picture.get(i).draw(g); 
                }
     
            }
     
        public void draw(Graphics g){
                Graphics g2d = (Graphics2D) g;
     
                g2d.setColor(Color.BLUE);
     
                g2d.fillOval(getX(), getY(), radius, radius);
     
                g.drawOval(getX(), getY(), radius, radius);
            }

    drawpanel:
    import java.util.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.Color;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
     
    public class DrawPanel extends JPanel implements MouseListener{
        Application owner;
        Stack myShapes = new Stack();
        Stack undoStack = new Stack();
        ArrayList<Shape> picture;
        int currentX = 10;
        int currentY = 10;
     
        public DrawPanel(Application parent){
            //this.setOpaque(true);
            this.owner = parent;
            this.setPreferredSize(new Dimension(600,800));
            this.setBackground(Color.WHITE);  
            this.setVisible(true);
            picture = new ArrayList<Shape>();
            addMouseListener(this);
            repaint();
            //addMouseMotionListener(this);
     
        }
     
        public void paint(Graphics g){
     
            super.paint(g);
            for( int i = 0; i < owner.myDraw.picture.size(); i++ ){
                owner.myDraw.picture.get(i).draw(g); 
                System.out.println("I BET THIS NEVER PRINTS");
            }
     
     
            System.out.println("I AM TRYING TO PAINT");
        }
        public void mousePressed(MouseEvent e) {
            currentX = e.getX();
            currentY = e.getY();
            //System.out.println(e.getY());
            Shape newShape = owner.currentBrush;
            picture.add(newShape); 
            repaint();
        }
        public void mouseEntered(MouseEvent e) {
        }
        public void mouseExited(MouseEvent e) {
        }
        public void mouseReleased(MouseEvent e){
        }
        public void mouseClicked(MouseEvent e) {
            owner.myDraw.currentX = e.getX();
     
            owner.myDraw.currentY = e.getY();
     
           // System.out.println(e.getY());
            Shape newShape = owner.getBrush();
            owner.myDraw.picture.add(newShape); 
            System.out.println(picture.size());
            repaint();
        }
     
        public void redo(){
            try{
                owner.myDraw.picture.add((Shape)undoStack.pop());
            } catch(LinkedListException p){
                System.out.println("Empty stack no poppy please");
            }
        }
     
        public void undo(){
            try{
                owner.myDraw.undoStack.push(picture.remove(picture.size()-1));
            } catch(LinkedListException e){
                System.out.println("Nothing to undo");
            }
        }
     
    }

    main app:

    import java.util.*;
    import javax.swing.*;
    import java.awt.*;
    import java.io.Serializable;
     
    public class Application extends JFrame{
        protected ButtonPanel myButton;
        protected DrawPanel myDraw;
     
        Shape currentBrush;
        JFrame frame;
        int currentX;
        int currentY;
     
        public Application() {
            frame = new JFrame("Main App");
            currentBrush = new Circle(5,5,5);
            this.myDraw = new DrawPanel(this);
            this.myButton = new ButtonPanel(this);
     
            frame.setSize(800, 800);
            frame.add(new ButtonPanel(this));
            frame.add(new DrawPanel(this));
            frame.setVisible(true);
            frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.LINE_AXIS));
     
        }
     
        public Application(int x, int y, int r) {
            System.out.println("Setting to custom brush");
            currentBrush = new Circle(x,y,r);
        }
     
        public void add(){
            try{
                myDraw.myShapes.insert(currentBrush);
            } catch(LinkedListException e){
                System.out.println("Good day sir");
            }
        }
     
        public Shape getBrush(){
            return new Shape(currentBrush);
        }
     
        public void setCurrentBrush(Shape newBrush){
        this.currentBrush = new Shape(newBrush);
        }
     
        public void setX(int nx){
        this.currentX = nx;
        }
     
        public void setY(int ny){
            this.currentY = ny;
        }
     
    }
    Last edited by steuben; June 13th, 2014 at 02:16 PM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,803
    My Mood
    Mellow
    Thanks
    236
    Thanked 717 Times in 703 Posts

    Default Re: Java Custom Paint Buttons Not Being Buttons

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    Several questions/points to consider:

    1. What is Application? What purpose does it serve?
    2. The use of 'this.' in the constructor is unnecessary.
    3. The proper way to paint on a JPanel is to override the paintComponent() method. Overriding paint() should generally not ever be done in Swing, unless being done by an expert.
    4. Where's the draw() method?
    5. We cannot see the buttons or the action listeners attached to them so can't tell what should be happening. Show that code if you want help.
    6. This design looks unnecessarily complicated, but it's hard to be sure without the unposted code.

    Good enough for now. Come back when you can.

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem printing onto JPanel

    1. It is a mini ms paint. That is all my professor told us to do
    2. the this is the only way I know how to access between a button, draw, and main app class.
    3. I wish I was an expert, but I have to do this. It is 10% of my grade overall
    4. added
    5. Will add some more, will be a huge wall of text though.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,337
    Thanks
    182
    Thanked 830 Times in 773 Posts
    Blog Entries
    5

    Default Re: Problem printing onto JPanel

    But when I click on panel it seems the paint doesnt put anything there. Debugger tells me shapes are saved though.
    In the Application class constructor:

            this.myDraw = new DrawPanel(this);
            this.myButton = new ButtonPanel(this);
     
            frame.setSize(800, 800);
            frame.add(new ButtonPanel(this));
            frame.add(new DrawPanel(this));

    Think about the code above for a second...how many instances of DrawPanel does the code create?

  5. #5
    Junior Member
    Join Date
    Jun 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem printing onto JPanel

    Quote Originally Posted by copeg View Post
    In the Application class constructor:

            this.myDraw = new DrawPanel(this);
            this.myButton = new ButtonPanel(this);
     
            frame.setSize(800, 800);
            frame.add(new ButtonPanel(this));
            frame.add(new DrawPanel(this));

    Think about the code above for a second...how many instances of DrawPanel does the code create? Very good catch though.
    Yeah but I would just be drawing on the new Jpanel even so. Didn't seem to fix the not drawing.

    the bluej folder http://www.filedropper.com/paintproject
    Full code @ [Java] import java.util.*; import javax.swing.*; import java.awt.*; import java.io.S - Pastebin.com I think.
    Last edited by steuben; June 13th, 2014 at 03:35 PM.

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,337
    Thanks
    182
    Thanked 830 Times in 773 Posts
    Blog Entries
    5

    Default Re: Problem printing onto JPanel

    Yeah but I would just be drawing on the new Jpanel even so.
    Since it creates two references, the drawing routine in the panel loops over an empty list (the non-visible JPanel)

    for( int i = 0; i < owner.myDraw.picture.size(); i++ ){
                owner.myDraw.picture.get(i).draw(g); 
                System.out.println("I BET THIS NEVER PRINTS");
            }

    Once that's fixed - look at where you code creates the Shape: x = 5 y = 5 with radius 5.

    And for future reference, often I (and many others) will not look at code linked on 3rd party sites - post it here. In addition, posting as an SSCCE saves a lot of time and helps you get help faster (and in many cases, helps you better understand and sometimes even fix the problem)

Similar Threads

  1. Function and radio Buttons in JAVA
    By learning2code in forum Other Programming Languages
    Replies: 7
    Last Post: December 17th, 2013, 06:32 AM
  2. can i use custom buttons ?
    By Gaurav18 in forum Member Introductions
    Replies: 3
    Last Post: September 12th, 2011, 09:56 AM
  3. New to Java: Adding Functionality to my Buttons
    By Staticity in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 23rd, 2011, 04:19 PM
  4. Help with Java buttons
    By Nameless _1 in forum AWT / Java Swing
    Replies: 2
    Last Post: June 18th, 2011, 03:16 PM
  5. Java swing buttons are flickering in windows 7 environment
    By javasam in forum AWT / Java Swing
    Replies: 1
    Last Post: February 14th, 2011, 11:13 AM