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 5 of 5

Thread: How do I change the color of my cursor?

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do I change the color of my cursor?

    This might seem strange, but that's kind of the problem I'm new to this so please bear with me.
    I'm programming in school using Netbeans and we've just started with graphical programming (i.e JPanel, JClass, JFrame). This is my first assignment and the purpose of this program is that when you press the mouse you will fill 4 circles with diffrent colors. I've managed to draw 4 circles with diffrent colors but I want my cursor to change color simultaneously. Here's my code so far (i know that some of it är unnecassary);

    //Java klass "Klick"
    public class Klick {
        //Medlemsvariabler.
        private int x = 0;
        private int y = 0;
        private int r = 1;    
        public int antal = Rityta.antalKlick;
     
        //Konstruktor
        public Klick ( int x, int y, int r ) {
            this.sättX(x);
            this.sättY(y);
            this.sättRadie(r);
            this.sättAntal(antal);
            }
     
        //Sätt och hämtametoder för radie
            public int hämtaRadie( int r ){
                return r;           
            }
            public void sättRadie( int r ){
                if (r > 0) this.r = r;
                else       this.r = -r;
            }
     
            //Sätt och hämtametoder för Y-koordinater
            public int hämtaY( int y ){
                return y;           
            }
            public void sättY( int y ){
                if (y > 0) this.y = y;
                else       this.y = -y;
            }
     
            //Sätt och hämtametoder för X-koordinater
            public int hämtaX( int x ){
                return x;           
            }
            public void sättX( int x ){
                if (x > 0) this.x = x;
                else       this.x = -x;
            }
     
            //Sätt och hämtametoder för Antal
            public int hämtaAntal( int antal ){
                return antal;           
            }
            public void sättAntal( int antal ){
                if (antal > 0) this.antal = antal;
                else       this.antal = -antal;
            }
     
        public void rita (java.awt.Graphics g) {
            if (antal == 1) {
                    g.setColor(Color.BLUE);
                    g.fillOval(x-r, y-r, r*2, r*2);
                }
            else if (antal == 2) {
                   g.setColor(Color.ORANGE);
                    g.fillOval(x-r, y-r, r*2, r*2); 
                }
            else if (antal == 3) {
                    g.setColor(Color.RED);
                    g.fillOval(x-r, y-r, r*2, r*2);
                }
            else {
                    g.setColor(Color.CYAN);
                    g.fillOval(x-r, y-r, r*2, r*2);
                }
     
        }
      }
     
    //Java Klass "Sikte" (this is the one that's not working properly)
    public class Sikte {
     
        //Medlemsvariabler.
        private int x = 0;
        private int y = 0;
        private int r = 1;
        public int antal = Rityta.antalKlick;
     
        //Konstruktor
        public Sikte ( int x, int y, int r ) {
            this.sättX(x);
            this.sättY(y);
            this.sättRadie(r);
            this.sättAntal(antal);
            }
     
        //Sätt och hämtametoder för radie
            public int hämtaRadie( int r ){
                return r;           
            }
            public void sättRadie( int r ){
                if (r > 0) this.r = r;
                else       this.r = -r;
            }
     
            //Sätt och hämtametoder för Y-koordinater
            public int hämtaY( int y ){
                return y;           
            }
            public void sättY( int y ){
                if (y > 0) this.y = y;
                else       this.y = -y;
            }
     
            //Sätt och hämtametoder för X-koordinater
            public int hämtaX( int x ){
                return x;           
            }
            public void sättX( int x ){
                if (x > 0) this.x = x;
                else       this.x = -x;
            }
     
            //Sätt och hämtametoder för Antal
            public int hämtaAntal( int antal ){
                return antal;           
            }
            public void sättAntal( int antal ){
                if (antal > 0) this.antal = antal;
                else       this.antal = -antal;
            }
     
        public void rita (java.awt.Graphics g) {
            if (antal == 0) {
                    g.setColor(Color.BLUE);
                    g.fillOval(x-r, y-r, r*2, r*2);
                }
            else if (antal == 1) {
                   g.setColor(Color.ORANGE);
                    g.fillOval(x-r, y-r, r*2, r*2); 
                }
            else if (antal == 2) {
                    g.setColor(Color.RED);
                    g.fillOval(x-r, y-r, r*2, r*2);
                }
            else {
                    g.setColor(Color.CYAN);
                    g.fillOval(x-r, y-r, r*2, r*2);
                }
            }
        }
     
    //Java Panel "Rityta"
    public class Rityta extends javax.swing.JPanel {
     
        // Data:
        Sikte sikte;
        Klick[] klick = new Klick[4];
        public static int antalKlick = 0;
     
        public Rityta() {
            initComponents();  
           sikte = new Sikte(20, 20, 10);
        }
     
        public void paintComponent(java.awt.Graphics g) {
            super.paintComponent(g);
            sikte.rita(g);
            for (int i=0; i < antalKlick; i++)
                if (klick[i] != null) klick[i].rita(g);
        }
    private void formMouseClicked(java.awt.event.MouseEvent evt) {                                  
            klick[antalKlick++] = new Klick(evt.getX(), evt.getY(),10);        
            repaint();       
        }                                 
     
        private void formMouseMoved(java.awt.event.MouseEvent evt) {                                
            sikte.sättX(evt.getX());
            sikte.sättY(evt.getY());
            repaint();       
        }

    As i said, this is really basic programming but my teacher is not the sharpest tool in shed so i couldn't get help from him :/
    Hope someone can help me Sorry if it's difficult to read my variables since they are in swedish.


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    571
    My Mood
    Sleepy
    Thanks
    9
    Thanked 84 Times in 78 Posts

    Default Re: How do I change the color of my cursor?

    Hi, google for "java swing custom mouse cursor", there are quite a few tutorials and questions in forums.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,529
    My Mood
    Mellow
    Thanks
    157
    Thanked 615 Times in 602 Posts

    Default Re: How do I change the color of my cursor?

    This is a non-standard constructor design:
        public Klick ( int x, int y, int r ) {
            this.sättX(x);
            this.sättY(y);
            this.sättRadie(r);
            this.sättAntal(antal);
            }
    It's "wrong" only in the sense that it's inefficient and unnecessarily complex. Also, the variable 'antal' is already initialized, so the last statement in the constructor that calls a method to set the variable 'antal' to the value it already contains is kind of silly plus ineffcient and unnecessarily complex.

    More typical or standard declarations of instance variables and their initialization in the constructor would be:
        //Medlemsvariabler.
        private int x;
        private int y;
        private int r;  // there may be a need for '= 1' but not sure
        public int antal;
     
        //Konstruktor
        public Klick ( int x, int y, int r ) {
            this.x = x;
            this.y = y;
            this.r = r;
            antal = Rityta.antalKlick;
            }
    Good luck!

  4. #4
    Junior Member
    Join Date
    Jan 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I change the color of my cursor?

    Thanks Greg, it did decrease the amount of code (i wrote this constructor because our book instructed us to do so the x, y, r and antal can't be negative, however in this case i think you're right)

    But my problem still remains, the "Klick"-class paint 4 circles in diffrent colors but the "Sikte"-class doesn't change color when the:
    private void formMouseClicked(java.awt.event.MouseEvent evt) {                                  
            klick[antalKlick++] = new Klick(evt.getX(), evt.getY(),10);        
            repaint();       
        }
    ...action is used. "antalKlick" is increasing and it impact "Klick"-class (since it change the painted color) but not the "Sikte"-class

  5. #5
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: How do I change the color of my cursor?

    Quote Originally Posted by Chokladmunken View Post
    But my problem still remains, the "Klick"-class paint 4 circles in diffrent colors but the "Sikte"-class doesn't change color when the:
    private void formMouseClicked(java.awt.event.MouseEvent evt) {                                  
            klick[antalKlick++] = new Klick(evt.getX(), evt.getY(),10);        
            repaint();       
        }
    Who/where invokes formMouseClicked?


    And the fact that antalKlick is a "class" (static) variable is also a bad design.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

Similar Threads

  1. Trying To Change The Color Of JScrollBar
    By Damian3395 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 4th, 2013, 06:40 PM
  2. How to change color of JMenuItem when mouse is over it
    By Bagzli in forum AWT / Java Swing
    Replies: 5
    Last Post: April 2nd, 2012, 02:09 PM
  3. Change font color and size
    By javanovice in forum AWT / Java Swing
    Replies: 2
    Last Post: April 20th, 2010, 09:57 AM
  4. Change of color for selected text in AWT
    By venkyInd in forum AWT / Java Swing
    Replies: 2
    Last Post: April 9th, 2010, 03:51 AM
  5. Checkbox - Change Font Color
    By wakebrdr77 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2010, 09:57 AM