Author Topic: [Java] Adding graphics to frame  (Read 8547 times)

0 Members and 1 Guest are viewing this topic.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
[Java] Adding graphics to frame
« on: August 18, 2011, 12:25:15 pm »
I was just trying to add Graphics2D to a Frame this way:

Game.java
Code: [Select]
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:

Quote
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:

Code: [Select]
Component mainGraphics = new MainGraphics();
However, I get (in this line):

Quote
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:

Code: [Select]
import java.awt.Graphics2D;


public abstract class MainGraphics extends Graphics2D {
public MainGraphics()
{
this.create();
this.drawLine(5, 5, 10, 10);
}
}
« Last Edit: August 18, 2011, 12:25:29 pm by ephan »

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: [Java] Adding graphics to frame
« Reply #1 on: August 18, 2011, 12:42:23 pm »
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:

Code: [Select]
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();
    }
}


Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: [Java] Adding graphics to frame
« Reply #2 on: August 18, 2011, 12:45:32 pm »
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!

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: [Java] Adding graphics to frame
« Reply #3 on: August 18, 2011, 12:54:38 pm »
Graphics is also an abstract class, so that wouldn't work either. you can still split it into multiple files:

Code: ("Runner.java") [Select]
public class Runner{
    public static void main(String[] args){
        new Game().run();
    }
}
Code: ("Game.java") [Select]
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.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: [Java] Adding graphics to frame
« Reply #4 on: August 18, 2011, 01:03:36 pm »
Thanks nemo! I have this now:

Runner.java
Code: [Select]
public class Runner
{
    public static void main(String[] args)
    {
        new Game().run();
    }
}

Game.java
Code: [Select]
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!

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: [Java] Adding graphics to frame
« Reply #5 on: August 18, 2011, 02:17:46 pm »
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.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: [Java] Adding graphics to frame
« Reply #6 on: August 18, 2011, 02:21:39 pm »
Game.java
Code: [Select]
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
Code: [Select]
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!

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: [Java] Adding graphics to frame
« Reply #7 on: August 18, 2011, 02:23:55 pm »
yes, you aren't adding the game panel to the JFrame.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: [Java] Adding graphics to frame
« Reply #8 on: August 18, 2011, 02:27:07 pm »
Hm, I tried adding this in public Game():

Code: [Select]
mainFrame.getContentPane().add(this);
So that I had the panel to the frame, but I still don't see the line.

Thanks a lot!

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: [Java] Adding graphics to frame
« Reply #9 on: August 18, 2011, 02:37:34 pm »
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?


Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: [Java] Adding graphics to frame
« Reply #10 on: August 18, 2011, 02:39:50 pm »
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.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: [Java] Adding graphics to frame
« Reply #11 on: August 19, 2011, 10:07:36 am »
Game.java
Code: [Select]
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.

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: [Java] Adding graphics to frame
« Reply #12 on: August 19, 2011, 03:54:06 pm »
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.


Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: [Java] Adding graphics to frame
« Reply #13 on: August 19, 2011, 03:57:14 pm »
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 ;)