?? showemotion.java
字號(hào):
import java.awt.*;
import javax.swing.*;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Line2D;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import java.awt.Toolkit;
import java.awt.Image;
import java.awt.Dimension;
import java.awt.Container;
import java.awt.Graphics;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.event.*;
public class ShowEmotion extends JFrame
{
private JPanel p1;
private DrawPanel p2;
String choice="angry";
public static void main(String[] args)
{
ShowEmotion frame=new ShowEmotion();
frame.setTitle("the front view of a microwave oven");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setVisible(true);
}
public ShowEmotion()
{
setLayout(new BorderLayout());//set border Layout for the frame
p1=new JPanel();
p1.setLayout(new GridLayout(1,4,5,5));
JButton Bt=new JButton("流淚");
JButton Bs=new JButton("微笑");
JButton Ba=new JButton("生氣");
JButton Bc=new JButton("退出");
Ba.addActionListener(new ButtonAction("angry"));
Bt.addActionListener(new ButtonAction("tears"));
Bs.addActionListener(new ButtonAction("smile"));
Bc.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}});
p1.add(Bt);
p1.add(Bs);
p1.add(Ba);
p1.add(Bc);
add(p1,BorderLayout.SOUTH);
p2=new DrawPanel();
add(p2,BorderLayout.CENTER);
}
class ButtonAction implements ActionListener
{
private String str;
public ButtonAction(String str){this.str=str;}
public void actionPerformed(ActionEvent e)
{
choice=str;
repaint( );
}
}
class DrawPanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
if(choice.equals("angry"))
new Angry().draw(g);
else if(choice.equals("tears"))
new Tears().draw(g,getWidth(),getHeight()); //增加,getWidth(),getHeight() Angry是默認(rèn)的表情
else if(choice.equals("smile"))
new Smile().draw(g,getWidth(),getHeight());
}
}
}
class Angry
{
public void draw(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
g2.drawLine(200,150,160,120);
g2.drawLine(200,150,240,120);
g2.fillArc(110,80,200,160,0,360);
g2.setColor(Color.white);
g2.fillArc(150,110,50,60,150,180);
g2.fillArc(200,110,50,60,210,180);
g2.drawArc(160,200,100,60,20,150);
g2.setColor(Color.black);
g2.fillArc(185,150,10,10,0,360);
g2.fillArc(205,150,10,10,0,360);
}
}
class Tears
{
public void draw(Graphics g,int width,int height)
{
Graphics2D g2 = (Graphics2D)g;
int xc=width/2;
int yc=height/2;
// int radius=(int)(Math.min(getWidth(),getHeight())*0.4);
//draw mouth
int rmouth=10;
int x=xc-rmouth;
int y=yc-rmouth;
g2.drawArc(x,y-8,5*rmouth,3*rmouth,-5,190);
//draw eyes
int reye=20;
int xe1=xc-40-reye;
int ye1=yc-70-reye;
g2.drawArc(xe1,ye1,3*reye,2*reye,30,120);//draw left eye
g2.drawArc(xe1+10,ye1+10,2*reye,1*reye,190,160);
g2.drawArc(xe1+80,ye1,3*reye,2*reye,30,120);//draw right eye
g2.drawArc(xe1+90,ye1+10,2*reye,1*reye,190,160);
g2.drawArc(xc-90,yc-120,200,160,0,360);//draw head
g2.fillArc(xe1+110,ye1+50,3,6,0,360);//draw tears
g2.fillArc(xe1+30,ye1+70,4,8,0,360);
g2.drawArc(xe1+112,ye1+60,3,4,0,360);
g2.drawArc(xe1+25,ye1+30,5,9,0,360);
}
}
class Smile
{
public void draw(Graphics g,int width,int height)
{
Graphics2D g2 = (Graphics2D)g;
int xc=width/2;
int yc=height/2;
//draw mouth
int rmouth=20;
int x=xc-rmouth;
int y=yc-rmouth;
g2.drawArc(x,y-8,3*rmouth,2*rmouth,200,140);
g2.drawArc(x,y,3*rmouth,2*rmouth,170,200);
//draw eyes
int reye=20;
int xe1=xc-40-reye;
int ye1=yc-70-reye;
g2.drawArc(xe1,ye1,3*reye,2*reye,30,120);//draw left eye
g2.drawArc(xe1+10,ye1+15,2*reye,1*reye,10,160);
g2.drawArc(xe1+10,ye1+10,2*reye,1*reye,190,160);
g2.drawArc(xe1+80,ye1,3*reye,2*reye,30,120);//draw right eye
g2.drawArc(xe1+90,ye1+15,2*reye,1*reye,10,160);
g2.drawArc(xe1+90,ye1+10,2*reye,1*reye,190,160);
g2.fillArc(xe1+102,ye1+15,17,15,0,360);//draw left eyeball
g2.fillArc(xe1+22,ye1+15,17,15,0,360);//draw right eyeball
g2.drawArc(xc-90,yc-120,200,160,0,360);//draw head
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -