Forum Stats

  • 3,837,134 Users
  • 2,262,229 Discussions
  • 7,900,211 Comments

Discussions

Can't figure out how to paint a rectangle in Java 11

4206567
4206567 Member Posts: 1
edited Mar 7, 2020 12:55AM in Java Programming

So I'm making a little personal Pokemon spinoff for the purpose of learning Java, but I can't seem to do something as simple as drawing a rectangle. I'm sure it's some absolutely idiotic error on my part (or a collection of some), but I've been trying to figure it out for hours now and can't come up with any fix. I'm still pretty new to Java but not new to the basic concepts of programming. The problem isn't that it's giving me a fatal error, but it is giving me a null pointer exception. The frame does come up but nothing is inside of it..Could you help out this little noob?

Here's the code for the thing. I just gave you the whole file in case anything was important.

package com.happyhippo77.pokemon_qo.main;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class Main extends Canvas{   public static void main(String[] args) {// Config Setup
//----------------------------------------------------------------------------------------------------------------------
   File config = new File("config.properties");
   Properties prop = new Properties();

  try {  FileInputStream ip = new FileInputStream(config);
   prop.load(ip);
   } catch (IOException ex) {  ex.printStackTrace();
   }//----------------------------------------------------------------------------------------------------------------------

   System.out.println(prop.getProperty("music"));
   GameFunctions();
   }   public static void GameFunctions() {// Background Setup
//----------------------------------------------------------------------------------------------------------------------
   Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
   JFrame frame = new JFrame("Pokemon Quarts/Onyx");
   Canvas canvas = new Canvas();
   canvas.setSize(frame.getSize());
   frame.setSize((int) screenSize.getWidth() / 2, (int) screenSize.getHeight() / 2);
   frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.addKeyListener(new Input());
   frame.setVisible(true);
   canvas.setVisible(true);
   canvas.paint(canvas.getGraphics());
   System.out.println(canvas.getSize());


//----------------------------------------------------------------------------------------------------------------------
   }   @Override
   public void paint(Graphics g) {  g.setColor(Color.red);
   g.fillRect(0, 0, 2000, 2000);
   }}class Input extends KeyAdapter {   @Override
   public void keyPressed(KeyEvent event) {   char input = event.getKeyChar();

  if (input == 'w') {  System.out.println("You'd be walking forwards right now");
   }  }}
Tagged:

Answers