Я создаю JFrame и JPanel внутри него. Я реализовал paintComponent, но он ничего не показывает, просто появляется пустой JFrame, я запутался с ним. Изображение, когда программа запускается
Это код JPanel
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
/**
*
* @author nguyencong
*/
public class RobotWorld extends JPanel {
public Robot robot;
public PlayField field;
public RobotWorld(Robot robot , PlayField field) {
super();
this.robot = robot;
this.field = field;
this.setSize(field.width , field.height);
this.setVisible(true);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D graphic = (Graphics2D)g;
graphic.setBackground(field.fill_Color);
graphic.setColor(robot.color);
graphic.drawOval(robot.x, robot.y, 4, 4);
}
}
И это код JFrame:
import java.awt.Color;
import javax.swing.JFrame;
/**
*
* @author nguyencong
*/
public class GameMain extends JFrame {
public void Game_Start()
{
Robot a = new Robot(10, 10, Color.yellow);
PlayField field = new PlayField(500, 500, Color.BLACK);
RobotWorld world = new RobotWorld(a, field);
this.setSize(field.width , field.height);
this.setLayout(null);
this.add(world);
world.setBounds(0, 0, world.field.width, world.field.height);
this.setVisible(true);
world.repaint();
}
public static void main(String args[])
{
GameMain main = new GameMain();
main.Game_Start();
}
}
И этоКод класса робота
import java.awt.Color;
/**
*
* @author nguyencong
*/
public class Robot {
public int x;
public int y;
public Color color;
public final int speed = 2;
Robot(int x , int y , Color color)
{
this.x = x;
this.y = y;
this.color = color;
}
public void move()
{
}
}
Код класса игрового поля:
import java.awt.Color;
/**
*
* @author nguyencong
*/
public class PlayField {
public int width;
public int height;
public Color fill_Color;
PlayField(int width , int height , Color fill_Color)
{
this.width = width;
this.height = height;
this.fill_Color = fill_Color;
}
}
Что с ними не так ??