Person GUI Java Program
Java Code:
import java.awt.*;
import
java.awt.event.MouseAdapter;
import
java.awt.event.MouseEvent;
import javax.swing.*;
public class Person extends JPanel{
int numLeft;
public Person()
{
this.addMouseListener(new MouseAdapter() {
public void
mouseClicked(MouseEvent e) {
showNext();
}
});
numLeft = 7;
}
public int getNumLeft()
{
return numLeft;
}
public void reset()
{
numLeft = 0;
repaint();
}
public void showNext()
{
numLeft--;
repaint();
}
private void paintHead(final Graphics g)
{
g.setColor(Color.red);
g.fillRect(155,
50, 100, 105);
g.setColor(Color.BLACK);
g.fillRect(180,
75, 10, 10);
g.fillRect(215,
75, 10, 10);
g.fillRect(200,
95, 7, 10);
g.fillRect(190,
120, 29, 10);
}
private void paintBody(final Graphics g)
{
g.setColor(Color.black);
g.fillOval(130,
150, 150, 210);
g.setColor(Color.white);
g.fillRect(140,
300, 130, 10);
g.fillRect(200,
180, 10, 10);
g.fillRect(200,
200, 10, 10);
g.fillRect(200,
220, 10, 10);
g.fillRect(200,
240, 10, 10);
g.fillRect(200,
260, 10, 10);
}
private void paintLeftArm(final Graphics g)
{
g.setColor(Color.gray);
g.fillRect(270,
220, 20, 140);
g.setColor(Color.white);
g.fillRect(270,
340, 20, 10);
}
private void paintRightArm(final Graphics g)
{
g.setColor(Color.gray);
g.fillRect(120,
220, 20, 140);
g.setColor(Color.white);
g.fillRect(120,
340, 20, 10);
}
private void paintLeftLeg(final Graphics g)
{
g.setColor(Color.LIGHT_GRAY);
g.fillRoundRect(220,
350, 20, 150, 25,25);
g.setColor(Color.white);
g.fillRect(220,
495, 25, 20);
}
private void paintRightLeg(final Graphics g)
{
g.setColor(Color.LIGHT_GRAY);
g.fillRoundRect(180,
350, 20, 150, 25,25);
g.setColor(Color.white);
g.fillRect(180,
495, 25, 20);
}
public void paintComponent(final Graphics g)
{
if (numLeft == 0)
{
super.paintComponent(g); // clears the panel
numLeft = 7;
}
if (numLeft == 6) paintHead(g);
if (numLeft == 5) paintBody(g);
if (numLeft == 4)
paintLeftArm(g);
if (numLeft == 3)
paintRightArm(g);
if (numLeft == 2)
paintLeftLeg(g);
if (numLeft == 1)
paintRightLeg(g);
}
public static void main(String[] args)
{
// TODO Auto-generated
method stub
JFrame
frame = new JFrame("NAME");
frame.setSize(400, 600);
frame.setLocation(300, 200);
frame.add(new Person()); // add the Person
panel to the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
No comments:
Post a Comment