cannot find sysmol paintComponent

Thanks, what wrong with my code structure?

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.*;

import java.applet.Applet;

// import AppletAndApplicationExample.AppQuitter;

publicclass Comboxextends JApplet

{

private LayoutManager Layout;

private Container Panel;

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

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

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

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

privatedouble red[][];

privatedouble green[][];

privatedouble blue[][];

private String energyId[];

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

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

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;

privatedouble lineWidth;

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

double scale = 1.0;

privateint id;

privatedouble maximumX;

privatedouble maximumY;

private JSlider dJSlider;

private JComboBox comb;

publicvoid init(){

Layout =new BorderLayout ();

FlowLayout Layout1 =new FlowLayout();

getEnergyIdData();

getDrawingData();

comb =new JComboBox(energyId);

comb.setAlignmentX(Component.LEFT_ALIGNMENT);

comb.setEditable(false);

comb.setForeground (Color.red);

comb.setBackground (Color.yellow);

comb.setAlignmentX(Component.LEFT_ALIGNMENT);

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

comb.addActionListener(

new ActionListener()// anonymous inner class

{

publicvoid actionPerformed(ActionEvent e)

{

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

int id = cb.getSelectedIndex();

setColor(id);

}

}

);

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;

scaleData(scale);

}

}

);

JPanel cPanel =new JPanel();

cPanel.setLayout(Layout1);

cPanel.add(comb);

cPanel.add(dJSlider);

Panel = getContentPane();

Panel.setLayout (Layout);

Panel.add (cPanel, BorderLayout.EAST);

}

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 getEnergyIdData()

{

TestCell p =new TestCell();

p.cellNumber();

numberofCells = p.getnumberofCells();

numberofsets = p.getnumberofsets();

energyId =new String[numberofsets];

p.energyId();

energyId = p.getenergyId();

}

publicvoid paintComponent(Graphics g)

{

super.paintComponent(g);//Here<<===================

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

g2d.setBackground(Color.LIGHT_GRAY);

drawPolygon(g2d);

//Composite originalComposite = g2d.getComposite();

drawSides(g2d);

g2d.setComposite(makeComposite(1.0f));

// g2d.setComposite(originalComposite);

}

publicvoid getDrawingData()

{

TestCell p =new TestCell();

p.cellNumber();

numberofCells = p.getnumberofCells();

numberofsets = p.getnumberofsets();

xCells =newdouble[numberofCells][];

yCells =newdouble[numberofCells][];

xCells0 =newdouble[numberofCells][];

yCells0 =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

xCells0[i] =newdouble[numberofsets];// copy for xCells

yCells0[i] =newdouble[numberofsets];// copy for yCells

}

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];

}

p.cellData();

p.rgbs();

xCells = p.getxCells();

yCells = p.getyCells();

// clone arrays

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

{

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

{

xCells0[i][j] = xCells[i][j];

yCells0[i][j] = yCells[i][j];

System.out.println(xCells[i][j]);

System.out.println(xCells0[i][j]);

}

}

sidesTotalNumber = p.getsidesTotalNumber();

red = p.getred();

green = p.getgreen();

blue = p.getblue();

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

rgbLength = p.getrgbLength();

}

publicvoid scaleData(double scale)

{

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

{

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

{

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

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

}

}

repaint();

}

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 setColor(int id)

{this.id = id;

repaint();

}

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);

}

// end method paintComponent

}

publicstaticvoid main(String[] args){

/* Set up the frame that holds the applet. */

JFrame appletFrame =new JFrame("My Applet");

Applet theApplet =new Combox();

appletFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

appletFrame.setSize(800,800);

appletFrame.add(theApplet,"Center");// applet fills

/* Now start invoking applet methods. */

theApplet.init();

//appletFrame.pack(); // resize frame to fit initialized

// applet

appletFrame.setVisible(true);// bring frame and initialized

// applet on screen

}

}// end class

>

[18570 byte] By [ardmorea] at [2007-11-27 10:35:33]
# 1

Take a look at the JApplet API. does it have a paintComponent method? Would you be better off drawing on a JPanel and adding that panel to the JApplet?

petes1234a at 2007-7-28 18:35:40 > top of Java-index,Java Essentials,New To Java...
# 2

Also, if you look at JFrame, you'll see the same thing. JFrames and JApplets are top-level windows. I have never painted directly on them, but rather I have either painted on a JPanel and added this, or painted on the top-level window's contentPane.

Your best bet (IMHO) is to do most of your work in a class that subclasses JPanel. Then in your JApplet init method, you add this JPanel to the JApplet's content pane. This gives you the added flexibility of letting you use the panel in a stand alone app or a japplet with just a few lines of code.

Message was edited by:

petes1234

petes1234a at 2007-7-28 18:35:40 > top of Java-index,Java Essentials,New To Java...
# 3

Thanks for your advice

ardmorea at 2007-7-28 18:35:40 > top of Java-index,Java Essentials,New To Java...
# 4

> Thanks for your advice

your welcome. of course my advice has been known to be wrong at times. :)

petes1234a at 2007-7-28 18:35:40 > top of Java-index,Java Essentials,New To Java...