Omnimaga: The Coders Of Tomorrow
Welcome, Guest. Please login or register.
 
Omnimaga: The Coders Of Tomorrow
22 May, 2013, 20:01:20 *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   home   news downloads projects tutorials misc forums rules new posts irc about Login Register  
+-OmnomIRC

You must Register, be logged in and have at least 40 posts to use this shout-box! If it still doesn't show up afterward, it might be that OmnomIRC is disabled for your group or under maintenance.

Note: You can also use an IRC client like mIRC, X-Chat or Mibbit to connect to an EFnet server and #omnimaga.

Pages: [1]   Go Down
  Print  
Author Topic: [Java] Adding graphics to frame -  (Read 1333 times) Bookmark and Share
0 Members and 1 Guest are viewing this topic.
Munchor
LV13 Extreme Addict (Next: 9001)
*************
Offline Offline

Gender: Male
Last Login: Yesterday at 17:58:01
Date Registered: 16 October, 2010, 15:39:13
Location: Position
Posts: 6209


Topic starter
Total Post Ratings: +174

View Profile
« on: 18 August, 2011, 18:25:15 »
0

I was just trying to add Graphics2D to a Frame this way:

Game.java

1
2
3
4
5
6
7
8
9
10
11
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:


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


1
2
3
4
5
6
7
8
9
10
11
import java.awt.Graphics2D;


public abstract class MainGraphics extends Graphics2D {
public MainGraphics()
{
this.create();
this.drawLine(5, 5, 10, 10);
}
}
« Last Edit: 18 August, 2011, 18:25:29 by ephan » Logged
nemo
LV9 Veteran (Next: 1337)
*********
Offline Offline

Last Login: 04 April, 2013, 01:12:57
Date Registered: 16 May, 2010, 03:55:30
Posts: 1198

Total Post Ratings: +83

View Profile
« Reply #1 on: 18 August, 2011, 18:42:23 »
0

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:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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();
    }
}
Logged


Munchor
LV13 Extreme Addict (Next: 9001)
*************
Offline Offline

Gender: Male
Last Login: Yesterday at 17:58:01
Date Registered: 16 October, 2010, 15:39:13
Location: Position
Posts: 6209


Topic starter
Total Post Ratings: +174

View Profile
« Reply #2 on: 18 August, 2011, 18:45:32 »
0

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!
Logged
nemo
LV9 Veteran (Next: 1337)
*********
Offline Offline

Last Login: 04 April, 2013, 01:12:57
Date Registered: 16 May, 2010, 03:55:30
Posts: 1198

Total Post Ratings: +83

View Profile
« Reply #3 on: 18 August, 2011, 18:54:38 »
+1

Graphics is also an abstract class, so that wouldn't work either. you can still split it into multiple files:


1
2
3
4
5
6
public class Runner{
    public static void main(String[] args){
        new Game().run();
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
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.
Logged


Munchor
LV13 Extreme Addict (Next: 9001)
*************
Offline Offline

Gender: Male
Last Login: Yesterday at 17:58:01
Date Registered: 16 October, 2010, 15:39:13
Location: Position
Posts: 6209


Topic starter
Total Post Ratings: +174

View Profile
« Reply #4 on: 18 August, 2011, 19:03:36 »
0

Thanks nemo! I have this now:

Runner.java

1
2
3
4
5
6
7
8
public class Runner
{
    public static void main(String[] args)
    {
        new Game().run();
    }
}

Game.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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!
Logged
nemo
LV9 Veteran (Next: 1337)
*********
Offline Offline

Last Login: 04 April, 2013, 01:12:57
Date Registered: 16 May, 2010, 03:55:30
Posts: 1198

Total Post Ratings: +83

View Profile
« Reply #5 on: 18 August, 2011, 20:17:46 »
+1

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.
Logged


Munchor
LV13 Extreme Addict (Next: 9001)
*************
Offline Offline

Gender: Male
Last Login: Yesterday at 17:58:01
Date Registered: 16 October, 2010, 15:39:13
Location: Position
Posts: 6209


Topic starter
Total Post Ratings: +174

View Profile
« Reply #6 on: 18 August, 2011, 20:21:39 »
0

Game.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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

1
2
3
4
5
6
7
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!
Logged
nemo
LV9 Veteran (Next: 1337)
*********
Offline Offline

Last Login: 04 April, 2013, 01:12:57
Date Registered: 16 May, 2010, 03:55:30
Posts: 1198

Total Post Ratings: +83

View Profile
« Reply #7 on: 18 August, 2011, 20:23:55 »
+1

yes, you aren't adding the game panel to the JFrame.
Logged


Munchor
LV13 Extreme Addict (Next: 9001)
*************
Offline Offline

Gender: Male
Last Login: Yesterday at 17:58:01
Date Registered: 16 October, 2010, 15:39:13
Location: Position
Posts: 6209


Topic starter
Total Post Ratings: +174

View Profile
« Reply #8 on: 18 August, 2011, 20:27:07 »
0

Hm, I tried adding this in public Game():


1
mainFrame.getContentPane().add(this);

So that I had the panel to the frame, but I still don't see the line.

Thanks a lot!
Logged
nemo
LV9 Veteran (Next: 1337)
*********
Offline Offline

Last Login: 04 April, 2013, 01:12:57
Date Registered: 16 May, 2010, 03:55:30
Posts: 1198

Total Post Ratings: +83

View Profile
« Reply #9 on: 18 August, 2011, 20:37:34 »
0

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?
Logged


Munchor
LV13 Extreme Addict (Next: 9001)
*************
Offline Offline

Gender: Male
Last Login: Yesterday at 17:58:01
Date Registered: 16 October, 2010, 15:39:13
Location: Position
Posts: 6209


Topic starter
Total Post Ratings: +174

View Profile
« Reply #10 on: 18 August, 2011, 20:39:50 »
0

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.
Logged
Munchor
LV13 Extreme Addict (Next: 9001)
*************
Offline Offline

Gender: Male
Last Login: Yesterday at 17:58:01
Date Registered: 16 October, 2010, 15:39:13
Location: Position
Posts: 6209


Topic starter
Total Post Ratings: +174

View Profile
« Reply #11 on: 19 August, 2011, 16:07:36 »
0

Game.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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.
Logged
nemo
LV9 Veteran (Next: 1337)
*********
Offline Offline

Last Login: 04 April, 2013, 01:12:57
Date Registered: 16 May, 2010, 03:55:30
Posts: 1198

Total Post Ratings: +83

View Profile
« Reply #12 on: 19 August, 2011, 21:54:06 »
0

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.
Logged


Munchor
LV13 Extreme Addict (Next: 9001)
*************
Offline Offline

Gender: Male
Last Login: Yesterday at 17:58:01
Date Registered: 16 October, 2010, 15:39:13
Location: Position
Posts: 6209


Topic starter
Total Post Ratings: +174

View Profile
« Reply #13 on: 19 August, 2011, 21:57:14 »
0

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 Wink
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by EzPortal
Powered by MySQL Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Powered by PHP
Page created in 0.183 seconds with 32 queries.
Skin by DJ Omnimaga edited from SMF default theme with the help of tr1p1ea.
All programs, games and songs avaliable on this website are property of their respective owners.
Best viewed in Opera, Firefox, Chrome and Safari with a resolution of 1024x768 or above.