图形中的按钮

时间:2015-03-27 00:02:19

标签: java button graphics awt paint

我的最后一个问题可能很长,所以我决定改变它 我正在寻找制作按钮或听众的方法,这些按钮或听众在按下之后才会显示这些图形,我刚开始编程,所以我不知道如何准确地完成它。 任何帮助将不胜感激:)

 import java.applet.Applet; 
import java.awt.*; 

 //The applet code
 public class Main extends Applet {

     /**
     * 
     */
    private static final long serialVersionUID = 1L;

    /**
     * 
     */


    public void init() {  
            setSize(2000,500);  
        }  

     public void paint(Graphics g) {    
    int A = 0;





//Hat
             g.setColor(Color.blue);
             int m[] = {A+170,A+120,A+220};
             int hat[] = {0,35,35};
             int pts = 3;
             g.fillPolygon(m, hat, pts);       
//head
             g.setColor(Color.gray); 
             g.fillRoundRect(123,35,95,95,20,20);   
//Eye one
             g.setColor(Color.blue); 
             g.fillOval(130,50,25,25);
//Eye (inside) one
             g.setColor(Color.black); 
             g.fillOval(137,57,10,10);
//Eyelid right
             g.setColor(Color.cyan); 
             g.fillArc(130,50,26,15,-15,220);   
//Eye two 
             g.setColor(Color.blue); 
             g.fillOval(185,50,25,25);
//Eye (inside)two 
             g.setColor(Color.black); 
             g.fillOval(192,57,10,10);  
//Eyelid left
             g.setColor(Color.cyan); 
             g.fillArc(184,50,26,15,-15,220);   
//Nose
             g.setColor(Color.green); 
             g.fillRoundRect(165,70,10,25,20,20);
//Mouth
             g.setColor(Color.red); 
             g.fillArc(140,90,60,40,20,-220);
//Mouth (inside fill out)
             g.setColor(Color.white); 
             g.fillArc(142,99,55,26,20,-220);   
//Mouth (inside2 middle1)
             g.setColor(Color.red); 
             g.drawArc(142,97,55,20,20,-220); 
//Mouth (inside3 middle2)
             g.setColor(Color.red); 
             g.drawArc(142,98,55,20,20,-220); 
//Neck
             g.setColor(Color.gray); 
             g.fillRect(155,130,30,20);
//Body 
             g.setColor(Color.gray); 
             g.fillRoundRect(100,151,145,70,20,20);
//Body 2 
             g.setColor(Color.gray); 
             g.fillRoundRect(120,185,100,100,20,20);
//Chest 1 (left)
             g.setColor(Color.black); 
             g.fillRoundRect(107,157,60,50,20,20);  
//Chest 2 (right)
             g.setColor(Color.black); 
             g.fillRoundRect(178,157,60,50,20,20);  
//Abs 1 (left)
             g.setColor(Color.black); 
             g.fillRoundRect(140,209,30,22,20,20);  
//Abs 2 (left)
             g.setColor(Color.black); 
             g.fillRoundRect(140,233,30,22,20,20);  
//Abs 3 (left)
             g.setColor(Color.black); 
             g.fillRoundRect(140,257,30,22,20,20);
//Abs 1 (right)
             g.setColor(Color.black); 
             g.fillRoundRect(174,209,30,22,20,20);  
//Abs 2 (right)
             g.setColor(Color.black); 
             g.fillRoundRect(174,233,30,22,20,20);  
//Abs 3 (right)
             g.setColor(Color.black); 
             g.fillRoundRect(174,257,30,22,20,20);
//Arm 1 
             g.setColor(Color.gray); 
             g.fillRoundRect(45,151,50,130,20,20);  
//Hand 1 
             g.setColor(Color.black); 
             g.fillRoundRect(45,250,50,30,20,20);
//Gun top hole
             g.setColor(Color.red); 
             g.fillOval(55,260,30,30);        
//Gun 1 
             g.setColor(Color.blue); 
             g.fillRoundRect(60,270,20,60,20,20);        
//Gun end 
             g.setColor(Color.black); 
             g.fillRect(60,320,20,10);
//Fire 
             g.setColor(Color.red); 
             g.fillOval(63,330,14,40);
//Arm 2
             g.setColor(Color.gray); 
             g.fillRoundRect(250,151,50,130,20,20);
//Hand 2 
             g.setColor(Color.black); 
             g.fillRoundRect(250,250,50,30,20,20);       
//Saw top hole
             g.setColor(Color.red); 
             g.fillOval(260,260,30,30);
//Saw stick
             g.setColor(Color.black); 
             g.fillRoundRect(270,270,10,60,20,20);
//Saw teeth 1
             g.setColor(Color.gray);
             g.fillRect(256,311,38,38);
//Saw teeth 2
             g.setColor(Color.red);
             int a[] = {A+250,A+275,A+300,A+275};
             int saw[] = {330,305,330,355};
             int fourPoints = 4;
             g.fillPolygon(a, saw, fourPoints);    
//Saw middle wheel 
             g.setColor(Color.black);
             g.fillOval(257,312,37,37);
//Saw middle wheel end
             g.setColor(Color.gray);
             g.fillOval(267,322,16,16);
//Pants 
             g.setColor(Color.black); 
             g.fillRect(120,284,101,50);
//Leg 1
             g.setColor(Color.gray); 
             g.fillRoundRect(115,335,50,150,20,20);
//Leg 2 
             g.setColor(Color.gray); 
             g.fillRoundRect(181,335,50,150,20,20);
//feet1
             g.setColor(Color.black); 
             g.fillRoundRect(115,460,50,30,20,20);
//feet2 
             g.setColor(Color.black); 
             g.fillRoundRect(181,460,50,30,20,20);
     }
 }

