Never call paint
or paintComponent
methods.
- Just call
repaint
in the method shown by you. - Make sure you have called
board.setOpaque(true);
at the start.
Good luck.
Question
I did update, paint, repaint graphics but i can't see that all... i was checked viewport but it is limited my View Panel...!
I can't know how can i do... T-T
And i did update that when scrolls are moving
top.add(tf1);
top.add(new Label("×"));
top.add(tf2);
Button set = new Button("SET!!");
set.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
try{
view.remove(board);
}catch(NullPointerException e111){
}
int x=Integer.parseInt(tf1.getText()), y=Integer.parseInt(tf2.getText());
board = new Board(x,y);
view.add(board);
view.setPreferredSize(new Dimension(x*20, y*20));
view.setBounds(0, 0, x*20, y*20);
}
});
top.add(set);
rp.add(top);
editView.setBounds(0, 100, 500, 600);
editView.getViewport().addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
try{
// board.update(board.getGraphics());
// board.repaint();
// board.paint(board.getGraphics());
// board.paintComponents(getGraphics());
/*view.remove(board);
view.add(board);*/
}catch(NullPointerException e2){
}
}
});
rp.add(editView);
'view' is just a Panel(null); and~ inner class Board = board
class Board extends JPanel{
public Image board;
public int y,x;
Board(int x, int y){
this.x = x;
this.y = y;
setBounds(0, 0, x*20, y*20);
setPreferredSize(new Dimension(x*20, y*20));
addMouseListener(this);
addMouseMotionListener(this);
setOpaque(true);
}
@Override
public void paint(Graphics g) {
board = createImage(x*20, y*20);
Graphics g2 = board.getGraphics();
g2.setColor(Color.white);
g2.fillRect(0, 0, x*20, y*20);
g2.setColor(Color.black);
for(int i=0;i<y;i++){
if(i%10==0){
g2.setColor(Color.red);
}
g2.drawLine(0, i*20, x*20, i*20);
if(i%10==0){
g2.setColor(Color.black);
}
}
for(int i=0;i<x;i++){
if(i%10==0){
g2.setColor(Color.red);
}
g2.drawLine(i*20, 0, i*20, y*20);
if(i%10==0){
g2.setColor(Color.black);
}
}
g.drawImage(board, 0, 0, x*20, y*20, null);
}
i did like this things... i try many ways.. but same i can't fix it... =_=...
please help me!!~~
structure : JScrollPane - Panel - Panel
panel define size method : setPreferredSize(new Dimension(x,y))
i use only graphics when i draw it.
Solution
Never call paint
or paintComponent
methods.
repaint
in the method shown by you.board.setOpaque(true);
at the start.Good luck.
OTHER TIPS
I fixed that problem How i create a New ScrollPane... =_=
like this
class ComponentScroller extends Container {
private static final long serialVersionUID = -4959363258095925908L;
Scrollbar horizontal, vertical;
Panel scrollarea = new Panel();
Component component;
int orgX, orgY;
public ComponentScroller() {
scrollarea.setLayout( null );
horizontal = new Scrollbar( Scrollbar.HORIZONTAL );
vertical = new Scrollbar( Scrollbar.VERTICAL );
setLayout( new BorderLayout() );
add( "Center", scrollarea );
add( "South", horizontal );
add( "East", vertical );
}
ComponentScroller( Component comp ) {
scrollarea.setLayout( null ); // We'll handle the layout
scrollarea.add( component = comp );
horizontal = new Scrollbar( Scrollbar.HORIZONTAL );
horizontal.setMaximum( component.getSize().width);
horizontal.addAdjustmentListener( new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
component.setLocation( orgX = -e.getValue(), orgY );
}
} );
vertical = new Scrollbar( Scrollbar.VERTICAL );
vertical.setMaximum( component.getSize().height);
vertical.addAdjustmentListener( new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
component.setLocation( orgX, orgY = -e.getValue() );
}
} );
setLayout( new BorderLayout() );
add( "Center", scrollarea );
add( "South", horizontal );
add( "East", vertical );
}
public void add_e(Component comp){
scrollarea.add( component = comp );
horizontal.setMaximum( component.getSize().width );
horizontal.addAdjustmentListener( new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
component.setLocation( orgX = -e.getValue(), orgY );
}
} );
vertical.setMaximum( component.getSize().height);
vertical.addAdjustmentListener( new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
component.setLocation( orgX, orgY = -e.getValue() );
}
} );
doLayout();
}
public void doLayout() {
super.doLayout();
horizontal.setVisibleAmount( scrollarea.getSize().width );
vertical.setVisibleAmount( scrollarea.getSize().height );
}
}
I can't find problem on existing source so I create new things look like it... =_=