Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  paintComponent  (Read 1436 times)
0 Members and 1 Guest are viewing this topic.
Offline Frann

Senior Newbie





« Posted 2011-05-21 17:37:07 »

Hi

I'm trying to make a kind of UML design software and I was going to use the paintComponent to draw boxes.

However I cannot include paintComponent in a method or using multiple painComponents or calling methods in paintComponents as it says I haven't declared the Graphics g.

So I thought to use arrays to have multiple elements, but this way everything gets way too complicated, am I using a wrong approach?
Can you help me sort this out?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
package graphicdraw;

import javax.swing.*;

public class Main
{
    public static void main(String[] args)
    {
   JFrame f = new JFrame("UMLIDE");
   f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        draw d = new draw();
   f.add(d);
   f.setSize(800, 250);
   f.setVisible(true);
    }
}


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  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
package graphicdraw;

import java.awt.*;
//import java.awt.event.*;
import javax.swing.*;

public class draw extends JPanel
{

    int i;
    int n = 3;
    int[] x = new int[n];
    int[] y = new int[n];
    int[] w = new int[n];
    int[] l = new int[n];
    String[] className = new String[n];

    public draw()
    {
        i = 0;
        x[i] = 25;
        y[i] = 25;
        l[i] = 100;
        w[i] = 30;
        className[0] = "CLASS 1";
        className[1] = "CLASS 2";
        className[2] = "CLASS 3";

        for(i=1; i<n; i++)
        {
        x[i]=x[i-1]+110;
        y[i] = 25;
        l[i] = 100;
        w[i] = 30;
        }
    }
   
    public draw(int $x, int $y, int $l, int $w)
    {
        i = 0;
        x[i] = $x;
        y[i] = $y;
        l[i] = $l;
        w[i] = $w;
    }

    @Override
    public void paintComponent(Graphics g)
    {
   super.paintComponent(g);
   this.setBackground(Color.WHITE);
       
   g.setColor(Color.BLACK);

        for(i=0; i<n; i++)
        {
   g.drawRect(x[i], y[i], l[i], w[i]); //Class
        g.drawRect(x[i], y[i]+w[i], l[i], w[i]); //Variables
        g.drawRect(x[i], y[i]+w[i]*2, l[i], w[i]); //Methods
        g.drawString(className[i], x[i]+15, y[i]+15);
        g.drawString("VARIABLES", x[i]+15, y[i]+w[i]+15);
        g.drawString("METHODS", x[i]+15, y[i]+w[i]*2+15);
        }
    }  
}


Offline zoto

Senior Devvie


Medals: 4



« Reply #1 - Posted 2011-05-21 18:49:52 »

You don't call paintComponent directly, rather you call repaint() and swing will take care of calling paintComponent.

 If you want to perform drawing in a separate function make sure to call it from within paintComponent and pass the Graphics object to your other function.
Offline Kurten

Junior Devvie


Medals: 3
Exp: 3 years


-w-


« Reply #2 - Posted 2011-05-21 18:56:01 »

I solved that by using paint(Graphics g) instead of paintComponent(Graphics g).

Add the paint to a class that extends JPanel, then add the class to the frame.
Then you can call paint by using repaint(); ^_^
Swing will take care of the Graphics object
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Frann

Senior Newbie





« Reply #3 - Posted 2011-05-21 19:38:38 »

Thanks guys,

what I was doing wrong is not sending (Graphics g) to the method I was calling.

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  
31  
32  
@Override
    public void paintComponent(Graphics g)
    {
   super.paintComponent(g);
   this.setBackground(Color.WHITE);
       
   g.setColor(Color.BLACK);

        drawBox(g);

        for(i=0; i<n-1; i++) // change for the purpose of testing, so paintComponent draws only the first and second box
        {
   g.drawRect(x[i], y[i], l[i], w[i]); //Class
        g.drawRect(x[i], y[i]+w[i], l[i], w[i]); //Variables
        g.drawRect(x[i], y[i]+w[i]*2, l[i], w[i]); //Methods
        g.drawString(className[i], x[i]+15, y[i]+15);
        g.drawString("VARIABLES", x[i]+15, y[i]+w[i]+15);
        g.drawString("METHODS", x[i]+15, y[i]+w[i]*2+15);
        }
    }

    public void drawBox(Graphics g)
    {
        i=2; // this method draws only the third box
        g.drawRect(x[i], y[i], l[i], w[i]); //Class
        g.drawRect(x[i], y[i]+w[i], l[i], w[i]); //Variables
        g.drawRect(x[i], y[i]+w[i]*2, l[i], w[i]); //Methods
        g.drawString(className[i], x[i]+15, y[i]+15);
        g.drawString("VARIABLES", x[i]+15, y[i]+w[i]+15);
        g.drawString("METHODS", x[i]+15, y[i]+w[i]*2+15);
        repaint();  //this is optional if called in this way
    }

Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2011-05-21 23:32:01 »

Slightly off topic, there is no point to extend JPanel as it is only used as a container for other components. JComponent is the most appropriate class to draw into and since JPanel extends JComponent, there should be no change to your code at all.

Offline Frann

Senior Newbie





« Reply #5 - Posted 2011-05-22 17:57:20 »

I still don't know the difference between paint and paintComponent. I also have no idea what JComponent is.
Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2011-05-22 18:04:26 »

javax.swing.JComponent is the Swing version of java.awt.Component. AWT components only have "paint" and that is where everything is drawn. In Swing, "paint" doesn't draw anything, instead it calls 3 methods: "paintComponent", "paintBorder",  and "paintChildren". Instead of overriding "paint", it is recommended to override "paintComponent" if you extend any Swing component (which all start with a "J"). You should read this article about painting in AWT and Swing to get a grasp on how the mechanics work. You should also look at the Java Swing API and familiarize yourself with it.

Offline Frann

Senior Newbie





« Reply #7 - Posted 2011-06-05 18:09:13 »

-
Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #8 - Posted 2011-06-06 02:31:06 »

@Frann
O_o ?

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

trollwarrior1 (27 views)
2014-11-22 12:13:56

xFryIx (69 views)
2014-11-13 12:34:49

digdugdiggy (48 views)
2014-11-12 21:11:50

digdugdiggy (42 views)
2014-11-12 21:10:15

digdugdiggy (36 views)
2014-11-12 21:09:33

kovacsa (60 views)
2014-11-07 19:57:14

TehJavaDev (64 views)
2014-11-03 22:04:50

BurntPizza (62 views)
2014-11-03 18:54:52

moogie (77 views)
2014-11-03 06:22:04

CopyableCougar4 (77 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!