-
Using a class within a class to draw
Here, I have class "Panel," if clicked, leads to a link. I was wondering how it could be manipulated into FramePage.java with this statement:
Panel p1 = new Panel(desired text displayed w/ green border);
Panel p2 = new Panel(desired text displayed w/ green border);
...
-----------------------------------------------------
A brief skim should do this,
Here is Panel.java:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class Panel extends Applet implements Runnable, MouseListener, MouseMotionListener
{
private Rectangle rect = null;
private Rectangle highlight;
private Color foreground;
private Color background;
private Color selection_color;
private Font font;
private int current_pos_x;
private int current_pos_y;
private int fontsize;
private int current_width;
private int current_height;
private int final_pos_x;
private int final_pos_y;
private int final_width;
private int final_height;
private int unit_measure_x;
private int unit_measure_y;
private int start_pos_x;
private int start_pos_y;
private int stringX;
private int stringY;
private int stringHeight;
private int stringTop;
private int pause;
private boolean hover;
private boolean hover_2;
private boolean clicked;
private boolean animated;
private boolean expanding_width;
private boolean expanding_height;
private boolean running;
private boolean stopped;
private Thread looper;
private String string;
private Image image;
URL url;
// Where should I create "public Panel(String string)" ?
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
foreground = new Color(205, 205, 205);
background = Color.black;
selection_color = Color.green;
current_width = 0;
current_height = 0;
pause = 5;
stopped = true;
hover = false;
hover_2 = false;
clicked = false;
animated = false;
expanding_width = false;
expanding_height = false;
fontsize = 0;
}
public void start()
{
if(!running)
{
running = true;
looper = new Thread(this);
looper.start();
}
}
public void stop()
{
running = false;
}
public void run()
{
try
{
while(running)
{
if(expanding_width)
current_width = current_width + 2;
if(expanding_height)
current_height = current_height + 2;
repaint();
looper.sleep(pause);
}
}
catch(InterruptedException e)
{
running = false;
}
}
public void update(Graphics g)
{
if(running)
{
if(rect == null)
{
rect = getBounds();
final_width = (rect.width - (rect.width / 10));
final_height = (rect.height - (rect.height / 2));
start_pos_x = rect.width / 2;
start_pos_y = rect.height / 2;
image = createImage(rect.width, rect.height);
}
paint(image.getGraphics());
g.drawImage(image, 0, 0, null);
if(hover)
{
if(current_width < final_width)
{
expanding_width = true;
current_pos_x = current_pos_x - 1;
}
if(current_height < final_height){
expanding_height = true;
current_pos_y = current_pos_y - 1;}
if(current_width >= final_width)
expanding_width = false;
if(current_height >= final_height)
expanding_height = false;
}
else
{
current_pos_x = start_pos_x;
current_pos_y = start_pos_y;
current_width = 0;
current_height = 0;
}
}
}
// When requested to paint, in class FramePage.java, would I use p1.paint??
// not likely, then what would it be to create each button?
public void paint(Graphics g)
{
if (rect == null)
return;
g.setColor(background);
g.fillRect(0, 0, rect.width, rect.height);
if(hover)
{
g.setColor(selection_color);
g.fillRect(current_pos_x, current_pos_y, current_width, current_height);
g.setColor(background);
g.fillRect(current_pos_x + 3, current_pos_y + 3, current_width - 6, current_height - 6);
}
fontsize = (int) (rect.width / 10);
font = new Font ("TimesRoman", Font.PLAIN, fontsize);
FontMetrics fm = g.getFontMetrics(font);
stringX = (rect.width - fm.stringWidth(string)) / 2;
stringY = rect.height;
stringY -= fm.getAscent() + fm.getDescent();
stringY /= 2;
stringY += fm.getAscent();
g.setColor(foreground);
g.setFont(font);
g.drawString(string, stringX, stringY);
}
public void mousePressed(MouseEvent event) {}
public void mouseDragged(MouseEvent event) {}
public void mouseReleased(MouseEvent event) {}
public void mouseMoved(MouseEvent event) {}
public void mouseClicked(MouseEvent event)
{
if(hover)
{
// URL should change for different cases, but that is not the problem now.
try { url = new URL("http://www.google.com");}
catch (MalformedURLException e) { System.out.println("Bad URL");}
getAppletContext().showDocument(url);
hover = false;
}
}
public void mouseEntered(MouseEvent event) { hover = true;
repaint();}
public void mouseExited(MouseEvent event)
{hover = false;
current_pos_x = start_pos_x;
current_pos_y = start_pos_y;
repaint();
}
}
Best regards,
Ben
Similar Threads
-
By pentacle1 in forum Java
Replies: 2
Last Post: 01-22-2006, 07:45 AM
-
Replies: 5
Last Post: 01-15-2006, 07:10 PM
-
By none_none in forum Java
Replies: 17
Last Post: 04-28-2005, 03:00 PM
-
Replies: 5
Last Post: 10-17-2002, 01:58 PM
-
By Shailesh C.Rathod in forum .NET
Replies: 2
Last Post: 03-13-2002, 07:53 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks