Solved

adding names/text to a shape drawn in java

Posted on 2004-03-23
10
262 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now