display incorrctly!
Hi, in my code. I want to plot a 2D object, a JSlider and two comb box.
But the 2D object can't displayed correctly, can you look at my code?
Please don't provide the link of sun swing tutorial, I have studied many times, but no achievement.
import java.awt.event.*;
import javax.swing.event.*;
import java.applet.Applet;
publicclass Comboxextends JApplet{
...
private JSlider dJSlider;
private JComboBox comb;
private JComboBox alfa;
private Container panel;
publicvoid init()
{
panel = getContentPane();
drawing =new Drawing(a,id,scale);
JPanel p2 =new JPanel();
panel.add(p2);
p2.add(drawing);// this drawing can't be plotted,only a small white square is showed.
p2.add(new getSlider(),BorderLayout.NORTH);
p2.add(new getComboPanel(),BorderLayout.EAST);
}
class getSliderextends JPanel{
public getSlider(){
...
add(dJSlider);
}
}
class getComboPanelextends JPanel{
public getComboPanel(){
...
comb =new JComboBox(e);
layout =new GridBagLayout();
setLayout(layout);
alfa =new JComboBox(af);
add(comb);
add(alfa);
}
}
class Drawingextends JPanel
{
//plot 2D
...
}
publicstaticvoid main(String[] args)
{
...
JFrame a =new JFrame("show");
Applet b =new Combox();
...
}
But if the code becomes
publicvoid init()
{
panel = getContentPane();
drawing =new Drawing(a,id,scale);
panel.add(drawing);
panel.add(new getSlider(),BorderLayout.NORTH);
panel.add(new getComboPanel(),BorderLayout.EAST);
}
the plot is right.why?
Thanks
[3570 byte] By [
ardmorea] at [2007-11-27 10:52:37]

Try
p2.add(drawing);// this drawing can't be plotted,only a small white square is showed.
p2.add(new getSlider(),BorderLayout.NORTH);
p2.add(new getComboPanel(),BorderLayout.EAST);
panel.add(p2);
Sounds like an update/refresh problem to me. By the way, your way of naming classes like methods is most nauseating.
> Try
>
> p2.add(drawing);// this drawing
> can't be plotted,only a small white square is
> showed.
> p2.add(new
> getSlider(),BorderLayout.NORTH);
> p2.add(new
> getComboPanel(),BorderLayout.EAST);
>panel.add(p2);
> update/refresh problem to me. By the way, your way
> of naming classes like methods is most nauseating.
Rene? I don't think we've been on here at the same time in MONTHS. How are you?
> Rene? I don't think we've been on here at the same
> time in MONTHS. How are you?
Completely fscked. Spent two hours and 150 km riding my motorcycle through my town trying to get a clear head. No chance. Now it's 3:24 am and I haven't even eaten.
You?
> Rene? I don't think we've been on here at the same
> time in MONTHS. How are you?
He's been trying to avoid you ;)
> > Rene? I don't think we've been on here at the
> same
> > time in MONTHS. How are you?
>
> Completely fscked. Spent two hours and 150 km riding
> my motorcycle through my town trying to get a clear
> head. No chance. Now it's 3:24 am and I haven't even
> eaten.
>
> You?
In comparison... I'm great, thanks. Sorry to hear things aren't going well; hopefully things take a turn for the better and you'll get to grab a bite to eat :-)
> > Rene? I don't think we've been on here at the
> same
> > time in MONTHS. How are you?
>
> He's been trying to avoid you ;)
Not that I blame him or anything ...
>Try
p2.add(drawing);// this drawing can't be plotted,only a small white square is showed.
p2.add(new getSlider(),BorderLayout.NORTH);
p2.add(new getComboPanel(),BorderLayout.EAST);
panel.add(p2);
>
still wrong!
but you're not showing the rest of the forum that little Drawing JPanel business. Show it to them and let's see what they have to say about it. I still haven't figured out what it's supposed to do.
drawing will plot a ploygon and fill a ploygon.(contains two methods)
drawPolygon(g2d);
drawSides(g2d);
pets1234, you have traced my poster so long.
here is the Drawing class.
class Drawing extends JPanel
{
final int TRANSITION = 100; // Transition x,y coordinates of cells.
private double x1, y1, x2, y2; // to plot lines from (x1,y1) to (x2,y2)
private double xCells[][]; // Every cell's points's x coordinates array
private double yCells[][]; // Every cell's points's y coordinates array
private double rgb[][]; // Color red, green, blue for each point in each cell
private double red[][];
private double green[][];
private double blue[][];
private double lineWidth;
private int sideId;
private int sidesTotalNumber; // Total sides number for all cells
private int rgbLength; // Total rgb color group number for all cells at each point
private int cellsPointsNumber[]; // point numbers of each cell.(each cell has how many points)
private int numberofCells;
private int numberofsets;
int a,id;
double scale;
Drawing(int a, int id, double scale)
{this.a = a;
this.id = id;
this.scale = scale;
}
public void setscale(double scale)
{
this.scale = scale;
repaint();
}
public void setid(int id)
{
this.id = id;
repaint();
}
public void seta(int a)
{
this.a = a;
repaint();
}
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();
if (a==0)
drawPolygon(g2d);
drawSides(g2d);
}
// end method paintComponent
public void getDrawingData()
{
TestCell p = new TestCell();
p.cellData();
p.rgbs();
xCells = p.getxCells();
yCells = p.getyCells();
sidesTotalNumber = p.getsidesTotalNumber();
red = p.getred();
green = p.getgreen();
blue = p.getblue();
rgbLength = p.getrgbLength();
cellsPointsNumber = p.getcellsPointsNumber();//array: each cell has how many points
numberofCells = p.getnumberofCells();
numberofsets = p.getnumberofsets();
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;
}
}
}
public void 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
}
}
}
public void 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);
}
}
}
>
It has grown quite a bit since I last saw it.
actual, I post the whole code once time, but nobody interests it, just say ok,please see the link of tutorial.
I am tired.
panel = getContentPane();
drawing = new Drawing(a,id,scale);
JPanel p2 = new JPanel();
panel.add(p2);
p2.add(drawing);
p2.add(new getSlider(),BorderLayout.NORTH);
p2.add(new getComboPanel(),BorderLayout.EAST);
// this drawing can't be plotted,only a small white square is showed.
As expected. The default layout manager for a JPanel is FlowLayout. You need to set the
layout (of p2) to BorderLayout for things to work the way you want in this code.
Why is the drawing panel so small?
The default size of a JPanel is 10, 10. The preferredSize is computed by its layout
manager as it queries the child components in the process of laying them out. When you do
custom drawing on a panel that has no components its parent container has no idea what
size it needs for display so it uses the default. In a BorderLayout center section or in a
cell of a GridLayout the panel is expanded to fill the available space. In a layout that
respects and uses the preferredSize for display, egs, FlowLayout and GridBagLayout, the
panel is asked for its display size. If the panel has no components it reports back the
default size of 10, 10.
To fix this you can call setPreferredSize on the panel reference/instance or you can
override getPreferredSize in the panel class and return the Dimension desired for proper
display. Or you can use a layout such as BorderLayout where the parent container controls
the display size.
drawing.setPreferredSize(new Dimension(400,400));
// or, the override:
class Drawing...
protected void paintComponent(...
...
}
public Dimension getPreferredSize() {
return new Dimension(400,400);
}
}
