简体   繁体   English

制作一个将图形添加到JFrame的按钮

[英]Making a button that adds a drawing to a JFrame

So, my code is linked to some other classes called SkeletonHand , Cross and Headstone . 因此,我的代码链接到其他一些名为SkeletonHandCrossHeadstone 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). 基本上,这部分代码充当所有类的管理器,在我提到的三个类中的每一个中调用draw方法,然后在JFrame上绘制骨架,十字或墓石的图片(表示一个墓地)。 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? 我现在要尝试的问题是,如何在创建的最后一个按钮( HandClicker )中创建事件,以便在按下按钮时创建新的骨架手?

So, I want to create a button called "Raise the Dead" that whenever pressed creates a new Skeleton hand on the page. 因此,我想创建一个名为“ Raise the Dead”的按钮,每按一次该按钮都会在页​​面上创建一个新的Skeleton手。 The hand will be at a random X pos. 这只手将处于随机X位置。 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------- ------ 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);
   }
}

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? 如何在最后一个创建的按钮(HandClicker)中创建事件,以便每当按下按钮时都创建新的骨架手? So, I want to create a button called "Raise the Dead" that whenever pressed creates a new Skeleton hand on the page. 因此,我想创建一个名为“ Raise the Dead”的按钮,每按一次该按钮都会在页​​面上创建一个新的Skeleton手。 The hand will be at a random X pos. 这只手将处于随机X位置。 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... 创建一个List作为实例字段...

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 .... 调用HandClickerActionListener ,将SkeletonHand的新实例添加到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);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM