简体   繁体   中英

Change the color of a word in a applet when it is clicked

What I need to have happen is that when the user clicks the word "Java" in the middle of the pink circle, it will change the words color from black to red. My issue is that I don't know how to do it, and I lost my Java book and am waiting for one to come in the mail so I'm trying to work through online forums but just wasn't finding a good example to use. Any help or links to other examples would be much appreciated!

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Circle extends JApplet{


public void inti()
{
    getContentPane().setBackground(Color.white);
}

public void paint(Graphics g)
{
    super.paint(g);

    g.setColor(Color.black);
    g.drawOval(20, 20, 140, 140);
    g.setColor(Color.pink);
    g.fillOval(20,20,140,140);
    g.setColor(Color.BLACK);
    g.setFont(new Font("SansSerif",Font.BOLD, 25));
    g.drawString("Java", 60, 95);
}
}
  1. (You need to first make sure that your write all method names correctly and your code doesn't have any typos. For example you init method has a typo: inti() ).

  2. Then you need to ensure that your applet class implements the Runnable "Functional Interface", as well as the MouseListener Interface.

  3. Then you would need to override or implement the abstract methods for each or those Interfaces

  4. Since you need to change the color of your text when a mouse click event occurs, then you should override mouseClicked to perform your desired action

  5. Make sure to repaint() so that your changes takes effect appropriately. Also, ensure that the run() method repaints the shape with the desired frequency

Here's the final solution:

public class Main extends JApplet implements Runnable, MouseListener {

//this member field will specify what color should be the text
//in every painting cycle
private Color textColor = Color.BLACK;


@Override
public void paint(Graphics g) {

    g.setColor(Color.black);
    g.drawOval(20, 20, 140, 140);
    g.setColor(Color.pink);
    g.fillOval(20, 20, 140, 140);
    g.setColor(Color.BLACK);

    g.setColor(textColor);
    g.setFont(new Font("SansSerif", Font.BOLD, 25));
    g.drawString("Java", 60, 95);
}

@Override
public void init() {
    //screen size --modify it as desired
    this.setSize(200, 200);
    getContentPane().setBackground(Color.white);
    addMouseListener(this);
}

public void run() {
    while ( true ) {
        repaint();

        try {
            Thread.sleep(17);  //specifies repaint frequency
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

@Override
public void mouseClicked(MouseEvent e) {
    int x = e.getX(), y = e.getY();

    if ( x >= 60 && x <= 120 && y >= 80 && y <= 95 )
        textColor = Color.RED;
    else
        textColor = Color.black;

    repaint();


    //you can use this to change the condition of the if statement as you desire
    System.out.println("mouse clicked: x="+x+ " --- y="+y);
}

@Override
public void mousePressed(MouseEvent e) {}

@Override
public void mouseReleased(MouseEvent e) {}

@Override
public void mouseEntered(MouseEvent e) {}

@Override
public void mouseExited(MouseEvent e) {}
}

(Thanks to Alex's solution for clarifying what the question was actually asking for!)

This takes several steps.

  1. make the textcolor a field of the class

     private Color textcolor = Color.BLACK; 
  2. let the paint method paint take the value, then its been called

     g.setColor(textcolor); g.setFont(new Font("SansSerif",Font.BOLD, 25)); g.drawString("Java", 60, 95); 
  3. implement the Mouselistener Interface

     public class Circle extends JApplet implements MouseListener{ 
  4. Initilize the class with a Mouselistener

     public void inti() { addMouseListener(this); getContentPane().setBackground(Color.white); } 
  5. Add the Method mouseClicked

     mouseClicked(MouseEvent e){ e.GetX(); e.GetY(); // Get the Clickcoordinates and check if its inside the circle if(//Inside the cirle){ textcolor = Color.RED; } else{ textcolor = Color.BLACK; } // Make the class refrech its content repaint(); } 

That would be my first raw sketch of the solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM