Solved

adding names/text to a shape drawn in java

Posted on 2004-03-23
10
265 Views
Last Modified: 2010-03-31
hi,
here's what i need help with. i have one table in my database and what i want to happen is that the name of that database should appear on the shape( preferrable rectangle) that i have drawn.
i have the code for getting the name for the database shown below.

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
 conn=DriverManager.getConnection("jdbc:odbc:myDB","userid","password");
DatabaseMetaData dbmd = conn.getMetaData();
ResultSet rs = dbmd.getTables(null,null,null,new String[]{"TABLE"});
System.out.println("the tables in the database are:");

while(rs.next())
{
    String tables = rs.getString("TABLE_NAME");
    System.out.println(tables);
}                              
conn.close();


i know the code for drawing a rectangle goes something like this:

public void paint(Graphics g)
{
  g.drawLine(55,75,225,75);

  g.drawRect(30,50,50,50);
  g.setColor(Color.green);
  g.fillRect(31,51,49,49);
   g.setColor(Color.red);
  g.drawString("A", 55, 75);
              
}

so, instead of that "A" appearing, i want the name of the table to appear. any assistance would be greatly appreciated
0
Comment
Question by:msberth
  • 5
  • 3
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10658861
You just need to drawString(tableName,......


instead
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10658891
Make 'tables' an instance variable:

while(rs.next())
{
      tables = rs.getString("TABLE_NAME");
         System.out.println(tables);
}                        



..........

g.drawString(tables, 55, 75);


0
 

Author Comment

by:msberth
ID: 10660483
ok. what if more than one table in the database exist. say four? and i want the rectangles to be displayed at different(random location) locations.  for example, if there are four tables, then there should be four rectangles. i dont want to be the one to specify the number of rectangles to be drawn.
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 86

Expert Comment

by:CEHJ
ID: 10660578
Then you'd need to iterate them.

List tableList = new ArrayList();

.......


while(rs.next())
{
     String table = rs.getString("TABLE_NAME");
     tableList.add(table);
}                        

.........

// (in paint or paintComponent)

final int WIDTH = getWidth();
final int HEIGHT = getHeight();
Iterator iter = tableList.iterator();
while (iter.hasNext()) {
     drawString(iter.next().toString(), (int)(Math.random() * WIDTH), (int)(Math.random() * HEIGHT));



0
 

Author Comment

by:msberth
ID: 10661955
thanx
one more thing before i close the question. how about adding actionlistener/mouselistener to a rectangle clicked. say for example i click on one of the displayed rectangles, then a message like "the rectangle named ,"name on rectangle", "has been clicked.
i hope you can help.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10661983
0
 

Author Comment

by:msberth
ID: 10674758
that doesnt help much. can you be a bit clearer
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 30 total points
ID: 10682273
>>
say for example i click on one of the displayed rectangles, then a message like "the rectangle named ,"name on rectangle", "has been clicked.
>>
// 'rects' is an array of Rectangle
for(int i = 0;i < rects.length;i++) {
      boolean wasHit = rects[i].contains(mouseEvent.getPoint());
      if (wasHit) {
            System.out.println("Rect " + i + " was clicked");
      }
 
}


0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Spring Framework HTTPSession management 1 45
Should localization be done inside spring controller 5 32
by zero exception 10 52
going to wrong jsp page 2 33
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question