Discussions
Categories
- 197.1K All Categories
- 2.5K Data
- 546 Big Data Appliance
- 1.9K Data Science
- 450.7K Databases
- 221.9K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 552 MySQL Community Space
- 479 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.1K ORDS, SODA & JSON in the Database
- 555 SQLcl
- 4K SQL Developer Data Modeler
- 187.2K SQL & PL/SQL
- 21.3K SQL Developer
- 296.3K Development
- 17 Developer Projects
- 139 Programming Languages
- 293K Development Tools
- 110 DevOps
- 3.1K QA/Testing
- 646.1K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 158 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.2K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 19 Java Essentials
- 162 Java 8 Questions
- 86K Java Programming
- 81 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 466 LiveLabs
- 39 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 175 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 233 Portuguese
Can't figure out how to paint a rectangle in Java 11

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");
} }}
Answers
-
why not look at https://www.oracle.com/technetwork/java/javase/overview/javafx-samples-2158687.html ?
any way, I believe the tutorial (https://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) covers what you are looking for.