3 个答案:

答案 0 :(得分:2)

首先至少使用Swing API。我还质疑是否需要使用applet,并建议使用基于窗口的类,它们更容易处理。

首先创建一个自JPanel之类的自定义组件,覆盖它的paintComponent方法并将自定义绘画置于其中。

创建另一个JPanel并为其分配BorderLayout并将其添加到主容器中,这将充当“机器人窗格”的占位符

使用JButton创建BorderLayout,将“内容窗格”和按钮放在顶级容器中(在您的情况下,小程序)

ActionListener附加到按钮,当触发时,创建“机器人窗格”的新实例并将其放在“内容窗格”中

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JPanel;

public class Main extends JApplet {

    private JPanel content;

    public void init() {
        setLayout(new BorderLayout());

        content = new JPanel(new BorderLayout()) {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(500, 2000);
            }
        };
        add(content);

        JButton btn = new JButton("Show me the robot");
        add(btn, BorderLayout.SOUTH);
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                btn.setEnabled(false);
                RobotPane botPane = new RobotPane();
                content.add(botPane);
                revalidate();
                repaint();
            }
        });
    }

    public class RobotPane extends JPanel {

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(500, 2000);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int A = 0;

//Hat
            g.setColor(Color.blue);
            int m[] = {A + 170, A + 120, A + 220};
            int hat[] = {0, 35, 35};
            int pts = 3;
            g.fillPolygon(m, hat, pts);
//head
            g.setColor(Color.gray);
            g.fillRoundRect(123, 35, 95, 95, 20, 20);
//Eye one
            g.setColor(Color.blue);
            g.fillOval(130, 50, 25, 25);
//Eye (inside) one
            g.setColor(Color.black);
            g.fillOval(137, 57, 10, 10);
//Eyelid right
            g.setColor(Color.cyan);
            g.fillArc(130, 50, 26, 15, -15, 220);
//Eye two 
            g.setColor(Color.blue);
            g.fillOval(185, 50, 25, 25);
//Eye (inside)two 
            g.setColor(Color.black);
            g.fillOval(192, 57, 10, 10);
//Eyelid left
            g.setColor(Color.cyan);
            g.fillArc(184, 50, 26, 15, -15, 220);
//Nose
            g.setColor(Color.green);
            g.fillRoundRect(165, 70, 10, 25, 20, 20);
//Mouth
            g.setColor(Color.red);
            g.fillArc(140, 90, 60, 40, 20, -220);
//Mouth (inside fill out)
            g.setColor(Color.white);
            g.fillArc(142, 99, 55, 26, 20, -220);
//Mouth (inside2 middle1)
            g.setColor(Color.red);
            g.drawArc(142, 97, 55, 20, 20, -220);
//Mouth (inside3 middle2)
            g.setColor(Color.red);
            g.drawArc(142, 98, 55, 20, 20, -220);
//Neck
            g.setColor(Color.gray);
            g.fillRect(155, 130, 30, 20);
//Body 
            g.setColor(Color.gray);
            g.fillRoundRect(100, 151, 145, 70, 20, 20);
//Body 2 
            g.setColor(Color.gray);
            g.fillRoundRect(120, 185, 100, 100, 20, 20);
//Chest 1 (left)
            g.setColor(Color.black);
            g.fillRoundRect(107, 157, 60, 50, 20, 20);
//Chest 2 (right)
            g.setColor(Color.black);
            g.fillRoundRect(178, 157, 60, 50, 20, 20);
//Abs 1 (left)
            g.setColor(Color.black);
            g.fillRoundRect(140, 209, 30, 22, 20, 20);
//Abs 2 (left)
            g.setColor(Color.black);
            g.fillRoundRect(140, 233, 30, 22, 20, 20);
//Abs 3 (left)
            g.setColor(Color.black);
            g.fillRoundRect(140, 257, 30, 22, 20, 20);
//Abs 1 (right)
            g.setColor(Color.black);
            g.fillRoundRect(174, 209, 30, 22, 20, 20);
//Abs 2 (right)
            g.setColor(Color.black);
            g.fillRoundRect(174, 233, 30, 22, 20, 20);
//Abs 3 (right)
            g.setColor(Color.black);
            g.fillRoundRect(174, 257, 30, 22, 20, 20);
//Arm 1 
            g.setColor(Color.gray);
            g.fillRoundRect(45, 151, 50, 130, 20, 20);
//Hand 1 
            g.setColor(Color.black);
            g.fillRoundRect(45, 250, 50, 30, 20, 20);
//Gun top hole
            g.setColor(Color.red);
            g.fillOval(55, 260, 30, 30);
//Gun 1 
            g.setColor(Color.blue);
            g.fillRoundRect(60, 270, 20, 60, 20, 20);
//Gun end 
            g.setColor(Color.black);
            g.fillRect(60, 320, 20, 10);
//Fire 
            g.setColor(Color.red);
            g.fillOval(63, 330, 14, 40);
//Arm 2
            g.setColor(Color.gray);
            g.fillRoundRect(250, 151, 50, 130, 20, 20);
//Hand 2 
            g.setColor(Color.black);
            g.fillRoundRect(250, 250, 50, 30, 20, 20);
//Saw top hole
            g.setColor(Color.red);
            g.fillOval(260, 260, 30, 30);
//Saw stick
            g.setColor(Color.black);
            g.fillRoundRect(270, 270, 10, 60, 20, 20);
//Saw teeth 1
            g.setColor(Color.gray);
            g.fillRect(256, 311, 38, 38);
//Saw teeth 2
            g.setColor(Color.red);
            int a[] = {A + 250, A + 275, A + 300, A + 275};
            int saw[] = {330, 305, 330, 355};
            int fourPoints = 4;
            g.fillPolygon(a, saw, fourPoints);
//Saw middle wheel 
            g.setColor(Color.black);
            g.fillOval(257, 312, 37, 37);
//Saw middle wheel end
            g.setColor(Color.gray);
            g.fillOval(267, 322, 16, 16);
//Pants 
            g.setColor(Color.black);
            g.fillRect(120, 284, 101, 50);
//Leg 1
            g.setColor(Color.gray);
            g.fillRoundRect(115, 335, 50, 150, 20, 20);
//Leg 2 
            g.setColor(Color.gray);
            g.fillRoundRect(181, 335, 50, 150, 20, 20);
//feet1
            g.setColor(Color.black);
            g.fillRoundRect(115, 460, 50, 30, 20, 20);
//feet2 
            g.setColor(Color.black);
            g.fillRoundRect(181, 460, 50, 30, 20, 20);
        }

    }
}

