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?
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);
}
}
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);
}
}
}