JAVA learning drawing color and stroke attributes set font display text, java Set Font

Source: Internet
Author: User

JAVA learning drawing color and stroke attributes set font display text, java Set Font

Package com. graphics; import java. awt. *; import java. awt. geom. rectangle2D; import java. util. date; import javax. swing. *;/***** @ author biexiansheng **/public class DrawString extends JFrame {private Shape rect; // rectangular object private Font font; // Font object private Date date; // current date object public DrawString () {rect = new Rectangle2D. double (10, 10,200, 80); font = new Font ("", Font. BOLD, 16); date = new Date (); this. setSize (230,140); // set the size of the form // set the form closing method setdefaclocloseoperation (WindowConstants. EXIT_ON_CLOSE); add (new CanvasPanel (); // set the Form Panel as the drawing panel object this. setTitle ("Drawing text"); // set the Form title} class CanvasPanel extends JPanel {public void paint (Graphics g) {super. paint (g); Graphics2D g2 = (Graphics2D) g; // force type conversion g2.setColor (Color. CYAN); // set the current drawing Color g2.fill (rect); // fill the rectangle g2.setColor (Color. blue); // set the current drawing color g2.setFont (font); // set the font g2.drawString ("the current time is", 20, 30); // draw the text g2.drawString (String. format ("% tr", date), 50, 60); // draw event text} public static void main (String [] args) {// TODO Auto-generated method stub DrawString ds = new DrawString (); // ds. setVisible (true );}}

The instance running result is as follows:

 

Package com. graphics; import java. awt. canvas; import java. awt. graphics; import java. awt. graphics2D; import java. awt. image; import java. awt. toolkit; import java.net. URL; import javax. swing. JFrame; import javax. swing. windowConstants;/**** 1: Drawing Images * The drawing class can not only draw graphics and text, but also use drawImage () the method is to display Image resources in the drawing context * and implement various special effects, such as film scaling and flip * syntax * drawImage (Image img, int x, int y, imageObserver observer); * This method places the img Image on x and y, and the observer is the Image observer to be notified */public class DrawImage extends JFrame {Image img; public DrawImage () {URL url = DrawImage. class. getResource ("1.jpg"); // obtain the image resource path img = Toolkit. getdefatooltoolkit (). getImage (url); // obtain the image resource this. setSize (250,200); // set the size of the form // set the form closing method setdefaclocloseoperation (WindowConstants. EXIT_ON_CLOSE); add (new CanvasPanel (); // set the Form Panel as the drawing panel object this. setTitle ("Drawing Image");} class CanvasPanel extends Canvas {public void paint (Graphics g) {super. paint (g); Graphics2D g2 = (Graphics2D) g; g2.drawImage (img, this); // display image} public static void main (String [] args) {DrawImage di = new DrawImage (); // di. setVisible (true );}}

The running result of the Image Rendering instance is as follows:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.