Java uses swing to draw a turtle that can walk

Source: Internet
Author: User
Tags stub

The code is as follows:

Import Java.awt.Color;
Import Java.awt.Graphics;
Import java.awt.event.KeyEvent;
Import Java.awt.event.KeyListener;

Import Javax.swing.JFrame;
Import Javax.swing.JPanel;

/**
* @author Brucelong
* Draw the King eight and set the keyboard listener event
* */
public class Turtlemove {

public static void Main (string[] args) {
1. Create a form, size 1366*768
JFrame frame = new JFrame ();
Frame.setsize (1366, 768);

/*//Draw a Board
JPanel panel = new JPanel ();
Set the background color of the panel
Panel.setbackground (Color.Black);
Set up the form add panel
Frame.add (panel); * *
/** Create a panel and set the background color of the panel
* and add to the form * *
Xiaowangba XWB = new Xiaowangba ();
Xwb.setbackground (Color.light_gray);
form to add a keyboard listener event, the listener is a panel
Frame.addkeylistener (XWB);
Frame.add (XWB);

3. Set the form title to: "Draw King eight-personal signature"
Frame.settitle ("Wang eight-wangyunlong");
4. Set the form Close button function
Frame.setdefaultcloseoperation (Jframe.exit_on_close);
5. Setting the form size cannot be changed
Frame.setresizable (FALSE);
2. Set form visible
Frame.setvisible (TRUE);

}

}

Panel class
Class Xiaowangba extends JPanel implements keylistener{
Coordinate position of bastard
int x = 400,y = 200;
Wang 84-legged (foot) coordinates
int fx1 = 400,FY1 = 200;
int FX2 = 400,FY2 = 200;
int fx3 = 400,FY3 = 200;
int FX4 = 400,FY4 = 200;

Moving variables
int f = 0;

Drawing method, first enter paint and then alt+/
@Override//jframe Frame
public void Paint (Graphics g) {
TODO auto-generated Method Stub
Super.paint (g);
1. First step to set the color of the brush
2. Second step drawing the canvas to set the coordinates
/** drawing his head * *
Set Brush color
G.setcolor (Color.yellow);
Red Green Blue
G.setcolor (New Color (0, 100, 0));

Draw a solid ellipse
G.filloval (x, Y, 70, 100);

/** to draw eyes */
Set Brush color
G.setcolor (Color.Black);
G.filloval (x+10, y+30, 10, 10);
G.filloval (x+50, y+30, 10, 10);
/*//Eyes shine
G.setcolor (Color.White);
G.filloval (x+15, Y+30, 4, 4);
G.filloval (x+55, Y+30, 4, 4); * *

/** Draw the tail * *
G.setcolor (New Color (0, 100, 0));
G.filloval (x, y+290, 60, 80);
Sets the same brush color and background color
G.setcolor (Color.light_gray);
G.filloval (x+20, y+300, 60, 80);

/** King 84 Legs */
Upper left
G.setcolor (New Color (0, 100, 0));
G.filloval (fx1-85, fy1+80, 70, 70);

Top Right
G.setcolor (New Color (0, 100, 0));
G.filloval (fx2+85, fy2+80, 70, 70);
Lower left
G.setcolor (New Color (0, 100, 0));
G.filloval (fx3-85, fy3+235, 70, 70);

Lower right
G.setcolor (New Color (0, 100, 0));
G.filloval (fx4+85, fy4+235, 70, 70);
/** draw the King eight shell */
G.setcolor (New Color (0, 50, 0));
G.filloval (x-66, y+70, 200, 240);
/** Drawing Moire */
G.setcolor (Color.Black);
G.drawoval (x-66, y+70, 200, 240);
(upper) left middle right
G.setcolor (Color.yellow);
G.filloval (x-40, y+130, 50, 30);

G.setcolor (Color.Blue);
G.filloval (x+15, y+80, 30, 50);

G.setcolor (Color.yellow);
G.filloval (x+60, y+130, 50, 30);
Middle
G.setcolor (New Color (255, 0, 0));
G.filloval (x+10, y+155, 45, 45);
(bottom) left middle right
G.setcolor (Color.Blue);
G.filloval (x-45, y+195, 50, 30);

G.setcolor (Color.yellow);
G.filloval (x+20, y+220, 30, 50);

G.setcolor (Color.Blue);
G.filloval (x+70, y+195, 50, 30);

Draw a straight line (top)
G.setcolor (Color.Black);
G.drawline (x-10, y+82, x+20, y+158);

G.setcolor (Color.Black);
G.drawline (x+80, y+82, x+43, y+158);

Draw a straight line (middle)
G.setcolor (Color.Black);
G.drawline (x-65, y+180, x+10, y+180);

G.setcolor (Color.Black);
G.drawline (x+133, y+180, x+55, y+180);

Draw Straight line (bottom)
G.setcolor (Color.Black);
G.drawline (x-10, y+298, x+20, y+198);

G.setcolor (Color.Black);
G.drawline (x+80, y+298, x+43, y+198);
}

@Override
public void keytyped (KeyEvent e) {
TODO auto-generated Method Stub

}

@Override
public void keypressed (KeyEvent e) {
Press keyboard events
Test on key
if (E.getkeycode () ==keyevent.vk_up && fy1>0) {
if (fy1<fy2) {
f = 1;
}else {
f = 0;
}

y=y-10;
if (f==0) {
fy1-=20;
fy4-=20;
}if (f==1) {
fy2-=20;
fy3-=20;
}
SYSTEM.OUT.PRINTLN ("Test on Key");
}

If you press the down key
if (E.getkeycode () ==keyevent.vk_down) {
System.out.println ("123");
if (fy1<fy2) {
f = 1;
}else {
f = 0;
}
y+=10;
if (f = = 1) {
fy1+=20;
fy4+=20;
}
if (f = = 0) {
fy2+=20;
fy3+=20;
}
}
If the left button is pressed
if (E.getkeycode () ==keyevent.vk_left &&fx1>99) {
if (FX1&GT;FX2) {
f = 1;
}else {
f = 0;
}
x-=10;
if (f = = 1) {
fx1-=20;
fx4-=20;
}
if (f = = 0) {
fx2-=20;
fx3-=20;
}
}
If the right button is pressed
if (E.getkeycode () ==keyevent.vk_right) {
if (FX1&LT;FX2) {
f = 1;
}else {
f = 0;
}
x+=10;
if (f = = 1) {
fx1+=20;
fx4+=20;
}
if (f = = 0) {
fx2+=20;
fx3+=20;
}
}
/*if (E.getkeycode () ==keyevent.vk_down&& fy1>0) {
y=y+10;
if (fy1<=fy2) {
fy1+=20;
fy4+=20;
}else {
fy2+=20;
fy3+=20;
}
SYSTEM.OUT.PRINTLN ("Test key");
}
if (E.getkeycode () ==keyevent.vk_left && fx1>0) {
x-=10;
if (FX1&GT;=FX2) {
fx1-=20;
fx4-=20;
}else {
fx2-=20;
fx3-=20;
}
SYSTEM.OUT.PRINTLN ("Test left button");
}
if (E.getkeycode () ==keyevent.vk_right && fx1>0) {
x+=10;
if (FX1&LT;=FX2) {
fx1+=20;
fx4+=20;
}else {
fx2-=20;
fx3-=20;
}
System.out.println ("Right-click Test");
}*/
Redraw the Paint method (recall the Paint method)
Repaint ();
}

@Override
public void keyreleased (KeyEvent e) {
TODO auto-generated Method Stub

}

}

The result of the operation is:

Java uses swing to draw a turtle that can walk

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.