Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Chronic788

Thread: Calling a method that paints something

  1. #1
    Chronic788 is offline Member
    Join Date
    Jun 2012
    Posts
    5
    Rep Power
    0

    Default Calling a method that paints something

    Hello all. I am fairly new to Java and don't know much about using Java to paint using 2DGraphics. I have a fairly complex program in the works that implements a lot of painting. I am writing this program to learn more about painting and more about Java in general. However, I have run into a problem that prevents me from writing any further until I figure it out. I decided to simplify the problem into a simple example so you guys didnt have to sort through a bunch of code to figure out what the heck Im talking about.

    Please look at the following code and then I will explain my problem:

    Here is the first class entitled Board.

    import java.awt.*;
    import javax.swing.*;


    public class Board extends JPanel{


    public Board(){
    setBackground(Color.LIGHT_GRAY);
    }

    public void paintComponent(Graphics g){

    super.paintComponent(g);

    g.setColor(Color.BLACK);
    g.drawOval(20, 20, 500, 500);
    }
    }
    Here is the second class entitled Test:

    import javax.swing.*;
    import java.awt.*;

    public class Test {

    public static boolean isRedTurn = false;
    public static boolean isBlackTurn = false;

    public static void main(String[] args) {

    new Test();
    }

    public Test() {

    super();

    JFrame gui = new JFrame();

    gui.setTitle("Paint Program");
    gui.setSize(683, 683);
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
    gui.setVisible(true);

    Board panel = new Board();

    Container pane = gui.getContentPane();
    pane.setLayout(new GridLayout(1,1));

    pane.add(panel);

    redTurn();

    }

    public void redTurn() {
    isRedTurn = true;

    ******* I want to call paintOval here*************

    }
    }
    Now what I want to do is call a method called paintOval in redTurn() that will paint over the oval in Board. Here is the code I want to run:

    public void paintOval(Graphics g){

    super.paintComponent(g);

    if(Test.isRedTurn){
    g.setColor(Color.red);
    g.fillOval(20, 20, 500, 500);
    }
    }
    The error i get if I try to call paintOval() from within redTurn() is this: method paintOval in test cannot be applied to given types
    required: java.awt.Graphics;
    found: no arguments

    So my questions are these: can I pass something into paintOval() to satisfy the method to paint properly in the same JPanel as Board? If so what is it?
    Am I going about this completely wrong? Is there another way to paint something in Board's JPanel from within Test?

    Thanks for your input guys, I appreciate it in advance!

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

    Default Re: Calling a method that paints something

    Some suggestions:
    • You'll want to make all of your static variable instance variables. When you create a static variable or method, you cannot use it in an object-oriented way, causing your program to lose flexibility and power, and for this reason, you should use static anything only for very specific reasons, none of which apply here.
    • Consider giving Board a boolean variable, redTurn, that its paintComponent will use to fill or not fill a red oval if desired.
    • Then give Board a public void method called setRedTurn(boolean) that sets the redTurn boolean field and then calls repaint() on the Board instance. This will allow classes that hold Board object to change Board's state and let Board change its display.
    • Get 90% of your code out of your main static method and into a proper OOPs class.
    • Do read the painting in Swing tutorials to see how to do this correctly. You can find the basic tutorials here and the advanced tutorial here.
    • When posting code here, wrap the code in [code] [/code] tags not [quote] [/quote] tags. Otherwise your code becomes unreadable.

  3. #3
    Chronic788 is offline Member
    Join Date
    Jun 2012
    Posts
    5
    Rep Power
    0

    Default Re: Calling a method that paints something

    Thanks for your quick and concise reply, Fubarable! Your suggestion of using the Redturn boolean with the setmethod worked like a charm. The links to the tutorials were also a big help.
    Fubarable likes this.

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

    Default Re: Calling a method that paints something

    You're quite welcome -- glad it helped!

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Calling a method that paints something

    For your future reference: BB Code List - Java Programming Forum

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Chronic788 is offline Member
    Join Date
    Jun 2012
    Posts
    5
    Rep Power
    0

    Default Re: Calling a method that paints something

    Thanks guys for you help and links. I have made some progress on my program thanks to your help. However, I have run into another problem that, yet again, I cannot seem to get around. I have spent hours and tried as many different solutions as I can think of but to no avail. Seriously, I am out of ideas. I hoped to get a little bit more help from the experts here.

    Let me explain the some of the aspects of my program to clarify before I hit you with the code. My program generates 576 ovals called holes that need to be able to be made red or black after I click on them. The red player and then the black player take turns When an oval is clicked, it needs to stay either red or black for the rest of the execution of the program. This is the issue I am having trouble with. I cant seem, to find a way get the oval to stay filled with the color I clicked it with. I would really appreciate suggestions on how to do this. If I wasnt clear enough about the problem please ask specific questions so that I can clarify better for you guys.

    Here is the first Class named Board

    Java Code:
    package BoardAttributes;
    import Game.*;
    import java.awt.*;
    import javax.swing.*;
    
    public class Board extends JPanel{
    
        
        protected String name;
        protected boolean filled = false;
        protected boolean pylonStart;
        protected boolean pylonEnd;
        protected boolean redTurn = false;
        protected boolean blackTurn = false;
        protected boolean isRed = false;
        protected boolean isBlack = false;
        protected int placedHoleX;
        protected int placedHoleY;
        public boolean isFixedRed = false;
        public boolean isFixedBlack = false;
        
        public static void main(String[] args) {
            
            new Board();
          
        }
        
        public Board(){  //constructor
            
          setBackground(Color.LIGHT_GRAY);
        }
        
        
        public void paintComponent(Graphics g) {
            
            
            
            super.paintComponent(g);
            
            g.setColor(Color.red);
            g.drawLine(33, 33, 608, 33);
            g.drawLine(33, 608, 608, 608);
            g.setColor(Color.BLACK);
            g.drawLine(33, 33, 33, 608);
            g.drawLine(608, 33, 608, 608);
            
            
         //*************************************************
           
            int x = 15;
            int y = 15;
            final int r = 10;
            
                
            
            for (int i = 0; i < 25; i++) {
                
                for (int j = 0; j < 25; j++) {
                    
                    
                    g.setColor(Color.BLACK);
                    g.drawOval(x, y, r, r);
                    
                    
                             if(redTurn){
                                g.setColor(Color.red);
                                g.fillOval(Test.ovalStartX, Test.ovalStartY , r, r);
                                
                                
                                         }
                             if(blackTurn){
                                g.setColor(Color.BLACK);
                                g.fillOval(Test.ovalStartX, Test.ovalStartY, r, r);
                                
                                           }
                                     
                    x += 25;
                }//end of inner for
                
                y += 25;
                x = 15;
            }//end of outer for
          //*************************************************
       
            
    }
        
        public void setRedDraw(){
            blackTurn = false;
            redTurn = true;
            repaint();
            
        }
        
        public void setBlackDraw(){
            redTurn = false;
            blackTurn = true;
            repaint();
            
        }
    
    }
    And here is the second Class:

    Java Code:
    package Game;
    
    import BoardAttributes.*;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.*;
    
    public class Test extends JPanel {
        
        private JLabel statusbar;
        private int x = 0;
        private int y = 0;
        public boolean isRedTurn = false;
        public boolean isBlackTurn = false;
        public int clickedHole;       
        public int centerOfHoleX;
        public int centerOfHoleY;
        public static int ovalStartX;
        public static int ovalStartY;
        public boolean wasNotValidHole = false;
        
       
                
        public static void main(String[] args) { 
          
          new Test();
    
        }
        
        public Test(){
            
                    
            super();
            JFrame gui = new JFrame();
          
            gui.setTitle("Game");
            gui.setSize(683, 683);
            gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            gui.setVisible(true);
            
            Board panel = new Board();
            
            Container pane = gui.getContentPane();
            pane.setLayout(new GridLayout(1,1));
            
            pane.add(panel);
     
               statusbar = new JLabel("Nothings Happened Yet");
               panel.add(statusbar);
               //statusbar.setVerticalAlignment(JLabel.SOUTH);
               
               Handlerclass handler = new Handlerclass();
               
            panel.addMouseListener(handler);
            
                
            int finish = 0;   //this is just a temporary variable to keep the following loop going
            
            while(finish == 0){
            redTurn(panel);
            x = 0;
            y = 0;
            blackTurn(panel);
            x = 0;
            y = 0;
            }
            
        }
        
        public class Handlerclass implements MouseListener {
            
            
            @Override
            public void mouseClicked(MouseEvent event){
                
                x = event.getX();
                y = event.getY();
                statusbar.setText(String.format("You clicked at %d, %d", event.getX(), event.getY()));
                
            }
            @Override
            public void mousePressed(MouseEvent event){
               
            }
            @Override
            public void mouseReleased(MouseEvent event){
             
            }
            @Override
            public void mouseEntered(MouseEvent event){
              
            }
            @Override
            public void mouseExited(MouseEvent event){
                
            }
        }
        
        
        public void redTurn(Board panel){
            isRedTurn = true;
            
            while(x==0 && y==0){
       
                    statusbar.setText("Click the hole you would like to place a pin in, Red.");
            }
            testLocationRed(panel);
            
        }
        
       public void blackTurn(Board panel){
           isBlackTurn = true;
           
           while(x==0 && y==0){
              
               statusbar.setText("Click the hole you would like to place a pin in, Black.");
           }
           testLocationBlack(panel);
           
       } 
        
        public void testLocationRed(Board panel){
            
            
           int xmin = 15;
           int xmax = 25;
           int ymin = 15;
           int ymax = 25;
            
            for (int j = 0; j < 25; j++) {
                
            
            for (int i = 0; i < 25; i++) {
                
                if((x >= xmin && x <= xmax) && (y >= ymin && y <= ymax)){
                    clickedHole = i*j;
                    centerOfHoleX = xmin + 5;
                    centerOfHoleY = ymin + 5;
                    ovalStartX = xmin;
                    ovalStartY = ymin;
                    panel.setRedDraw();
                    break;
                }
    
                xmin += 25;
                xmax += 25;
                
            }
            xmin = 15;
            xmax = 25;
            ymin += 25;
            ymax += 25;
            }
        }
    
        public void testLocationBlack(Board panel){
            
            
           int xmin = 15;
           int xmax = 25;
           int ymin = 15;
           int ymax = 25;
            
            for (int j = 0; j < 25; j++) {
                
            
            for (int i = 0; i < 25; i++) {
                
                if((x >= xmin && x <= xmax) && (y >= ymin && y <= ymax)){
                    clickedHole = i*j;
                    centerOfHoleX = xmin + 5;
                    centerOfHoleY = ymin + 5;
                    ovalStartX = xmin;
                    ovalStartY = ymin;
                    panel.setBlackDraw();
                    break;
                }
                xmin += 25;
                xmax += 25;
                
            }
            xmin = 15;
            xmax = 25;
            ymin += 25;
            ymax += 25;
            }
        }
      
    }
    Thanks so much for your help in advance!

    I am very new to programming Java; Ive only take one class. And my class only learned about Objects for about a week. That; why I may be improperly using code or using I poorly. I know my whole program is based on objects and their manipulation so I have little choice but to wing it. So please bear with me!

    Any other suggestions are welcome.

Similar Threads

  1. Thread problem, calling method in run method
    By majk in forum Threads and Synchronization
    Replies: 4
    Last Post: 09-27-2010, 12:40 PM
  2. Calling The main method from another method
    By SwissR in forum New To Java
    Replies: 3
    Last Post: 07-27-2010, 12:03 PM
  3. calling method from main method
    By bob_bee in forum New To Java
    Replies: 4
    Last Post: 10-02-2009, 06:30 PM
  4. Replies: 29
    Last Post: 09-25-2008, 08:55 PM
  5. Replies: 3
    Last Post: 01-06-2008, 12:13 AM

Posting Permissions

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