JApplet is not working?

Hi, I am designing a gui which contains a Jslider, a JCombox and polygons.

but nothing can't be seen. No compile errors.

I guess it's the structure problem.

Any advice, THANKS.

import java.io.*;

import java.util.List;

import java.util.ArrayList;

import java.util.LinkedList;

import java.util.ListIterator;

import java.util.Scanner;

import java.awt.Font;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.BasicStroke;

import java.awt.GradientPaint;

import java.awt.geom.GeneralPath;

import java.awt.AlphaComposite;

import java.awt.Composite;

import java.awt.geom.*;

import java.awt.*;

import java.awt.BorderLayout;

import javax.swing.*;

import java.util.Hashtable;

import java.awt.event.*;

import javax.swing.event.*;

publicclass Comboxextends JApplet

{

finalint TRANSITION = 100;// Transition x,y coordinates of cells.

privatedouble x1, y1, x2, y2;// to plot lines from (x1,y1) to (x2,y2)

privatedouble xCells[][];// Every cell's points's x coordinates array

privatedouble yCells[][];// Every cell's points's y coordinates array

//private double rgb[][]; // Color red, green, blue for each point in each cell

private LayoutManager Layout;

private Container Panel;

private Drawing drawing;

privatedouble red[][];

privatedouble green[][];

privatedouble blue[][];

private String energyId[];

privateint sideId;

privateint sidesTotalNumber;// Total sides number for all cells

privateint rgbLength;// Total rgb color group number for all cells at each point

privateint cellsPointsNumber[];// point numbers of each cell.(each cell has how many points)

privateint numberofCells;

privateint numberofsets;

privateint id;//PlotEnergyId

privatedouble lineWidth;

double scale = 1.0;

private String currentPattern;

private JSlider dJSlider;

private JComboBox comb;

publicvoid ComboBoxDemo(){

Layout =new BorderLayout ();

getDrawingData();

scaleData();

currentPattern = energyId[0];

JLabel patternLabel1 =new JLabel("Select one set");

comb =new JComboBox(energyId);

drawing =new Drawing();

Panel = getContentPane();

Panel.setLayout (Layout);

Panel.add (comb, BorderLayout.SOUTH);

Panel.add(dJSlider, BorderLayout.EAST);

drawing.setBackground (Color.white);

drawing.setSize (600, 600);

comb.setEditable(false);

//comb.addActionListener(this);

comb.setForeground (Color.red);

comb.setBackground (Color.yellow);

comb.add(patternLabel1);

comb.setAlignmentX(Component.LEFT_ALIGNMENT);

comb.addActionListener(

new ActionListener()// anonymous inner class

{

publicvoid actionPerformed(ActionEvent e)

{

JComboBox cb = (JComboBox)e.getSource();

id = cb.getSelectedIndex();

}

}

);

SliderFrame();

Panel.add (drawing,"Center");

//setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

}

publicvoid SliderFrame()

{

int min = 1, max = 16, inc = 5;

dJSlider =new JSlider(min, max, 6);

dJSlider.setMajorTickSpacing(5);

dJSlider.setMinorTickSpacing(1);

dJSlider.setSnapToTicks(true);

dJSlider.setLabelTable(getLabelTable(min, max, inc));

dJSlider.setPaintTicks(true);

dJSlider.setPaintLabels(true);

dJSlider.addChangeListener(

new ChangeListener()// anonymous inner class

// handle change in slider value

{

publicvoid stateChanged(ChangeEvent e)

{

int value = ((JSlider)e.getSource()).getValue();

scale = (value+4)/10.0;

}

}

);

add(dJSlider,BorderLayout.EAST);// ADD Slider

}

private Hashtable getLabelTable(int min,int max,int inc){

Hashtable<Integer, JLabel> table =new Hashtable<Integer, JLabel>();

for(int j = min; j <= max; j+=inc){

String s = String.format("%.1f", (j+4)/10.0);

table.put(Integer.valueOf(j),new JLabel(s));

}

return table;

}

private AlphaComposite makeComposite(float alpha)

{

int type = AlphaComposite.SRC_OVER;

return(AlphaComposite.getInstance(type, alpha));

}

publicvoid getDrawingData()

{

TestCell p =new TestCell();

p.cellNumber();

numberofCells = p.getnumberofCells();

numberofsets = p.getnumberofsets();

xCells =newdouble[numberofCells][];

yCells =newdouble[numberofCells][];

for(int i = 0; i<numberofCells; i++)

{

xCells[i] =newdouble[numberofsets];// maximum points in ith cell

yCells[i] =newdouble[numberofsets];// maximum points in ith cell

}

cellsPointsNumber =newint[numberofCells];

red =newdouble[numberofCells][];

green =newdouble[numberofCells][];

blue =newdouble[numberofCells][];

for(int i=0; i><numberofCells; i++)

{

red[i]=newdouble[numberofsets];

green[i] =newdouble[numberofsets];

blue[i] =newdouble[numberofsets];

}

energyId =new String[numberofsets];

p.cellData();

p.energyId();

p.rgbs();

xCells = p.getxCells();

yCells = p.getyCells();

sidesTotalNumber = p.getsidesTotalNumber();

// rgb = p.getrgb();

red = p.getred();

green = p.getgreen();

blue = p.getblue();

energyId = p.getenergyId();

cellsPointsNumber = p.getcellsPointsNumber();//array: each cell has how many points

rgbLength = p.getrgbLength();

}

publicvoid scaleData()

{

for(int i = 0; i >< numberofCells; i++)

{

for(int j = 0; j<cellsPointsNumber[i]; j++)

{

xCells[i][j] = (xCells[i][j])*scale+TRANSITION;

yCells[i][j] = (yCells[i][j])*scale+TRANSITION;

}

}

}

publicvoid drawSides(Graphics2D g2d)

{

g2d.setColor(Color.GRAY);

g2d.setStroke(new BasicStroke((float)lineWidth));

for (int i = 0; i><numberofCells; i++)

{

for (int j = 0; j><cellsPointsNumber[i]-1; j++)

{

x1 = xCells[i][j];

y1 = yCells[i][j];

x2 = xCells[i][j+1];

y2 = yCells[i][j+1];

Line2D line =new Line2D.Double(x1,y1,x2,y2);

g2d.draw(line);// draw Sidess here

}

}

}

publicvoid drawPolygon(Graphics2D g2d)

{

for (int i = 0; i><numberofCells; i++)

{

Polygon poly =new Polygon();

float r = (float)red[i][id];

// red for ith cell,id is from 0 to numberofsets-1

// if id=0, means "Mixture"

float g = (float)green[i][id];// green for ith cell

float b = (float)blue[i][id];// blue for ith cell

List><Integer> xList =new ArrayList<Integer>();

List<Integer> yList =new ArrayList<Integer>();

for (int j = 0; j<cellsPointsNumber[i]; j++)

{

xList.add((int)(xCells[i][j]));

yList.add((int)(yCells[i][j]));

}

Integer[] x = xList.toArray(new Integer[xList.size()]);

Integer[] y = yList.toArray(new Integer[yList.size()]);

for (int s = 0; s><xList.size(); s++)

{

poly.addPoint(x[s],y[s]);

}

g2d.setColor(new Color(r, g, b));// transparent drawing

g2d.fillPolygon(poly);

}

}

class Drawingextends JPanel

{

publicvoid paintComponent(Graphics g)

{

//super.paintComponent( g ); // call superclass's paint method

//this.setBackground( Color.WHITE );

Graphics2D g2d = (Graphics2D)g;// cast g to Graphics 2D

getDrawingData();

ComboBoxDemo();

repaint();

drawPolygon(g2d);

//Composite originalComposite = g2d.getComposite();

//g2d.setColor(Color.GRAY);

drawSides(g2d);

g2d.setComposite(makeComposite(1.0f));

// g2d.setComposite(originalComposite);

}

// end method paintComponent

}

}// end class

