Solved

Can Someone help me make this java run on html...

Posted on 2001-06-18
8
201 Views
Last Modified: 2010-04-16
import java.awt.*;
import javax.swing.*;

class TestFrame extends JFrame
{
  public TestFrame() {
    super( "Graphics demo" );
    getContentPane().add(new JCanvas());
  }

  public static void main( String args[] ) {
    TestFrame mainFrame = new TestFrame();
    mainFrame.pack();
    mainFrame.setVisible( true );
  }
}

class JCanvas extends JComponent
{
  private static Color m_tRed = new Color(255,0,0,150);
  private static Color m_tGreen = new Color(0,255,0,150);
  private static Color m_tBlue = new Color(0,0,255,150);

  private static Font m_biFont = new Font("Monospaced", Font.BOLD | Font.ITALIC, 36);
  private static Font m_pFont = new Font("SanSerif", Font.PLAIN, 12);
  private static Font m_bFont = new Font("Serif", Font.BOLD, 24);
 
  private static ImageIcon m_flight = new ImageIcon("flight.gif");

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
 
    // counter
    int c = 0;

    // for use below
    int w = 0;
    int h = 0;
    int d = 0;

    // get damaged region
    Rectangle r = g.getClipBounds();
    int clipx = r.x;
    int clipy = r.y;
    int clipw = r.width;
    int cliph = r.height;

    // fill only damaged region only
    g.setColor(Color.white);
    g.fillRect(clipx,clipy,clipw,cliph);

    // filled yellow circle if bounding region has been damaged
    if (clipx <= 240 && clipy <= 240) {
      g.setColor(Color.yellow);
      g.fillOval(0,0,240,240); c++;
    }
       
    // filled magenta circle if bounding region has been damaged
    if (clipx + clipw >= 160 && clipx <= 400
        && clipy + cliph >= 160 && clipy <= 400) {
      g.setColor(Color.magenta);
      g.fillOval(160,160,240,240); c++;
    }

    w = m_flight.getIconWidth();
    h = m_flight.getIconHeight();
    // paint the icon below blue sqaure if bounding region has been damaged
    if (clipx + clipw >= 280-(w/2) && clipx <= (280+(w/2))
        && clipy + cliph >= 120-(h/2) && clipy <= (120+(h/2))) {
      m_flight.paintIcon(this,g,280-(w/2),120-(h/2)); c++;
    }

    // paint the icon below red sqaure if bounding region has been damaged
    if (clipx + clipw >= 120-(w/2) && clipx <= (120+(w/2))
        && clipy + cliph >= 280-(h/2) && clipy <= (280+(h/2))) {
      m_flight.paintIcon(this,g,120-(w/2),280-(h/2)); c++;
    }

    // filled transparent red square if bounding region has been damaged
    if (clipx + clipw >= 60 && clipx <= 180
        && clipy + cliph >= 220 && clipy <= 340) {
      g.setColor(m_tRed);
      g.fillRect(60,220,120,120); c++;
    }

    // filled transparent green circle if bounding region has been damaged
    if (clipx + clipw > 140 && clipx < 260
        && clipy + cliph > 140 && clipy < 260) {
      g.setColor(m_tGreen);
      g.fillOval(140,140,120,120); c++;
    }

    // filled transparent blue square if bounding region has been damaged
    if (clipx + clipw > 220 && clipx < 380
        && clipy + cliph > 60 && clipy < 180) {
      g.setColor(m_tBlue);
      g.fillRect(220,60,120,120); c++;
    }

    g.setColor(Color.black);

    g.setFont(m_biFont);
    FontMetrics fm = g.getFontMetrics();
    w = fm.stringWidth("Swing");
    h = fm.getAscent();
    d = fm.getDescent();
    // Bold, Italic, 36-point "Swing" if bounding region has been damaged
    if (clipx + clipw > 120-(w/2) && clipx < (120+(w/2))
        && clipy + cliph > (120+(h/4))-h && clipy < (120+(h/4))+d) {
      g.drawString("Swing",120-(w/2),120+(h/4)); c++;
    }

