gpt4 book ai didi

java - 制作一个将绘图添加到 JFrame 的按钮

转载 作者:行者123 更新时间:2023-12-01 12:16:18 25 4
gpt4 key购买 nike

所以,我的代码链接到其他一些名为 SkeletonHand 的类, CrossHeadstone 。基本上,这部分代码充当所有类的管理器,调用我提到的三个类中的每个类中的绘制方法,然后在 JFrame 上绘制骨架手、十字架或墓碑的图片。 (代表墓地)。底部有几个按钮,我还设置了它们来更改背景颜色。

我的问题,也就是我现在正在尝试做的事情,是如何在创建的最后一个按钮( HandClicker )中创建事件,以便每当按下按钮时都会创建一个新的骨架手?

所以,我想创建一个名为“复活死者”的按钮,每当按下该按钮时,都会在页面上创建一个新的骷髅手。手将处于随机的 X 位置。但我知道该怎么做。首先我需要知道如何创建事件。有什么有用的见解吗?我将发布其他类(class),以便显示大图。

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);
}
}
}

------骷髅手------

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
}
}

------十字-----

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? 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.

创建一个List作为实例字段...

private java.util.List<SkeletonHand> hands = new java.util.ArrayList<>(25);

当调用 HandClickerActionListener 时,将 SkeletonHand 的新实例添加到 List 中。 ..

public void actionPerformed (ActionEvent event)
{
hands.add(new SkeletonHand (340));
repaint();
}

作为绘画过程的一部分,循环遍历此列表并绘制手...

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);

}

关于java - 制作一个将绘图添加到 JFrame 的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26987669/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com