how to make character in jframe

Solutions on MaxInterview for how to make character in jframe by the best coders in the world

showing results for - "how to make character in jframe"
Martina
16 Oct 2019
1import java.awt.*;
2import java.awt.event.ActionEvent;
3import java.awt.event.ActionListener;
4import java.awt.event.KeyEvent;
5import java.io.File;
6import java.io.IOException;
7import java.util.*;
8import javax.imageio.ImageIO;
9import javax.swing.*;
10
11public class primary extends JFrame {
12    public primary(){
13
14        //Creates Title Image 
15        JLabel title = new JLabel(" ");
16        ImageIcon tl = new ImageIcon("title.gif");
17        title.setIcon(tl);
18
19        //Creates Start Image
20        final JButton start = new JButton("");
21        ImageIcon st = new ImageIcon("start.gif");
22        start.setIcon(st);
23
24        //Creates Options Image
25        JButton options = new JButton("");
26        ImageIcon opt = new ImageIcon("options.gif");
27        options.setIcon(opt);
28        options.setBackground(Color.BLACK);
29
30        //Create first frame for "Start" button
31        final JPanel p1 = new JPanel();
32        p1.setLayout(new GridLayout(1, 1));
33        p1.add(start, BorderLayout.CENTER);
34
35        //Create second panel for title label
36        final JPanel p2 = new JPanel(new BorderLayout());
37        p2.setLayout(new GridLayout(1, 3));
38        p2.add(title, BorderLayout.WEST);
39
40        //Create third panel for "Options" button
41        final JPanel p3 = new JPanel(new BorderLayout());
42        p3.setLayout(new GridLayout(1, 1));
43        p3.add(options, BorderLayout.SOUTH);
44
45        //Creates fourth panel to organize all other primary
46        final JPanel p4 = new JPanel(new BorderLayout());
47        p4.setLayout(new GridLayout(1, 3));
48        p4.add(p1, BorderLayout.WEST);
49        p4.add(p2, BorderLayout.CENTER);
50        p4.add(p3, BorderLayout.EAST);
51
52
53        //When button is clicked, it changes the level
54        start.addActionListener(new ActionListener() {
55            @Override
56            public void actionPerformed(ActionEvent e) {
57                if(start.isEnabled()) {
58                    remove(p4);
59                    setSize(1440, 500);
60                    add(new ContentPanel1());
61                    validate();
62                }
63                else {
64                    return;
65                }
66            }
67        });
68
69        //Adds fourth panel to frame
70        add(p4, BorderLayout.CENTER);
71    }
72
73    public static void main(String arg[]) {
74        primary frame = new primary();
75
76        //Finds screen size of monitor
77        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
78
79        //Creates the frame
80        frame.setTitle("Cockadoodle Duty: Awakening");
81        frame.setSize(screenSize);
82        frame.setLocale(null); 
83        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
84        frame.setVisible(true);
85        String background = "#000000";
86        frame.setBackground(Color.decode(background));
87    }
88}
89
90class coordinate {
91    public static int x;
92    public static int y;
93}
94
95class ContentPanel1 extends JPanel{
96    Image back = Toolkit.getDefaultToolkit().getImage("back.gif");
97    Image chick = Toolkit.getDefaultToolkit().getImage("chicken.gif");
98    Image corn = Toolkit.getDefaultToolkit().getImage("corn.gif");
99
100    ContentPanel1() {
101        MediaTracker mt = new MediaTracker(this);
102
103        mt.addImage(back, 0);
104        try {
105            mt.waitForAll();
106        } catch (InterruptedException e){
107            e.printStackTrace();
108        }
109    }
110
111    protected void paintComponent(Graphics g){
112        coordinate.x = 20;
113        coordinate.y = 321;
114        super.paintComponent(g);
115        int imwidth = back.getWidth(null);
116        int imheight = back.getHeight(null);
117        g.drawImage(back, 1, 1, null);
118        g.drawImage(chick, coordinate.x, coordinate.y, null);
119        g.drawImage(corn, 700, 337, null);
120    }
121}
similar questions
queries leading to this page
how to make character in jframe