    g.setFont(m_pFont);
    fm = g.getFontMetrics();
    w = fm.stringWidth("is");
    h = fm.getAscent();
    d = fm.getDescent();
    // Plain, 12-point "is" if bounding region has been damaged
    if (clipx + clipw > 200-(w/2) && clipx < (200+(w/2))
        && clipy + cliph > (200+(h/4))-h && clipy < (200+(h/4))+d) {
      g.drawString("is",200-(w/2),200+(h/4)); c++;
    }

    g.setFont(m_bFont);
    fm = g.getFontMetrics();
    w = fm.stringWidth("powerful!!");
    h = fm.getAscent();
    d = fm.getDescent();
    // Bold 24-point "powerful!!" if bounding region has been damaged
    if (clipx + clipw > 280-(w/2) && clipx < (280+(w/2))
        && clipy + cliph > (280+(h/4))-h && clipy < (280+(h/4))+d) {
      g.drawString("powerful!!",280-(w/2),280+(h/4)); c++;
    }

    System.out.println("# items repainted = " + c + "/10");
  }
 
  // Some layout managers need this information
  public Dimension getPreferredSize() {
    return new Dimension(400,400);
  }

  // Some layout managers need this information
  public Dimension getMinimumSize() {
    return getPreferredSize();
  }
}
0
Comment
Question by:foxx3001
  • 5
  • 2
8 Comments
 
LVL 3

Expert Comment

by:shyamkumarreddy
ID: 6205087
what is the probs fox


shyam
0
 

Author Comment

by:foxx3001
ID: 6205189
i wanna make it run in the html mode....i mean in the browser......
how can i altered it to make available in the html file...
0
 
LVL 3

Accepted Solution

by:
rjackman earned 150 total points
ID: 6205461
hi
i had alterd ur file a bit
have extended ur class from JApplet instead of JFrame
and added init()
also try this code in appletviewer
if u want this code to run in browser then u have to get the plugin and html converter
then pass the html to converter which will convert the html to such a format to be displayed on to browser using plugin
u will get the plugin from the sun site
http://java.sun.com/products/plugin/

import java.awt.*;
import javax.swing.*;

public class TestFrame extends JApplet
{
 public void init() {
   //super( "Graphics demo" );
   this.getContentPane().add(new JCanvas());
 }

 public static void main( String args[] ) {
   TestFrame mainFrame = new TestFrame();
   //mainFrame.pack();
   mainFrame.setVisible( true );
 }
}

class JCanvas extends JComponent
{
 private static Color m_tRed = new Color(255,0,0,150);
 private static Color m_tGreen = new Color(0,255,0,150);
 private static Color m_tBlue = new Color(0,0,255,150);

 private static Font m_biFont = new Font("Monospaced", Font.BOLD | Font.ITALIC, 36);
 private static Font m_pFont = new Font("SanSerif", Font.PLAIN, 12);
 private static Font m_bFont = new Font("Serif", Font.BOLD, 24);
 
 private static ImageIcon m_flight = new ImageIcon("flight.gif");