了解更多详情

答案 1 :(得分:0)

使用内置的jframe类。 你可以set.contentpane(可见,假)将你的图形放入内容窗格。 然后使用jbutton将内容窗格设置为单击时可见

答案 2 :(得分:0)

为了得到你想要的东西,你需要一个按钮和一个听众。您需要JButtonActionListener。下面你会看到我编写的一些代码来创建一个使用JButtonsActionListeners的二十一点游戏。 ActionListeners会链接到您的JButtons,以便在点击按钮时,ActionListener会执行您想要的任何操作。

  JButton Quit = new JButton("Quit");
  Quit.addActionListener(new QuitListener());
  CenterSub.add(Quit, BorderLayout.SOUTH);

让我们从代码段的第一行开始。我在等号的右侧声明了一个名为JButton的{​​{1}}。在左侧,我完成了初始化,并在实际按钮上显示Quit文本。在这种情况下,程序运行后按钮将显示JButton。第二行向"Quit"添加ActionListener,以便在点击按钮后,JButton中的代码将会执行。在第二行的括号中,我调用了新的ActionListener ActionListener。当我编写Listener代码时,这将是它的名字。最后一行显示我在QuitListener上添加按钮的位置。我调用了JPanel名称(JPanel),然后传递了按钮的名称以及我希望它作为参数定位的位置。

CenterSub

此代码段是我在第一个代码段中添加到private class QuitListener implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } 的{​​{1}}的定义。 QuitListener实现了接口JButton,允许我定义QuitListener方法来执行我想要的操作。 ActionListener是玩家点击actionPerformed(ActionEvent e)按钮。单击该按钮时,方法ActionEvent将执行并调用终止程序的"Quit",从而允许玩家退出游戏。

因此,为了实现您想要达到的目标,请创建actionPerformedSystem.exit(0)并在JButton方法的ActionListener定义中调用您定义的paint方法。为了获得进一步的帮助,我强烈建议您查看Jbutton API以及您可能需要的任何其他API。

这是JButton API

这是我第一次回答问题,所以我希望这有助于您深入了解自己的需求。