Omnimaga
General Discussion => Technology and Development => Computer Programming => Topic started by: Munchor on August 18, 2011, 12:25:15 pm
-
I was just trying to add Graphics2D to a Frame this way:
Game.java
import java.awt.Component;
public class Game
{
public static void main(String[] args)
{
MainFrame mainFrame = new MainFrame();
MainGraphics mainGraphics = new MainGraphics();
mainFrame.getContentPane().add(mainGraphics);
}
}
That file contains the main function, where I create a new frame and a new graphics (both classes made in separate files), and then I try to add the graphics to the frame.
The problem is, though, I get:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Cannot instantiate the type MainGraphics
The method add(Component) in the type Container is not applicable for the arguments (MainGraphics)
I can only add components to frames and content panes.
So I tried to make mainGraphics a component, like this:
Component mainGraphics = new MainGraphics();
However, I get (in this line):
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Type mismatch: cannot convert from MainGraphics to Component
Cannot instantiate the type MainGraphics
Does anybody know how to solve this? Thanks! Also, MainGraphics is an abstract class, here's its code:
import java.awt.Graphics2D;
public abstract class MainGraphics extends Graphics2D {
public MainGraphics()
{
this.create();
this.drawLine(5, 5, 10, 10);
}
}
-
for one, as you said, MainGraphics is an abstract class so you can't make an object from it. secondly, you can't add graphics to a frame because the class Graphics2D is not a type of Component. here's a quick graphics example to get what you want to do:
public class Test extends JPanel{
public Test(){
JFrame frame = new JFrame("test");
frame.getContentPane().add(this);
frame.setSize(300, 300);
frame.setVisible(true);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawLine(5, 5, 10, 10);
}
public static void main(String[] args){
new Test();
}
}
-
Nemo, what if I use Graphics instead of Graphics2D? The example you gave me is good, but the problem is I need my code organized in separate files, I am expecting this to be a rather large project.
Thanks for the quick response too!
-
Graphics is also an abstract class, so that wouldn't work either. you can still split it into multiple files:
public class Runner{
public static void main(String[] args){
new Game().run();
}
}
public class Game extends JPanel{
public Game(){
//set up JFrame
}
public void run(){
//show JFrame
}
public void paintComponent(Graphics g){
//paint things here
}
}
what are you making, by the way? if you're worried the paintComponent method will get too large because you have too many objects to draw e.g. you have an array of 100 enemies, your character, the background, the tilemap etc. just make an Enemy, Character, Tilemap class and have them each have a draw(Graphics) method.
-
Thanks nemo! I have this now:
Runner.java
public class Runner
{
public static void main(String[] args)
{
new Game().run();
}
}
Game.java
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Game extends JPanel
{
JFrame mainFrame = new JFrame("Game");
public Game()
{
mainFrame.setSize(800, 600);
}
public void run()
{
mainFrame.setVisible(true);
}
public void paintComponent(Graphics2D g)
{
super.paintComponent(g);
g.drawLine(5, 5, 10, 10);
}
}
Right now, my only question is adding the Graphics to the Game Panel. Thanks!
-
the Game class has a Graphics instance variable inherited from JPanel. you don't need to add it. you can access the graphics variable using paintComponent.
-
Game.java
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Game extends JPanel
{
JFrame mainFrame = new JFrame("Game");
public Game()
{
mainFrame.setSize(800, 600);
}
public void run()
{
mainFrame.setVisible(true);
}
public void paintComponent(Graphics2D g)
{
super.paintComponent(g);
g.drawLine(5, 5, 10, 10);
}
}
Runner.java
public class Runner
{
public static void main(String[] args)
{
new Game().run();
}
}
Well, the line I'm drawing is not appearing, do you know what's wrong? Thanks!
-
yes, you aren't adding the game panel to the JFrame.
-
Hm, I tried adding this in public Game():
mainFrame.getContentPane().add(this);
So that I had the panel to the frame, but I still don't see the line.
Thanks a lot!
-
the exact some code is working on my computer. are you sure you saved/recompiled before you ran the code? also, what's the project you're working on?
-
the exact some code is working on my computer. are you sure you saved/recompiled before you ran the code? also, what's the project you're working on?
I just created those two files and then used "javac" to compile Game.java and then Runner.java, then used "java Runner" to run the program.
@Nemo: It's no project yet, it's more like testing.
-
Game.java
import java.awt.Graphics2D;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Game extends JPanel
{
JFrame mainFrame = new JFrame("Game");
public Game()
{
mainFrame.setSize(800, 600);
JButton myButton = new JButton("hello");
mainFrame.getContentPane().add(this);
this.add(myButton);
}
public void run()
{
mainFrame.setVisible(true);
}
public void paintComponent(Graphics2D g)
{
super.paintComponent(g);
g.fillArc(5, 5, 100, 100, 45, 45);
g.drawLine(5, 5, 10, 10);
}
}
I managed to add a button to the panel and it appears, so perhaps I'm not adding the Graphics to the panel. How would I do that? Thanks, once again.
-
Again, you cannot add Graphics to the panel. JPanels have a Graphics instance variable already. i just noticed your method header for paintComponent is incorrect. it's parameter should be of type Graphics, not Graphics2D.
-
Again, you cannot add Graphics to the panel. JPanels have a Graphics instance variable already. i just noticed your method header for paintComponent is incorrect. it's parameter should be of type Graphics, not Graphics2D.
I understand nemo, it works now, thanks a lot!
I know, though, that I can turn Graphics to Graphics2D and I know how ;)