Reputation: 3
So, my code is linked to some other classes called SkeletonHand
, Cross
and Headstone
. Basically this part of the code, which serves as a manager for all the classes, calls the draw methods in each of the three classes I mentioned and then draws pictures of either a skeleton hand, cross, or a headstone on a JFrame
(representing a graveyard). On the bottom are a couple buttons that I have also set to change the background color.
My question, which is what I am trying to do now, is how do I create and event in the last button created (HandClicker
) so that a new skeleton hand is created whenever the button is pressed?
So, I want to create a button called "Raise the Dead" that whenever pressed creates a new Skeleton hand on the page. The hand will be at a random X pos. but that I know how to do. First I need to know how to create the event. Any helpful insight? I'll post the other classes so that the big picture is shown.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton;
public class ManagerPanel extends JPanel
{
private JButton clicker;
private JButton redClicker;
private JButton greenClicker;
private JButton blueClicker;
private JButton handClicker;
private int red, green, blue;
private SkeletonHand skeletonHand1, skeletonHand2;
private Cross cross1, cross2;
private HeadStone headStone1, headStone2;
//-----------------------------------------------------------------
// Constructor: Creates objects.
//-----------------------------------------------------------------
public ManagerPanel()
{
skeletonHand1 = new SkeletonHand (340);
skeletonHand2 = new SkeletonHand (540);
cross1 = new Cross (150, "Andrew");
cross2 = new Cross (530, "Alexis");
headStone1 = new HeadStone (250, "Jose");
headStone2 = new HeadStone (431, "Alex");
clicker = new JButton("Click here");
clicker.addActionListener (new ClickerListener());
redClicker = new JButton("More red!");
redClicker.addActionListener (new RedListener());
greenClicker = new JButton("More green!");
greenClicker.addActionListener (new GreenListener());
blueClicker = new JButton("More blue!");
blueClicker.addActionListener (new BlueListener());
handClicker = new JButton("Raise the dead");
handClicker.addActionListener (new HandListener());
setBackground (Color.blue);
setPreferredSize (new Dimension(1000, 700));
setFont (new Font("Arial", Font.BOLD, 16));
add (clicker);
add (redClicker);
add (greenClicker);
add (blueClicker);
}
//-----------------------------------------------------------------
// Draws this panel by requesting that each graphic draw itself.
//-----------------------------------------------------------------
public void paintComponent (Graphics page)
{
super.paintComponent(page);
Color brown = new Color(160, 82, 45);
page.setColor (brown);
page.fillRect (0, 630, 1000, 70);
skeletonHand1.draw(page);
skeletonHand2.draw(page);
cross1.draw(page);
cross2.draw(page);
headStone1.draw(page);
headStone2.draw(page);
}
private class ClickerListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
red = 0; green = 0; blue = 0;
setBackground (new Color(red, green, blue));
}
}
private class RedListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
red += 20;
setBackground (new Color(red, green, blue));
}
}
private class GreenListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
green += 20;
setBackground (new Color(red, green, blue));
}
}
private class BlueListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
blue += 20;
setBackground (new Color(red, green, blue));
}
}
private class HandListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
skeletonHand1 = new SkeletonHand (340);
}
}
}
------SkeletonHand-------
import java.awt.*;
public class SkeletonHand
{
private final int BASEY = 630;
private int BASEX;
//-----------------------------------------------------------------
// Constructor: Sets up the Skeleton Hand with the specified values.
//-----------------------------------------------------------------
public SkeletonHand (int x)
{
BASEX = x;
}
//-----------------------------------------------------------------
// Draws the Skeleton Hands in the specified graphics context.
//-----------------------------------------------------------------
public void draw (Graphics page)
{
page.setColor (Color.white);
page.drawLine (BASEX, BASEY-1, BASEX, BASEY-20); // arm
page.drawLine (BASEX, BASEY-20, BASEX-5, BASEY-20); // thumb finger
page.drawLine (BASEX, BASEY-20, BASEX-5, BASEY-27); // index finger
page.drawLine (BASEX, BASEY-20, BASEX-2, BASEY-29); // middle finger
page.drawLine (BASEX, BASEY-20, BASEX+1, BASEY-29); // ring finger
page.drawLine (BASEX, BASEY-20, BASEX+4, BASEY-27); // pinky finger
}
}
------Cross-------
import java.awt.*;
public class Cross
{
private final int BASEY = 630;
private int BASEX;
private String name;
//-----------------------------------------------------------------
// Constructor: Sets up the Crosses with the specified values.
//-----------------------------------------------------------------
public Cross (int x, String n)
{
BASEX = x;
name = n;
}
//-----------------------------------------------------------------
// Draws the Cross' in the specified graphics context.
//-----------------------------------------------------------------
public void draw (Graphics page)
{
page.setColor (Color.darkGray);
page.fillRect (BASEX, BASEY-120, 30, 120);
page.fillRect (BASEX-30, BASEY-90, 90, 30);
page.setColor (Color.black);
page.drawString (String.valueOf(name), BASEX-10, BASEY-70);
}
}
-------HeadStone-------
import java.awt.*;
public class HeadStone
{
private final int BASEY = 630;
private int BASEX;
private String name;
//-----------------------------------------------------------------
// Constructor: Sets up the HeadStones with the specified values.
//-----------------------------------------------------------------
public HeadStone (int x, String n)
{
BASEX = x;
name = n;
}
//-----------------------------------------------------------------
// Draws the HeadStones in the specified graphics context.
//-----------------------------------------------------------------
public void draw (Graphics page)
{
page.setColor (Color.darkGray);
page.fillRect (BASEX, BASEY-80, 60, 80);
page.fillOval (BASEX, BASEY-110, 60, 60);
page.setColor (Color.black);
page.drawString (String.valueOf(name), BASEX+10, BASEY-75);
}
}
Upvotes: 0
Views: 69
Reputation: 347334
how do I create an event in the last button created (HandClicker) so that a new skeleton hand is created whenever the button is pressed? So, I want to create a button called "Raise the Dead" that whenever pressed creates a new Skeleton hand on the page. The hand will be at a random X pos. but that I know how to do. First I need to know how to create the event. Any helpful insight? I'll post the other classes so that the big picture is shown.
Create a List
as a instance field...
private java.util.List<SkeletonHand> hands = new java.util.ArrayList<>(25);
When the ActionListener
for HandClicker
is called, add a new instance of SkeletonHand
to the List
....
public void actionPerformed (ActionEvent event)
{
hands.add(new SkeletonHand (340));
repaint();
}
As part of your paint process, loop through this list and paint the hands...
public void paintComponent (Graphics page)
{
super.paintComponent(page);
Color brown = new Color(160, 82, 45);
page.setColor (brown);
page.fillRect (0, 630, 1000, 70);
for (SkeletonHand hand : hands) {
hand.draw(page)l
}
cross1.draw(page);
cross2.draw(page);
headStone1.draw(page);
headStone2.draw(page);
}
Upvotes: 1