File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Some painting problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Some painting problem" Watch "Some painting problem" New topic
Author

Some painting problem

saurangshu pandey
Greenhorn

Joined: Jun 19, 2002
Posts: 2
I was trying to make a bar chart from a given data. I used an extension JLabel (mentioned as inner class Leonardo) inside a scrollpane for the custom painting of the bars (rectangles).
The problem I am facing is the component is not visible when I open the screen. If I drag the scrool bar then the next area is painted and the component looks fine. If the frame is minimized and then maximized the custom painting is not visible. Any idea why it is doing so? I am pasting the full code in case some body wants to run it.
Thanks in advance,
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.util.Vector;
import java.util.Hashtable;
import java.util.Enumeration;
import java.awt.geom.*;
public class Reporter extends JFrame
{
private JPanel basePanel = new JPanel();
private Leonardo1 leo = null;
private JScrollPane scrollPane = new JScrollPane();
public Reporter()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
Reporter reporter = new Reporter();
reporter.pack();
reporter.setSize(700,500);
reporter.setVisible(true);
}
private void jbInit() throws Exception
{
this.setSize(new Dimension(679, 497));
this.getContentPane().setLayout(null);
this.setTitle("Process Reporter");
// this.addWindowListener(new Reporter_this_windowAdapter(this));
basePanel.setLayout(null);
basePanel.setBounds(new Rectangle(15, 60, 640, 405));
basePanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
/* Hard code data */
Vector d= new Vector();
for (int j=0; j<131;j++)
{
Data d1 = new Data(j*j*1.0,new java.sql.Date(System.currentTimeMillis()+j*1000000000),"yy","xx",Color.red);
d.add(d1);
}
leo = new Leonardo1(d);
/*End of Hard Code data*/
leo.setBounds(new Rectangle(10, 65, 656, 346));
scrollPane.setBounds(new Rectangle(10, 10, 620, 330));
scrollPane.getViewport().add(leo);
basePanel.add(scrollPane, null);
this.getContentPane().add(basePanel, null);
}
}
class Data implements java.io.Serializable
{
public double time;
public java.sql.Date day;
public String timeText;
public String dayText;
public Color color;
public Data(double t,java.sql.Date dt, String strT,String strD,java.awt.Color col)
{
time = t;
day=dt;
timeText=strT;
dayText=strD;
color=col;
}
public void setTime(double t) {time =t;}
public void setDay(java.sql.Date t) {day =t;}
public void setTimeText(String t) {timeText =t;}
public void setDayText(String t) {dayText =t;}
public void setColor(Color t) {color =t;}
public double getTime() {return time;}
public java.sql.Date getDay() {return day;}
public String getTimeText() {return timeText;}
public String getDayText() {return dayText;}
public Color getColor() {return color;}
}
class Leonardo1 extends JLabel implements java.io.Serializable
{
private Vector dataVec = new Vector();
private double xGap = 5.0;//Gap between 2 bars
private double xWidth = 12.0;//Width of a bar
private double yGap = 40.0;//Gap from start of Y axis to first data.
private int xLength = 645;//Length of the component
private int yLength = 330;//Height of the component
private int xMargin = 50;//The gap from the corner of the axis to the y axis
private int yMargin = 20;//The gap from the corner of the axis to the x axis
private double avgTime = 0.0;
protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
public int getWidth(){ return xLength;}
public int getHeight(){ return yLength;}
public Dimension getMaximumSize(){return new Dimension(xLength,yLength);}
public Dimension getMinimumSize(){return new Dimension(xLength,yLength);}
public Dimension getSize(Dimension rv){ return new Dimension(xLength,yLength);}
public Rectangle getBounds(Rectangle rv){ return new Rectangle(xLength,yLength); }
public Leonardo1(Vector vec) {
super();
setOpaque(true);
super.setBackground(Color.white);
setData(vec);
setBorder(noFocusBorder);
}
protected void setData(Vector vec){
if (vec == null) return;
dataVec = (Vector)vec.clone();
}
public void paintComponent(Graphics gin)
{
super.paintComponent(gin);
Graphics2D g2 = (Graphics2D)gin;
// setBackground(Color.white);
Color bl = Color.blue;
g2.setPaint(bl);
g2.setStroke(new BasicStroke(5.0f));
drawData(g2,dataVec);
g2.setPaint(Color.gray);
g2.setStroke(new BasicStroke(0.5f));
g2.draw(new Rectangle2D.Double(0, 0,xLength,yLength));
g2.setStroke(new BasicStroke(1.5f));
g2.draw(new Line2D.Double(xMargin, yMargin,xMargin,yLength -yMargin));
g2.draw(new Line2D.Double(xMargin, yLength -yMargin,xLength -xMargin,yLength -yMargin));
System.out.println("End1 ");
}

public Dimension getPreferredSize(){
if (dataVec.size()*(xGap+xWidth) > xLength)
{
xLength = (int)(dataVec.size()*(xGap+xWidth));
return new Dimension(xLength,yLength);
}
else
return new Dimension(xLength,yLength);
}
protected void drawData(Graphics2D g2, Vector vec)
{
if (vec == null || vec.size() == 0)
return;
//Check if expansion is required or not
if (vec.size()*(xGap+xWidth) > (xLength - 2*xMargin))
{
xLength = (int)(vec.size()*(xGap+xWidth)+xGap);//Expanded
setPreferredSize(new Dimension(xLength,yLength));
}
double maxTime = 0.0;
double minTime = 0.0;
double t=0.0;
double total=0.0;
for (int i=0;i<vec.size();i++)
{
if ((t = ((Data)vec.elementAt(i)).getTime()) > maxTime)
maxTime = t;
if (t < minTime)
minTime =t;
total += t;
}
avgTime = total/vec.size();
System.out.println("Avg:"+avgTime+" tot:"+total);
double scale = (yLength - 2*yMargin - 2*yGap )/(maxTime - minTime);
//Now the y-axis scale is calculated, we can draw the bar.
//Currently only bar is supported
//I asume data are in the order of dates otherwise have to sort it here
for (int i=1;i<=vec.size();i++)
{
//Set color to fill
if (((Data)vec.elementAt(i-1)).getColor() != null)
g2.setPaint(((Data)vec.elementAt(i-1)).getColor() );
//Fill
g2.fill(new Rectangle2D.Double(xMargin+xGap+(i-1)*(xGap+xWidth), yMargin+yGap+/*length of active y axis*/
(yLength-2*yMargin-2*yGap)-/*y value converted to scale*/(((Data)vec.elementAt(i-1)).getTime()- minTime)*scale,
xWidth,(((Data)vec.elementAt(i-1)).getTime()- minTime)*scale+yGap));
g2.setPaint(Color.black);
g2.setStroke(new BasicStroke(0.5f));
g2.draw(new Rectangle2D.Double(xMargin+xGap+(i-1)*(xGap+xWidth), yMargin+yGap+/*length of active y axis*/
(yLength-2*yMargin-2*yGap)-/*y value converted to scale*/(((Data)vec.elementAt(i-1)).getTime()- minTime)*scale,
xWidth,(((Data)vec.elementAt(i-1)).getTime()- minTime)*scale+yGap));
}
g2.setPaint(Color.blue);
float[] dash1 = {10.0f};
g2.setStroke(new BasicStroke(1.5f,BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f));
g2.draw(new Line2D.Double(xMargin,
yMargin+yGap+/*length of active y axis*/(yLength-2*yMargin-2*yGap)-
/*y value converted to scale*/(avgTime- minTime)*scale,
xMargin+(xGap+xWidth)*vec.size()+xGap,
yMargin+yGap+/*length of active y axis*/(yLength-2*yMargin-2*yGap)-
/*y value converted to scale*/(avgTime- minTime)*scale));
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Some painting problem
 
Similar Threads
Magnifying only a part of the image
calling another method from another class, interface issue
ActionListener as a parameter for a constructor
getting class file in java
Thread.sleep question