Graphics realizes several effects of discoloration, gradient, shadow, tilt, and stereo; after understanding these effects, it is easy to realize the click verification code and prepare for the next article.
package cn; import java.awt.Color; import java.awt.EventQueue; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.util.Random; import javax.swing.JFrame; import javax.swing.JPanel; /** * Text that will change color */ public class ChangeColorTextFrame extends JFrame { private ChangeColorTextPanel changeColorTextPanel = new ChangeColorTextPanel(); public static void main(String args[]) { EventQueue.invokeLater(new Runnable() { public void run() { try { ChangeColorTextFrame frame = new ChangeColorTextFrame(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace (); } } }); ChangeColorTextFrame frame=new ChangeColorTextFrame(); frame.setVisible(true); } public ChangeColorTextFrame() { super(); setBounds(100, 100, 400, 205); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setTitle("Text that will change color"); getContentPane().add(changeColorTextPanel); Thread thread = new Thread(changeColorTextPanel);// Create a thread object thread.start();// start the thread object } class ChangeColorTextPanel extends JPanel implements Runnable { // Create inner panel class Color color = new Color(0, 0, 255); public void paint(Graphics g) { // Override paint() method Graphics2D g2 = (Graphics2D) g;// Convert to Graphics2D type String value = ""Video Learning Java Programming"";//The drawn text int x = 2; // abscissa of the text position int y = 90; // vertical coordinate of text position Font font = new Font("Kaishi", Font.BOLD, 40); // create a font object g2.setFont(font); // set font g2.setColor(color);// set the color g2.drawString(value, x, y); // draw text } public void run() { Random random = new Random();// Create a random number object while (true) { int R = random.nextInt(256);// Randomly generate the R value of the color int G = random.nextInt(256);// Randomly generate the G value of the color int B = random.nextInt(256);// Randomly generate the B value of the color color = new Color(R, G, B);// create a color object repaint();// call paint() method try { Thread.sleep(300);// sleep for 300 milliseconds } catch (InterruptedException e) { e.printStackTrace (); } } } } }
package cn; import java.awt.Color; import java.awt.EventQueue; import java.awt.Font; import java.awt.GradientPaint; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JFrame; import javax.swing.JPanel; /** * Text with gradient effect */ public class GradientTextFrame extends JFrame { private GradientTextPanel gradientTextPanel = new GradientTextPanel(); public static void main(String args[]) { EventQueue.invokeLater(new Runnable() { public void run() { try { GradientTextFrame frame = new GradientTextFrame(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace (); } } }); } public GradientTextFrame() { super(); setBounds(100, 100, 365, 205); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setTitle("Text with gradient effect"); getContentPane().add(gradientTextPanel); } class GradientTextPanel extends JPanel { // Create inner panel class public void paint(Graphics g) { // Override paint() method Graphics2D g2 = (Graphics2D) g;// Convert to Graphics2D type String value = "Java Almighty";// Text drawn int x = 15; // abscissa of the text position int y = 60; // vertical coordinate of text position Font font = new Font("Kaishi", Font.BOLD, 60); // create a font object g2.setFont(font); // set font // Create a GraphientPaint object with a circular gradient GradientPaint paint = new GradientPaint(20, 20, Color.BLUE, 100,120, Color.RED, true); g2.setPaint(paint);// set the gradient g2.drawString(value, x, y); // draw text font = new Font("Chinese Xingkai", Font.BOLD, 60); // create a new font object g2.setFont(font); // set font x = 80; // abscissa of the text position y = 130; // vertical coordinate of text position value = "Programming Dictionary";// Text drawn g2.drawString(value, x, y); // draw text } } }
package cn; import java.awt.Color; import java.awt.EventQueue; import java.awt.Font; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; /** * Text with shadow effect */ public class ShadowTextFrame extends JFrame { private ShadowTextPanel shadowTextPanel = new ShadowTextPanel(); public static void main(String args[]) { EventQueue.invokeLater(new Runnable() { public void run() { try { ShadowTextFrame frame = new ShadowTextFrame(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace (); } } }); } public ShadowTextFrame() { super(); setBounds(100, 100, 354, 205); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setTitle("Text with shadow effect"); getContentPane().add(shadowTextPanel); } class ShadowTextPanel extends JPanel { // Create inner panel class public void paint(Graphics g) { // Override paint() method String value = "Programming Dictionary"; int x = 16; // abscissa of the text position int y = 100; // vertical coordinate of text position Font font = new Font("Chinese Xingkai", Font.BOLD, 72); // create a font object g.setFont(font); // set font g.setColor(Color.GRAY);// Set the color to gray int i = 0; // loop variable g.drawString(value, x, y); // draw the text x -= 3;// Adjust the abscissa value of the drawn point y -= 3;// Adjust the ordinate value of the drawn point g.setColor(Color.BLACK);// Set the color black g.drawString(value, x, y); // draw the text } } }
package cn; import java.awt.EventQueue; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JFrame; import javax.swing.JPanel; /** * Tilt effect text */ public class ShearTextFrame extends JFrame { private ShearTextPanel shearTextPanel = new ShearTextPanel(); public static void main(String args[]) { EventQueue.invokeLater(new Runnable() { public void run() { try { ShearTextFrame frame = new ShearTextFrame(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace (); } } }); } public ShearTextFrame() { super(); setBounds(100, 100, 365, 205); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setTitle("Tilt effect text"); getContentPane().add(shearTextPanel); } class ShearTextPanel extends JPanel { // Create inner panel class public void paint(Graphics g) { // Override paint() method Graphics2D g2 = (Graphics2D)g;// Convert to Graphics2D type String value = "Programming Dictionary";// Text drawn int x = 10; // abscissa of the text position int y = 170; // vertical coordinate of text position Font font = new Font("Chinese Xingkai", Font.BOLD, 72); // create a font object g2.setFont(font); // set font g2.shear(0.1, -0.4);// tilt the canvas g2.drawString(value, x, y); // draw text } } }
package cn; import java.awt.Color; import java.awt.EventQueue; import java.awt.Font; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; /** * Three-dimensional effect text */ public class SolidTextFrame extends JFrame { private SolidTextPanel solidTextPanel = new SolidTextPanel (); public static void main(String args[]) { EventQueue.invokeLater(new Runnable() { public void run() { try { SolidTextFrame frame = new SolidTextFrame (); frame.setVisible(true); } catch (Exception e) { e.printStackTrace (); } } }); } public SolidTextFrame () { super(); setBounds(100, 100, 354, 205); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setTitle("Text with three-dimensional effect"); getContentPane().add(solidTextPanel); } class SolidTextPanel extends JPanel { // Create inner panel class public void paint(Graphics g) { // Override paint() method String value = "Tomorrow Technology"; int x = 16; // abscissa of the text position int y = 100; // vertical coordinate of text position Font font = new Font("宋体", Font.BOLD, 72); // create a font object g.setFont(font); // set font g.setColor(Color.GRAY);// Set the color to gray int i = 0; // loop variable while (i<8){ g.drawString(value, x, y); // draw the text x+=1;// Adjust the abscissa value of the drawing point y+=1;// Adjust the ordinate value of the drawing point i++;// Adjust the value of the loop variable } g.setColor(Color.BLACK);// Set the color black g.drawString(value, x, y); // draw the text } } }
reward
If you think my article is helpful to you, if you have money, you can support yourself. If you have no money, you can support yourself. You are welcome to like or forward it, and please indicate the original source, thank you....