Reputation: 19
So I am quite new to Java programming and programming in general and please excuse my lack of knowledge. I am trying to draw a simple rectangle on the screen with some given coordinates.
Here is my code in the Drawer Class:
public class Drawer extends JPanel {
public static void drawPixel(Graphics g, int x, int y) {
g.fillRect(x, y, 5, 5);
}
}
And here is the code I am trying to use in the main class:
Drawer drawer = new Drawer();
Drawer.drawPixel(Graphics g, i9, i10);
So I really don't understand what the first argument is for. It gives me an error saying it can't resolve "Symbol g". i9 and i10 are the coordinates I want it to draw the rectangle at.
Thanks in advance for any help. :)
Upvotes: 1
Views: 955
Reputation: 299
Some of your code isn't right like Graphics g. I'm not going to go over it since others have but I recommend you look at some tutorials. If your new to java overall then check out:
You should check out that video series, it teaches you a solid understanding of the java syntax and basics. Then after that you can start checking out tutorials like this:
That one teaches you a really good solid understanding of making complicated games in Java. I recommend even intermediate and advanced Java programmer to check it out. When I started I looked at other tutorials and they mainly stopped halfway through, had terrible mistakes or something like that. This video teaches it a whole simpler way.
Upvotes: 0
Reputation:
You can not draw something like that. You should use paintComponent()
method to draw.
Check this:
public class RectDrawer extends JPanel {
private int x;
private int y;
public RectDrawer(int x, int y){
this.x = x;
this.y = y;
}
public void paintComponent(Graphics g) {
g.fillRect(x, y, 5, 5);
}
}
Upvotes: 4
Reputation: 723
Drawer.drawPixel(Graphics g, i9, i10);
is probably the source of your issue.
You are passing (as the first argument) something along the lines of Graphics g
. Consider just passing g
by itself without the Graphics
bit - that should solve your problem.
The Graphics g
object refers to the graphical context - this is what effectively draws stuff onto the screen. By calling methods on it, you can then literally draw things where a user can see them.
Using JPanel as an example, you can override its paintComponent() method. You'll notice this method takes in a Graphics object - you can then use this to draw stuff in the panel.
Upvotes: 1