Skip to main content

How to run PApplet

No replies
SwarupaK
Offline
Joined: 2013-02-25

Hi All,

Here is the code snippet for PApplet but when I am trying to run from ADF page..
It is keep on trying to realod the page.

=====

package wordcram;

import controlP5.ControlP5;
import controlP5.Tooltip;

import java.awt.BorderLayout;
import java.awt.Color;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

import javax.swing.UIManager;

import processing.core.PApplet;
import processing.core.PVector;

public class WordCramApplet extends PApplet implements MouseListener,MouseMotionListener,ActionListener {
// An array of stripes
//Stripe[] stripes = new Stripe[50];
WordCram words;
ControlP5 cp;
//MouseEvent mouseEvent;
JLabel label;
JPanel toolTip;

public WordCramApplet() {
label = new JLabel();
toolTip = new JPanel();

//toolTip.setBackground(Color.white);

label.setHorizontalAlignment(JLabel.LEFT);
label.setOpaque(true);
label.setBackground(UIManager.getColor("ToolTip.background"));
label.setBorder(UIManager.getBorder("ToolTip.border"));

toolTip.add(label);
toolTip.setVisible(false);
add(toolTip);
toolTip.setOpaque(true);
}

public void setup() {
size(600,600);
//this.add("toolTipPanel", new Imagepanel());
words = new WordCram(this);

cp = new ControlP5(this);

}

public void draw() {
background(200);
Word[] w = new Word[15];
w[0] = new Word("Hello",5);
w[1] = new Word("World",2);
w[2] = new Word("Timely",3);
w[3] = new Word("Incomplete",5);
w[4] = new Word("Good",8);
w[5] = new Word("Excellent",10);
w[6] = new Word("Bad review",2);
w[7] = new Word("Average",5);
w[8] = new Word("Very bad",1);
w[9] = new Word("Optimal",7);
w[10] = new Word("Nice work",9);
w[11] = new Word("Very low",3);
w[12] = new Word("Outstanding",11);
w[13] = new Word("Highly rated",9);
w[14] = new Word("Imperfect",4);

/* for (int i = 0; i < w.length; i++) {
w[i] = new Word(new String("Test"+i),i);
}*/
Color c = new Color(139,0,0);
System.out.println(" c.getBlue() : "+c.getBlue());
for ( int i=0; i < w.length; i++) {
if(w[i].weight>=5.0)
w[i].setColor(c.BLUE.getRGB());
if(w[i].weight<5.0)
w[i].setColor(c.RED.getRGB());
if(w[i].weight>10.0)
w[i].setColor(c.GREEN.getRGB());
}
this.words.fromWords(w);
//this.words.fromWebPage("http://en.wikipedia.org/wiki/Word_cloud");

this.words.drawAll();

PVector position = w[2].getRenderedPlace();

for(int i=0;i System.out.println("Pvector x : "+position.x+ " y : "+position.y+" : "+w[i]);

//
// cp.begin((int)position.x, (int)position.y);

cp.addTooltip();
cp.getTooltip().register("s2","Changes the Background");

// cp.getTooltip().setPositionOffset(position.x, position.y);
noLoop();
System.out.println(" this.height : "+this.height);
// Move and display all "stripes"
// for (int i = 0; i < stripes.length; i++) {
// // stripes[i].move();
// // stripes[i].display();
// }
}

public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2 = (Graphics2D)g;
//this.setDoubleBuffered(true);
System.out.println("In paint()");
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(getBackground());
//g2.fillRect(0, 0, getWidth(), getHeight());
g2.setColor(getForeground());
// setBackground(Color.WHITE);

//g2.setColor(Color.BLUE);
//g2.fill(rect);
}

public static void main(String args[]) {
// PApplet.main(new String[] { "--present", "wordcram.WordCramApplet" });
JFrame f1 = new JFrame();
f1.setBounds(100, 100, 600, 600);
WordCramApplet p1 = new WordCramApplet();
p1.init();
f1.add(p1);
f1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f1.setVisible(true);
}

/// Mouse action events

public void mousePressed(MouseEvent e) {
saySomething("Mouse pressed; # of clicks: "
+ e.getClickCount(), e);
}

public void mouseReleased(MouseEvent e) {
saySomething("Mouse released; # of clicks: "
+ e.getClickCount(), e);
}

public void mouseEntered(MouseEvent e) {
saySomething("Mouse entered", e);
// mouseEvent = e;
System.out.println(e.getLocationOnScreen().getX()+" y : "+e.getLocationOnScreen().getY());

}

public void mouseExited(MouseEvent e) {
saySomething("Mouse exited", e);
//hideToolTip();
}

public void mouseClicked(MouseEvent e) {
saySomething("Mouse clicked (# of clicks: "
+ e.getClickCount() + ")", e);
}

void saySomething(String eventDescription, MouseEvent e) {
System.out.println(eventDescription);
}

public void mouseMoved(MouseEvent e)
{
boolean traversing = false;
Point p = e.getPoint();
System.out.println(" In mouse moved event.... ");
// if(rect.contains(p))
Word word=null;
if(words!=null)
word = words.getWordAt(e.getX(),e.getY());
System.out.println(" Word at : "+word);
if(word!=null)
{
SwingUtilities.convertPointToScreen(p, this);
showToolTip(e.getX(),e.getY(),word.toString());
//showToolTip(10,100,"Sai");
System.out.println(" before text call ..");

traversing = true;
repaint();
}

// if(!traversing && isToolTipShowing())
// hideToolTip();
}

public void showToolTip(int x,int y, String text)
{
label.setText(text);
toolTip.setLocation(x,y-30);//p.x,p.y-80);
toolTip.setVisible(true);
System.out.println(" x : "+x+" y : "+y+" : "+text);
}
public boolean isToolTipShowing()
{
return true;
}
public void hideToolTip()
{
System.out.println("Hiding tooltip");
toolTip.setVisible(false);
}

public void actionPerformed(ActionEvent e) {
}

public void mouseDragged(MouseEvent e) {
}

}
====