How to add multiple components to a JFrame

喜欢而已 提交于 2020-05-29 07:25:46

问题


I'm trying to add multiple classes to my JFrame "frame" using multiple JPanels showing an enemy (red circle), a player (orange diamond), and a base (an outlined black rectangle) but they are not displaying correctly. I have four classes, "Assignment6" (the name of the assignment and my main), "Enemy" (the class for creating enemies), "Player" (the class for creating and moving the player), and "Base" (the class that creates the base that enemies cannot enter). I have not made a method in "Base" for the enemy's contact collision yet.

Assignment6:

import javax.swing.*;
import java.awt.Color;
import java.awt.FlowLayout;
public class Assignment6 extends JFrame {
    public static void main(String[] args){
        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
        JPanel panel3 = new JPanel();
        panel1.add(new Base());
        panel2.add(new Enemy());
        panel3.add(new Player());
        JFrame frame = new JFrame("Assignment 6");
        frame.setLayout(new FlowLayout());
        frame.setBackground(Color.WHITE);
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.setSize (1000, 1000);
        frame.getContentPane().add(panel1);
        frame.getContentPane().add(panel2);
        frame.getContentPane().add(panel3);
        frame.setVisible (true);
   }
}

Enemy:

import java.awt.Graphics;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.*;
public class Enemy extends JComponent {
    public Enemy(){
        setPreferredSize(new Dimension(100, 100));
    }
    public int createEnemyX(){
        int enemyX = (int)(100 * Math.random());
        return enemyX;
    }
    public int createEnemyY(){
        int enemyY = (int)(100 * Math.random());
        return enemyY;
    }
    @Override
    public void paintComponent(Graphics g){
       super.paintComponent(g);
       g.setColor(Color.RED);
       g.fillOval(createEnemyX(), createEnemyY(), 100, 100);
    }
}   

Player:

import java.awt.Graphics;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.*;
import javax.swing.*;
public class Player extends JComponent implements KeyListener{
    public int height = getHeight();
    public int width = getWidth();
    public int x = 500;
    public int y = 500;
    public Player(){
        setPreferredSize(new Dimension(100, 100));
        addKeyListener(this);
    }
    @Override
    public void paintComponent(Graphics g){
       super.paintComponent(g);
       Color orange = new Color(210, 105,30);
       g.setColor(orange);
       int[]x1 = {x-50, x, x+50, x};
       int[]y1 = {y, y-50, y, y+50};
       g.fillPolygon(x1, y1, 4);
    }
    @Override
    public void keyPressed(KeyEvent e){
        if (e.getKeyCode() == KeyEvent.VK_LEFT)
            x -= 10;
        else if (e.getKeyCode() == KeyEvent.VK_RIGHT)
            x += 10;
        else if (e.getKeyCode() == KeyEvent.VK_UP)
            y -= 10;
        else if (e.getKeyCode() == KeyEvent.VK_DOWN)
            y += 10;
        repaint();
    }
    @Override
    public void keyReleased(KeyEvent e){
        ;
    }
    @Override
    public void keyTyped(KeyEvent e){
        ;
    }
}

Base:

import java.awt.Graphics;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.*;
public class Base extends JComponent {
    public Base(){
        setPreferredSize(new Dimension(200, 200));
    }
    @Override
    public void paintComponent(Graphics g){
       super.paintComponent(g);
       g.setColor(Color.BLACK);
       g.drawRect(400,400,200,200);

    }
}

Once I run the project the enemy(red circle) appears but is cut off by white rectangles. I am assuming that the paintComponent methods are be called, but they are not being rendered.

I've used this question and answer as the main base of my code: How to add multiple classes to a single JFrame


回答1:


Your overall program design looks to be in error and needs to be re-done, and specifically I'm talking about having your drawable entity classes, Base, Enemy, and Player, each extend JComponent, since that allows these components to be drawn within their JComponent and their JComponent only and nowhere else. Instead:

  • Make your drawable entity classes, such as Player, Base, and Enemy implement a common interface, say Drawable, that has a painting method, say draw(Graphics g).
  • Do not have them extend JComponent, JPanel or any other Swing component.
  • Instead create one single class for graphics, say call it DrawingPanel, and have it extend JPanel or JComponent.
  • Within the single paintComponent method of this DrawingPanel, draw each entity, if it is located within the section of the game that requires drawing.
  • Strive to separate your view code, the GUI code, from your program model code, the code that governs your logical entity behaviors and locations, preferably having them in separate classes, and probably even separate packages.

Regarding questions:

I am currently trying to make Drawable interface, what should I put in the method?

The interface would not have any code within the method. Any classes that implement the interface would need to have code that allows the objects of that class to draw themselves.

If I have the DrawingPanel class, would I even need the Drawable interface?

Yes because DrawingPanel would hold a collection of your Drawable objects and would draw them within its paintComponent method, as already mentioned above.



来源:https://stackoverflow.com/questions/33488331/how-to-add-multiple-components-to-a-jframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!