So, I have a class with the following main method:
public class Game {
public static void main(String[] args) {
JFrame frame = new JFrame("Ball Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 550);
frame.setLayout(null);
frame.setVisible(true);
}
}
and a class with the method print:
public class Ball {
public void print(Graphics g) {
g.fillOval(20, 20, 30, 30);
}
}
How can I pass the print method at the main class Game?
Sorry for my english, it isn't my first language.
If your class Ball is in another file you will have to import the class.
In your main, you need to instantiate the class Ball as follow:
Ball b = new Ball();
Which will call the default constructor of the class Ball or a constructor you defined.
Then you can call your function print by doing:
b.print(g);
Assuming that g is an instance of Graphics class.
You can make the print method static.
like this
public static void print(Graphics g){...}
Then call it like this Ball.print(g) (assuming that g is instance of Graphics)
Related
I am quite new to Java application development and I want to draw some simple shapes in the JFrame-canvas without using one main class but rather split it into a main class and a class playerRectangle. In the playerRectangle class I created a method paint:
public void paint(Graphics g) {
setSize(500, 500);
g.drawRect(320, 20, 640, 120);
}
I then created an object in some render funtion in the main class and called the paint function of this object.
playerRectangle p1 = new playerRectangle();
p1.paint(null);
But somehow it threw the java.lang.NullPointerException error and didn't render the rect to the screen. When I don't split this function into two classes it works properly even though it throws this error. So please help me and tell me, what I am missing here.
Any research on Youtube and other stuff only told me that I have to create classes like screen etc. but I am not so sure that is helping me.
Thanks for helping
Simple example how to draw Rectangle using standard libraries :
public class SomeClass extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.GREEN);
g.fillRect(0,0,320,220);
}
SomeClass() {
JFrame frame = new JFrame();
frame.serContentPane(this);
frame.setBounds(0,0,320,220);
frame.setVisible(true);
}
}
Class with main method:
public class Main {
public static void main(String[] args) {
new SomeClass();
}
}
public void display() {
pan.repaint();
fen.add(pan);
fen.addKeyListener(this);
fen.setResizable(false);
fen.setTitle("Le Jeu 2D");
img.setText("Coucou");
pan.add(img);
pan.repaint();
pan.setBackground(Color.yellow);
fen.setVisible(true);
fen.setSize(480, 272);
pan.repaint();
fen.revalidate();
}
public void paintComponent(Graphics g) {
System.out.println("zzz");
pan.paint(g);
g.setColor(Color.red);
g.drawRect(10, 10, 10, 10);
}
It doesn't draw anything. Why? I have defined the paint component method, so I don't understand why.
Edit: I edited my code, please take a look
You don't create an instance of your Game class and don't add it to the JFrame.
Here is the Game panel which you are painting on:
Game.java
public class Game extends JPanel {
#Override
public final void paintComponent(final Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
g.drawRect(10, 10, 10, 10);
}
#Override
public Dimension getMinimumSize() {
return new Dimension(300, 300);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
#Override
public Dimension getMaximumSize() {
return new Dimension(300, 300);
}
}
You then need to create an instance of this Game panel and add it to your JFrame:
GameFrame.java
public class GameFrame extends JFrame {
public GameFrame() {
setLocationRelativeTo(null);
setResizable(false);
setTitle("2D Game");
Game game = new Game();
setContentPane(game);
pack();
setVisible(true);
}
}
Then when you create an instance of the JFrame:
Example.java
public class Example {
public static void main(String[] args) {
new GameFrame();
}
}
The panel will be added, and painted:
You never create an instance of the Game class and you never add the Game class to the frame. Even if you did create an instance the size would still be (0, 0) so there would be nothing to paint.
Basically the whole structure of your code is wrong.
I suggest you start over and start with the demo code found in the Swing tutorial on Custom Painting.
The basic structure of your code seems weird.
You could instantiate a JFrame in your main control class, ie. it should be GAME class in this case. Then you can create a new JPanel class and add its object into the JPanel object. Within the JPanel class, you can create all the elements you need and set corresponding parameters. You could also add the event listener in an inner class or a separate class.
I'm new with Java and I'm trying to a class that extends JPanel. I have a updateView() method that is suppose to draw rounded rectangles with text in the middle inside the JPanel. Also, I would need to stock the textfield and the rounded rectangle in a LinkedHashMap. I already got a var for this, I just don't know witch type of var I need to use.
I'have been searching the solution for a while, and all the answers I found are either to complicated for me to understand or just doesn't apply to my case.
I know that it's almost nothing, but here is what I got so far...
package game;
import javax.swing.JPanel;
public class GameNumView extends JPanel
{
private Map<Integer,Integer> backgroundText = new LinkedHashMap<"My rounded rectangle","My textfield">();
public GameNumView()
{
}
public void UpdateView(String[] pNumbers)
{
//Create the background
//Create the text
}
}
I'll give you the right path. It's up to you to adapt it to your existing code.
The idea is to use the Graphics object with the paintComponent method of an extended class of JPanel.
Here is the code, it is clear enough I think but if you have any question, do not hesitate.
The MyFrame class :
public class MyFrame extends JFrame {
public MyFrame(){
this.setTitle("Hello");
this.setSize(200, 200);
this.setLocationRelativeTo(null);
MyPanel pan = new MyPanel();
pan.setBackground(Color.ORANGE);
this.setContentPane(pan);
this.setVisible(true);
}
public static void main(String[] args) {
MyFrame f = new MyFrame();
}
}
And here is the MyPanel class :
public class MyPanel extends JPanel{
#Override
public void paintComponent(Graphics g) {
g.setColor(Color.black);
g.drawRoundRect(10, 10, this.getWidth()-20, this.getHeight()-20, 15, 15);
g.setColor(Color.black);
g.drawString("Hello", 75, 75);
}
}
And here is a picture of what you should have :
I have this code and want to repaint my graphic when the Button gets pressed:
public class JDraw extends JFrame {
/**
* Draws a figur in a JFrame.
* The color of it is random and can get changed by a button.
*/
public static JButton okButton = new JButton("change color");
public JDraw(String newTitel) {
super.setTitle(newTitel);
}
//Main method
public static void main(String str[]) {
JDraw window = new JDraw("Graphic");
window.setSize(300, 450);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.add(okButton, BorderLayout.SOUTH);
okButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//JDraw.repaint(); <-- problem
}
});
}
#Override
public void paint(final Graphics g) {
super.paint(g);
Random rand = new Random();
int r1 = rand.nextInt(255);
int b1 = rand.nextInt(255);
int g1 = rand.nextInt(255);
g.setColor(new Color(r1, g1, b1));
//head
g.drawOval(100, 50, 100, 100);
g.fillOval(100, 50, 100, 100); // filled
//more drawing stuff.....
}
}
However I have no idea how to do it, because I cant do the repaint in my ActionPerfomed.
Error: non-static method repaint() cannot be referenced from a static context
I hope somebody can help. :)
You need to make the following call in your actionPerformed:
window.repaint();
To be able to reference window from within you actionPerformed, you need to make your window variable final:
final JDraw window = ...;
However, if I can suggest a few improvements:
Don't extend JFrame
Don't override paint(Graphics) of JFrame
Instead create a class that extends JComponent or JPanel and set it as the content pane of the JFrame
Instead of overrding paint(Graphics), rather override paintComponent(Graphics)
Your okButton should not be static. Instead move all your code in a non-static method like initUI() and have a code like new JDraw().initUI();
Wrap the code starting your UI in a SwingUtilities.invokeLater(Runnable) so that your UI is properly initiated from the Event Dispatching Thread.
You can't refer to the class JDraw. You should use an object instead. The object in your case is window. So, use:
window.repaint();
It is like saying: Human, walk to the door. Human is the class. You can't tell Human to do something, you need an instance of Human, like Obama or Santa Claus. In your case: you can't tell JDraw to repaint, but the object of type JDraw, i.e.: window.
I'm being told by Eclipse to change the modifier of my string variable to static. I don't understand why. I think I'm declaring everything right but I'm not sure.
Here's my code. The problem is happening on both lines 12 and 13.
import java.awt.*;
import javax.swing.*;
public class MainClass {
Rectangle dot1 = new Rectangle(1,1), dot2 = new Rectangle(1,1);
JFrame frame = new JFrame("Pythagorean Theorem");
public static void main (String[] args){
frame.setVisible(true);
frame.setSize(500, 500);
}
}
frame is an instance variable of MainClass which means you need an instance of MainClass in order to access it. A static variable belongs to the class and does not require an instance. Generally speaking, you should be avoiding storing things statically as they are hard to modify and test.
Rather create an instance of MainClass in your main method and then access your frame inside an instance method.
public class MainClass {
Rectangle dot1 = new Rectangle(1,1), dot2 = new Rectangle(1,1);
JFrame frame = new JFrame("Pythagorean Theorem");
public void buildUI() {
frame.setVisible(true);
frame.setSize(500, 500);
}
public static void main (String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new MainClass().buildUI();
}
});
}
}
EDIT Notice that when working with Swing, when you create/touch UI components, you need to do that on the Event Dispatch Thread (EDT) which is what the invokeLater does.
You are defining frame as an instance variable, but using it as a static variable. There are two solutions to this:
1) You can change the modifier of frame to static
2) Create an instance of your class, like this:
public static void main (String[] args){
MainClass mc = new MainClass();
mc.frame.setVisible(true);
mc.frame.setSize(500, 500);
}