Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Conversion from Swing to SWT
Conversion from Swing to SWT [message #446235] Mon, 22 November 2004 06:07 Go to next message
Eclipse UserFriend
Originally posted by: spicydane.hotmail.com

can somebody plz help me convert this sing code to swt????????????????
atleast any guidance......
thanx

************************************************************ ************
import java.awt.*;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseListener;
import java.awt.event.*;
import java.util.*;
//swing
import javax.swing.*;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Shell;
//import org.eclipse.swt.events.*;



class GraphDesing extends JPanel implements MouseListener{

public Shell shell;

public static final int CHART_LINE = 0;
public static final int CHART_COLUMN = 1;
public static final int CHART_PIE = 2;

public static final int EFFECT_PLAIN = 0;
public static final int EFFECT_GRADIENT = 1;
public static final int EFFECT_IMAGE = 2;

protected int m_chartType = CHART_LINE;
protected JLabel m_title;
protected ChartPanel m_chart=null;
JLabel columnheader;

protected int m_nData;
protected int[] m_x1Data;
protected int[] m_x2Data;
protected int[] m_y1Data;
protected int[] m_y2Data;
protected int[] m_x1HData;
protected int[] m_x2HData;
protected int[] m_y1HData;
protected int[] m_y2HData;
protected String[] m_iconText;
protected int[] m_xicon;
protected int[] m_yicon;
protected int[] m_Sicon;
protected int m_ICLength;
protected int m_x1SHData;
protected int m_x2SHData;
protected int m_y1SHData;
protected int m_y2SHData;
protected int HLength = 0;
protected int SHLength = 0;
protected int iconLength = 0;
protected int[] m_colorData;
protected int[] hodata;
protected String[] ICData;
protected int m_xMin;
protected int m_xMax;
protected int m_yMin;
protected int m_yMax;

protected int m_PointCnt = 0;
protected int m_CurPointCnt = 0;
protected int m_PointW = 0;

protected int m_effectIndex = EFFECT_PLAIN;
protected Stroke m_stroke;
protected Image m_foregroundImage;
protected Color m_lineColor = Color.black;
protected Color m_columnColor = Color.blue;
protected double mousePush = 0.01;
protected double mousePushOld = 0;
protected double m_xRate = 0.01;
protected int m_Height = 0;
protected int m_Width = 0;
protected int m_STimeData;
protected int m_ETimeData;
public JScrollBar eventViewHScroll = null;
int mouseX;
int oldWidth;

public void setTime(int ST, int ET){
m_ETimeData = ET;
m_STimeData = ST;

}

public GraphDesing(){
super(new BorderLayout());
this.setBackground(Color.black);
//m_Width = this.getWidth()-10;
m_Height = this.getHeight();

m_nData = 0;

}
//parsing GraphData
public void parseGraphData(String cmd, int[] data){
String pdata = new String();
try{
StringTokenizer tok = new StringTokenizer(cmd, ";");
for(int i=0; tok.hasMoreTokens(); i++){
if( i >= 5 ) break;
pdata=tok.nextToken().trim();
data[i] = Integer.parseInt(pdata);
}
}catch(Exception e){
System.out.println(pdata);
e.printStackTrace();
}
}//end of setTableData

//parsing HorizonData
public void parseHorizonData(String cmd, int tmphleng){
try{
hodata = new int[tmphleng];
StringTokenizer tok = new StringTokenizer(cmd, ";");
for(int i=0; tok.hasMoreTokens(); i++){
if( i >= 4 ) break;
hodata[i] = Integer.parseInt(tok.nextToken().trim());
}
}catch(Exception e){
e.printStackTrace();
}
}//end of setHorizonData

public void setGraphData(String[] graphData)
{
clearAll();
int length = graphData.length;
int[] tmpData = new int[5];

int[] x1Data = new int[length];
int[] x2Data = new int[length];
int[] y1Data = new int[length];
int[] y2Data = new int[length];
int[] colorData = new int[length];

for( int i = 0; i < length; i++)
{
parseGraphData(graphData[i], tmpData );
x1Data[i] = tmpData[0];
y1Data[i] = tmpData[1];
x2Data[i] = tmpData[2];
y2Data[i] = tmpData[3];
colorData[i] = tmpData[4];
}

m_nData = length;
m_x1Data = x1Data;
m_x2Data = x2Data;
m_y1Data = y1Data;
m_y2Data = y2Data;
m_colorData = colorData;

m_xMin = m_xMax = 0;// To include 0 into the interval
m_yMin = m_yMax = 0;

for (int k=0; k<m_nData; k++){
m_xMin = Math.min(m_xMin, m_x1Data[k]);
m_xMax = Math.max(m_xMax, m_x1Data[k]);
m_xMin = Math.min(m_xMin, m_x2Data[k]);
m_xMax = Math.max(m_xMax, m_x2Data[k]);

m_yMin = Math.min(m_yMin, m_y1Data[k]);
m_yMax = Math.max(m_yMax, m_y1Data[k]);
m_yMin = Math.min(m_yMin, m_y2Data[k]);
m_yMax = Math.max(m_yMax, m_y2Data[k]);
}
if (m_xMin == m_xMax)
m_xMax++;
if (m_yMin == m_yMax)
m_yMax++;
if(mousePush == 0.01){
setPreferredSize(new Dimension((int)(m_xMax*m_xRate),m_yMax+30));
resize(new Dimension((int)(m_xMax*m_xRate),m_yMax+30));
repaint();

}
// Ž«±Ý »ýŒº //
columnheader = new JLabel() {
Font f = new Font("Arial",Font.PLAIN | Font.BOLD,10);
public void paintComponent(Graphics g) {
int PointWCnt = 0;
int PointW=0;
int Width = 0;
int LineCount = 0;
super.paintComponent(g);
Rectangle r = g.getClipBounds();
g.setFont(f);
g.setColor(Color.black);

Width = this.getWidth();


m_CurPointCnt = (int)(((float)Width/(m_xMax-m_xMin))*15);
m_CurPointCnt = m_CurPointCnt <= 0 ? 1 : m_CurPointCnt;

m_PointW = ((m_xMax-m_xMin)/m_CurPointCnt)+1;
m_PointW = m_PointW <= 0 ? 1 : m_PointW;

PointWCnt = (Width-10)/m_CurPointCnt;
PointWCnt = PointWCnt <= 0 ? 1 : PointWCnt;

double Wp = m_xMax * m_xRate / (double)(m_ETimeData-m_STimeData);
int TCount = 0;
int tmpD = 0;
//paint scale
if(m_xRate < 0.05){
for(double i=0; i< m_xMax * m_xRate ; i+=Wp){
g.drawLine((int)(i),0,(int)(i),3);
if(TCount % 5 == 0){
g.drawLine((int)(i),0,(int)(i),7);
g.drawString("" + (m_STimeData + TCount),(int)(i),16);
}
TCount++;
}
}
else
{
Wp = m_xMax * m_xRate / (double)((m_ETimeData-m_STimeData)*5);
for(double i=0; i<Width; i+=Wp){
g.drawLine((int)(i),0,(int)(i),3);
if(TCount % 5 == 0){
g.drawLine((int)(i),0,(int)(i),7);
g.drawString("" + (m_STimeData+tmpD),(int)(i),16);
tmpD++;
}
TCount++;
}
}

}
public Dimension getPreferredSize() {
return new Dimension((int)m_chart.getPreferredSize().getWidth(),25);
}
};//end of columnheader

columnheader.setOpaque(true);

m_chart = new ChartPanel();
add(columnheader, BorderLayout.NORTH);
add(m_chart, BorderLayout.CENTER);
m_chart.addMouseListener(this);
}

public void clearAll()
{
m_nData = 0;
m_x1Data = null;
m_x2Data = null;
m_y1Data= null;
m_y2Data= null;
m_x1HData= null;
m_x2HData= null;
m_y1HData= null;
m_y2HData= null;
HLength = 0;
SHLength = 0;
m_colorData= null;
m_xMin = 0;
m_xMax= 0;
m_yMin= 0;
m_yMax= 0;

m_PointCnt = 0;
m_CurPointCnt = 0;
m_PointW = 0;

mousePush = 0.01;
mousePushOld = 0;
m_xRate = 0.01;

if( columnheader != null ) remove (columnheader);
if( m_chart != null ) remove (m_chart);

m_chart = null;
columnheader = null;

repaint();
}

public void mouseReleased(MouseEvent e) { //event Œ³Á€

if( m_nData <= 0 ) return;
double dif;
int WinX;
mouseX = e.getX();
int mouseY = e.getY();

//resize
//m_chart.xChartToScreen(mouseX);
//m_chart.yChartToScreen(mouseY);
WinX = mouseX - eventViewHScroll.getValue();
int kk = e.getButton();

if(kk == 1){
ZoomIn();
} else if(kk == 3) {
ZoomOut();
}

dif = (mouseX/mousePushOld)*mousePush;
eventViewHScroll.setValue((int)(dif)-WinX);


}

public void mousePressed(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {

}

public void mouseExited(MouseEvent e) {
}

public void mouseClicked(MouseEvent e) {

}

public void setStroke(Stroke stroke){
m_stroke = stroke;
if( m_chart != null ) m_chart.repaint();
}

public void setLineColor(Color c){
m_lineColor = c;
if( m_chart != null ) m_chart.repaint();
}
public void ZoomIn(){//ZoomIn
//int m_Width = 0;
int tmp_x;

mousePushOld = mousePush;
mousePush = mousePush / 0.8;
if(mousePush >= 0.01 && mousePush <= 1){
if( mousePush != mousePushOld )
{
m_xRate = mousePush;
}
else if( (mousePush == 0.01) && (mousePushOld == 0) ){
mousePush = 0.01;
m_xRate = 0.01;
}

tmp_x = (int)(m_xMax *m_xRate);

setPreferredSize(new Dimension(tmp_x,m_yMax+30));
resize(new Dimension(tmp_x,m_yMax+30));

if( m_chart != null ) m_chart.repaint();
}else{
MessageDialog.openInformation(shell,"WARNING","No more Zoom In");
//JOptionPane.showMessageDialog(this, "No more Zoom In", "WARNING",
JOptionPane.WARNING_MESSAGE);
mousePush = mousePushOld;
}
}
public void ZoomOut(){//ZoomOut
//int m_Width = 0;
int tmp_x;

mousePushOld = mousePush;
mousePush = mousePush * 0.8;
if(mousePush >= 0.01 && mousePush <= 1){
if( mousePush != mousePushOld )
{
m_xRate = mousePush;
}else if( (mousePush == 0.01) && (mousePushOld == 0) ){
mousePush = 0.01;
m_xRate = 0.01;
}

tmp_x = (int)(m_xMax *m_xRate);

setPreferredSize(new Dimension(tmp_x,m_yMax+30));
resize(new Dimension(tmp_x,m_yMax+30));

if( m_chart != null ) m_chart.repaint();
}else{
MessageDialog.openInformation(shell,"WARNING","NO more Zoom Out");
//JOptionPane.showMessageDialog(this, "NO more Zoom Out", "WARNING",
JOptionPane.WARNING_MESSAGE);
mousePush = mousePushOld;
}
}

public void SelectHorizon(String[] SHData, int SInt){
SHLength = SHData.length;
int tmp_SHLength = SHLength * 4;
int[] x1SHData = new int[SHLength];
int[] x2SHData = new int[SHLength];
int[] y1SHData = new int[SHLength];
int[] y2SHData = new int[SHLength];

for( int i = 0; i < SHLength; i++)
{
parseHorizonData(SHData[i],tmp_SHLength);
x1SHData[i] = hodata[0];
y1SHData[i] = hodata[1];
x2SHData[i] = hodata[2];
y2SHData[i] = hodata[3];
}
m_x1SHData = x1SHData[SInt];
m_x2SHData = x2SHData[SInt];
m_y1SHData = y1SHData[SInt];
m_y2SHData = y2SHData[SInt];

m_chart.repaint();

}
//parsing icon Data
public void parseIcon_Data(String cmd, int tmp_leng){
try{
ICData = new String[tmp_leng];
StringTokenizer tok = new StringTokenizer(cmd, ";");
for(int i=0; tok.hasMoreTokens(); i++){
if( i >= 4 ) break;
ICData[i] = tok.nextToken().trim();
}
}catch(Exception e){
e.printStackTrace();
}
}//end of parseIcon_Data
public void showIcon(String[] iconData){
iconLength = iconData.length;
int tmp_iconLength = iconLength * 4;
String[] icontext = new String[iconLength];
int[] xicon = new int[iconLength];
int[] yicon = new int[iconLength];
int[] Sicon = new int[iconLength];
for( int i = 0; i < iconLength; i++)
{
parseIcon_Data(iconData[i],tmp_iconLength);
icontext[i] = ICData[0];
xicon[i] = Integer.parseInt(ICData[1]);
yicon[i] = Integer.parseInt(ICData[2]);
Sicon[i] = Integer.parseInt(ICData[3]);
}
m_ICLength = iconLength;
m_iconText = icontext;
m_xicon = xicon;
m_yicon = yicon;
m_Sicon = Sicon;

m_chart.repaint();

}

public void ShowHorizon(String[] HorizonData){//show horizon
HLength = HorizonData.length;
int tmp_HLength = HLength * 4;
int[] x1HData = new int[HLength];
int[] x2HData = new int[HLength];
int[] y1HData = new int[HLength];
int[] y2HData = new int[HLength];

for( int i = 0; i < HLength; i++)
{
parseHorizonData(HorizonData[i],tmp_HLength);
x1HData[i] = hodata[0];
y1HData[i] = hodata[1];
x2HData[i] = hodata[2];
y2HData[i] = hodata[3];

}
m_x1HData = x1HData;
m_x2HData = x2HData;
m_y1HData = y1HData;
m_y2HData = y2HData;

m_chart.repaint();

}


public void HideHorizon(){//hide horizon
HLength = 0;
m_x1HData = null;
m_x2HData = null;
m_y1HData = null;
m_y2HData = null;

m_chart.repaint();

}
class ChartPanel extends JComponent{
int m_xMargin = 5;
int m_yMargin = 5;
int m_pieGap = 10;

protected double m_yRate;
int m_x;
int m_y;
int m_w;
int m_h;

ChartPanel(){
enableEvents(ComponentEvent.COMPONENT_RESIZED);
}

protected void processComponentEvent(ComponentEvent e){
calcDimensions();
}

public void calcDimensions(){
Dimension d = getSize();
m_x = m_xMargin;
m_y = m_yMargin;
m_w = d.width-2*m_xMargin;
m_h = d.height-2*m_yMargin;

}

public int xChartToScreen(int x){
return m_x + (x-m_xMin)*m_w/(m_xMax-m_xMin);
}

public int yChartToScreen(int y){
return m_y + (m_yMax-y)*m_h/(m_yMax-m_yMin);
}

public void HorizonPaint(Graphics g){


}

public void paintComponent(Graphics g){//graph ±×ž®±â

if( columnheader == null ) return;
if( m_nData <= 0 ) return;


g.setColor(Color.black);
g.fillRect(0, 0, m_Width,m_Height);

g.setColor(Color.blue);
for( int k = 0; k < m_nData; k++)//Graph
{
if(m_colorData[k] < 5){
switch(m_colorData[k]){
case 0:
g.setColor(Color.white);
break;
case 1:
g.setColor(Color.red);
break;
case 2 :
g.setColor(Color.green);
break;
case 3 :
g.setColor(Color.blue);
break;
default:
g.setColor(Color.yellow);
break;
}
}
g.drawLine((int)((m_x1Data[k])*m_xRate),(int)(m_y1Data[k]),( int)((m_x2Data[k])*m_xRate),(int)(m_y2Data[k]));

}

Image img = null;
for(int j = 0;j < m_ICLength;j++)
{
switch(m_Sicon[j]){
case 0:
img = Toolkit.getDefaultToolkit().getImage("./icons/KernelTimer.gif ");
break;
case 1:
img = Toolkit.getDefaultToolkit().getImage("./icons/SysCall.gif");
break;
case 2:
img = Toolkit.getDefaultToolkit().getImage("./icons/Trap.gif");
break;
case 3:
img = Toolkit.getDefaultToolkit().getImage("./icons/SchedChng.gif ");
break;
case 4:
img = Toolkit.getDefaultToolkit().getImage("./icons/BottomHalf.gif ");
break;
case 5:
img = Toolkit.getDefaultToolkit().getImage("./icons/IOStart.gif");
break;
case 6:
img = Toolkit.getDefaultToolkit().getImage("./icons/IOEnd.gif");
break;
case 7:
img = Toolkit.getDefaultToolkit().getImage("./icons/IRQ.gif");
break;
}
g.drawImage(img,(int)((m_xicon[j])*m_xRate),m_yicon[j] - 15,this);
g.drawString(m_iconText[j],(int)((m_xicon[j]*m_xRate)+15),m_ yicon[j]);

}
if(HLength > 0){//show horizon
g.setColor(Color.GREEN);
for(int i =0; i < HLength ;i++){

g.drawLine((int)((m_x1HData[i])*m_xRate),(int)(m_y1HData[i]) ,(int)((m_x2HData[i])*m_xRate),
(int)(m_y2HData[i]));
}
}

if(SHLength > 0){//selected list
g.setColor(Color.red);
g.drawLine((int)((m_x1SHData)*m_xRate),(int)(m_y1SHData),(in t)((m_x2SHData)*m_xRate),
(int)(m_y2SHData));
}

}

}//end of ChartPanel

class Chart extends JComponent{

}

}//end of Class

************************************************************ ****************
Re: Conversion from Swing to SWT [message #446237 is a reply to message #446235] Mon, 22 November 2004 08:48 Go to previous message
Eclipse UserFriend
Originally posted by: mnaglik.nospam.gazeta.pl

di napisał(a):
> can somebody plz help me convert this sing code to swt????????????????
> atleast any guidance......
Maybe see:
http://swingwt.sourceforge.net/

M.
Previous Topic:Swing JPanel
Next Topic:Combo/List box with images?
Goto Forum:
  


Current Time: Mon Oct 26 14:53:48 GMT 2020

Powered by FUDForum. Page generated in 0.01743 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top