I’ve been making a 2D java game for a history project, but rectangles randomly stoped being drawn
The code:
import java.awt.Color;
import javax.swing.JFrame;
public class Main {
public static void main(String[] args){
JFrame jf = new JFrame();
Game g = new Game();
g.play = true;
jf.setBackground(Color.black);
jf.setTitle("History");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setBounds(10, 10, 800, 600);
jf.setResizable(false);
jf.setAlwaysOnTop(true);
jf.setVisible(true);
jf.add(g);
}
}
second class:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Game extends JPanel implements KeyListener, ActionListener, MouseListener{
public boolean play = false;
private static boolean clickHold = false;
private boolean start = true;
public Game(){
addKeyListener(this);
addMouseListener(this);
setFocusable(true);
Timer timer = new Timer(5, this);
timer.start();
}
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
if(start){
start = false;
g.setColor(Color.black);
g.drawRect(0, 0, 1000, 1000);
g.setColor(Color.WHITE);
g.setFont(new Font("serif", Font.BOLD, 50));
g.drawString("Copernicus Simulator 9000", 200, 200);
}
}
@Override
public void mouseClicked(MouseEvent e) {
clickHold = true;
}
@Override
public void mouseReleased(MouseEvent e) {
clickHold = false;
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void actionPerformed(ActionEvent e) {
}
}
is there any reason that the rectangles are not drawing?
when i run the code, it simply displays a empty white window.
if anyone could answer this, it would help me a bunch.
P.S. It would be great if this was answered sooner that later
New contributor
Dax Eschweiler is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1