>

[16200 byte] By [ardmorea] at [2007-11-27 10:00:22]
# 1

Swing related questions should be posted in the Swing forum.

Your are not creating an applet, your code is more like an application. An applet needs an init(...) method to build the GUI components.

I suggest you start by reading the Swing tutorial on "How to Make Applets", and then start with a simple example until you know what your are doing. Why write 100's of lines of code first. Learn the basics and then add more code.

http://java.sun.com/docs/books/tutorial/uiswing/components/applet.html

camickra at 2007-7-13 0:31:41 > top of Java-index,Java Essentials,Java Programming...
# 2

run this to see a major problem in your code

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class Testing

{

public void buildGUI()

{

Drawing dp = new Drawing();

dp.setPreferredSize(new Dimension(100,100));

JFrame f = new JFrame();

f.getContentPane().add(dp);

f.pack();

f.setLocationRelativeTo(null);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

}

public static void main(String[] args)

{

SwingUtilities.invokeLater(new Runnable(){

public void run(){

new Testing().buildGUI();

}

});

}

}

class Drawing extends JPanel

{

int x;

public void paintComponent(Graphics g)

{

//super.paintComponent( g ); // call superclass's paint method

//this.setBackground( Color.WHITE );

//Graphics2D g2d = (Graphics2D)g; // cast g to Graphics 2D

//getDrawingData();

//ComboBoxDemo();

repaint();//<--

System.out.println(x++);

//drawPolygon(g2d);

//Composite originalComposite = g2d.getComposite();

//g2d.setColor(Color.GRAY);

//drawSides(g2d);

//g2d.setComposite(makeComposite(1.0f));

//g2d.setComposite(originalComposite);

// end method paintComponent

}

}

Michael_Dunna at 2007-7-13 0:31:41 > top of Java-index,Java Essentials,Java Programming...
# 3
Yikes! Holy recursive calls, Batman!
petes1234a at 2007-7-13 0:31:41 > top of Java-index,Java Essentials,Java Programming...
# 4
So the repaint means paint forever and never stop?Why invented that?
ardmorea at 2007-7-13 0:31:41 > top of Java-index,Java Essentials,Java Programming...
# 5
> So the repaint means paint forever and never stop?> Why invented that?It's a good useful method. But look where you placed it.
petes1234a at 2007-7-13 0:31:41 > top of Java-index,Java Essentials,Java Programming...
# 6

> So the repaint means paint forever and never stop? Why invented that?

The repaint() method invokes the RepaintManager. When the repaint manager decides a component needs to be repainted it invokes the paint() method on that component. The paint() method in turn invokes the paintComponent() method, therefore you get your infinite loop.

You override the paintComponent() method to "DO" custom painting, not to "INVOKE" the RepaintManager.

camickra at 2007-7-13 0:31:41 > top of Java-index,Java Essentials,Java Programming...