 public void paintComponent(Graphics g) {
   super.paintComponent(g);
 
   // counter
   int c = 0;

   // for use below
   int w = 0;
   int h = 0;
   int d = 0;

   // get damaged region
   Rectangle r = g.getClipBounds();
   int clipx = r.x;
   int clipy = r.y;
   int clipw = r.width;
   int cliph = r.height;

   // fill only damaged region only
   g.setColor(Color.white);
   g.fillRect(clipx,clipy,clipw,cliph);

   // filled yellow circle if bounding region has been damaged
   if (clipx <= 240 && clipy <= 240) {
     g.setColor(Color.yellow);
     g.fillOval(0,0,240,240); c++;
   }
       
   // filled magenta circle if bounding region has been damaged
   if (clipx + clipw >= 160 && clipx <= 400
       && clipy + cliph >= 160 && clipy <= 400) {
     g.setColor(Color.magenta);
     g.fillOval(160,160,240,240); c++;
   }

   w = m_flight.getIconWidth();
   h = m_flight.getIconHeight();
   // paint the icon below blue sqaure if bounding region has been damaged
   if (clipx + clipw >= 280-(w/2) && clipx <= (280+(w/2))
       && clipy + cliph >= 120-(h/2) && clipy <= (120+(h/2))) {
     m_flight.paintIcon(this,g,280-(w/2),120-(h/2)); c++;
   }

   // paint the icon below red sqaure if bounding region has been damaged
   if (clipx + clipw >= 120-(w/2) && clipx <= (120+(w/2))
       && clipy + cliph >= 280-(h/2) && clipy <= (280+(h/2))) {
     m_flight.paintIcon(this,g,120-(w/2),280-(h/2)); c++;
   }

   // filled transparent red square if bounding region has been damaged
   if (clipx + clipw >= 60 && clipx <= 180
       && clipy + cliph >= 220 && clipy <= 340) {
     g.setColor(m_tRed);
     g.fillRect(60,220,120,120); c++;
   }

   // filled transparent green circle if bounding region has been damaged
   if (clipx + clipw > 140 && clipx < 260
       && clipy + cliph > 140 && clipy < 260) {
     g.setColor(m_tGreen);
     g.fillOval(140,140,120,120); c++;
   }

   // filled transparent blue square if bounding region has been damaged
   if (clipx + clipw > 220 && clipx < 380
       && clipy + cliph > 60 && clipy < 180) {
     g.setColor(m_tBlue);
     g.fillRect(220,60,120,120); c++;
   }

   g.setColor(Color.black);

   g.setFont(m_biFont);
   FontMetrics fm = g.getFontMetrics();
   w = fm.stringWidth("Swing");
   h = fm.getAscent();
   d = fm.getDescent();
   // Bold, Italic, 36-point "Swing" if bounding region has been damaged
   if (clipx + clipw > 120-(w/2) && clipx < (120+(w/2))
       && clipy + cliph > (120+(h/4))-h && clipy < (120+(h/4))+d) {
     g.drawString("Swing",120-(w/2),120+(h/4)); c++;
   }

   g.setFont(m_pFont);
   fm = g.getFontMetrics();
   w = fm.stringWidth("is");
   h = fm.getAscent();
   d = fm.getDescent();
   // Plain, 12-point "is" if bounding region has been damaged
   if (clipx + clipw > 200-(w/2) && clipx < (200+(w/2))
       && clipy + cliph > (200+(h/4))-h && clipy < (200+(h/4))+d) {
     g.drawString("is",200-(w/2),200+(h/4)); c++;
   }

   g.setFont(m_bFont);
   fm = g.getFontMetrics();
   w = fm.stringWidth("powerful!!");
   h = fm.getAscent();
   d = fm.getDescent();
   // Bold 24-point "powerful!!" if bounding region has been damaged
   if (clipx + clipw > 280-(w/2) && clipx < (280+(w/2))
       && clipy + cliph > (280+(h/4))-h && clipy < (280+(h/4))+d) {
     g.drawString("powerful!!",280-(w/2),280+(h/4)); c++;
   }

   System.out.println("# items repainted = " + c + "/10");
 }
 
 // Some layout managers need this information
 public Dimension getPreferredSize() {
   return new Dimension(400,400);
 }

 // Some layout managers need this information
 public Dimension getMinimumSize() {
   return getPreferredSize();
 }
}
*******************
//the html

<html>
<applet code ="TestReader.class" width=500 height=500>
</applet>
</html>
0
 

Author Comment

by:foxx3001
ID: 6205651
Hi....Thanks rjackman.....
Can i get your email......
i need a your advise in java and i wanna mail u a java fail.....i hope you can give me a solution....

thank for your concern
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:foxx3001
ID: 6205655
can i have your email......
need to learn more with you
0
 
LVL 3

Expert Comment

by:rjackman
ID: 6205715
hi
my emailid is
rickyjackman@usa.net
cheers
RJ
0
 

Author Comment

by:foxx3001
ID: 6237296
hai....mana datangnyer markah yang banyak2 tue....main tipu yer...!!!!!
0
 

Author Comment

by:foxx3001
ID: 6237306
excuse me...english pleaseee......
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to parse custom JSON to POJO java 4 55
for i loop in grovy 1 45
Java Message handling in Service Layer 3 57
servlet example issue 6 31
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.

911 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

27 Experts available now in Live!

Get 1:1 Help Now