Java GUI of Person
Java Code:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Person extends JPanel
{
int numLeft; // number of turns left
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();
}
public static void main(String[] args)
{
JFrame frame = new JFrame("Person");
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);
}
private void paintHead(final Graphics g)
{
g.setColor(Color.red);
g.fillOval(200 - 25, 100, 50, 50);
}
private void paintBody(final Graphics g)
{
g.setColor(Color.blue);
g.fillRect(160, 150, 80, 160);
}
private void paintLeftArm(final Graphics g)
{
g.setColor(Color.green);
g.fillOval(240, 150, 20, 140);
}
private void paintRightArm(final Graphics g)
{
g.setColor(Color.green);
g.fillOval(140, 150, 20, 140);
}
private void paintLeftLeg(final Graphics g)
{
g.setColor(Color.magenta);
g.fillRect(160, 310, 35, 150);
}
private void paintRightLeg(final Graphics g)
{
g.setColor(Color.magenta);
g.fillRect(205, 310, 35, 150);
}
@Override
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);
}
}
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Person extends JPanel
{
int numLeft; // number of turns left
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();
}
public static void main(String[] args)
{
JFrame frame = new JFrame("Person");
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);
}
private void paintHead(final Graphics g)
{
g.setColor(Color.red);
g.fillOval(200 - 25, 100, 50, 50);
}
private void paintBody(final Graphics g)
{
g.setColor(Color.blue);
g.fillRect(160, 150, 80, 160);
}
private void paintLeftArm(final Graphics g)
{
g.setColor(Color.green);
g.fillOval(240, 150, 20, 140);
}
private void paintRightArm(final Graphics g)
{
g.setColor(Color.green);
g.fillOval(140, 150, 20, 140);
}
private void paintLeftLeg(final Graphics g)
{
g.setColor(Color.magenta);
g.fillRect(160, 310, 35, 150);
}
private void paintRightLeg(final Graphics g)
{
g.setColor(Color.magenta);
g.fillRect(205, 310, 35, 150);
}
@Override
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);
}
}
No comments:
Post a Comment