Solved

adding names/text to a shape drawn in java

Posted on 2004-03-23
10
264 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java - Why doesn't this JFrame work 3 59
Java Loop 6 62
How to convert String matching to regex in java 4 33
tomcat startup error 5 65
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

810 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