Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to draw ?

 
cat cater
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i drawed four small rectangle on JPanel. when switched other program,and back this. now ,these rectangle are not display. i try to draw them
in paint(Graphics g). but no succeed. what event write in?

code:
public class JBaseBand extends JPanel {

protected String title = "";

private boolean isShow = true;

protected QuickRep parent = null;

public JBaseBand(){

setLayout(null);

this.setFocusable(true);

Border border = BorderFactory.createLineBorder(Color.red);

setBorder(border);

this.setBackground(Color.WHITE);

EventList();
}

public void paint(Graphics g){

super.paint(g);

Graphics2D g2 = (Graphics2D)g;

g2.setFont(new Font("SansSerif",Font.ITALIC,12));

g2.drawString(title,0,15);

if(isShow){

drawRect(g2);

parent.isChoose = this;

isShow = false;

}

}
//draw four small rectangle
public void drawRect(Graphics2D g2){

g2.fillRect(1,1,5,5); //左上角

g2.fillRect((getWidth()-5)/2,1,5,5); //上中间

g2.fillRect(getWidth()-5,1,5,5); //右上角

g2.fillRect(1,getHeight()-5,5,5); //左下角

g2.fillRect((getWidth()-5)/2,getHeight()-5,5,5); //下中间

g2.fillRect(getWidth()-5,getHeight()-5,5,5); //右下角

}

}
 
cat cater
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanked first.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
isShow = false;

The next time the JPanel paints the rectangles don't get drawn. Understand that you can't just draw something once and expect it to stay on the screen. Java must repaint whenever relevent state changes, which in any GUI is quite often, and every time it repaints it will paint over the area you drew the rectangles on, thus they need to be drawn again.

Read http://java.sun.com/docs/books/tutorial/2d/index.html and http://java.sun.com/docs/books/tutorial/uiswing/14painting/index.html
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the purpose of isShow?
 
cat cater
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh,i changed "public boolean isShow = false", and write some code in other class'paint.now it's ok. Thanks everybody.
code:
public class QuickRep extends JPanel{
public QuickRep(){
//when JBaseBand'mousePressed has active,
//and isChoose = JBaseBand;
Component isChoose = null;
add(new JBaseBand());
}
//overwide paint of QuickRep
public void paint(Graphics g){
super.paint(g);

if (isChoose != null){

if (isChoose instanceof Band){
isChoose.repaint();
((JBaseBand)isChoose).isShow = true;
}

}

}
}


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic