aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes how to draw ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "how to draw ?" Watch "how to draw ?" New topic
Author

how to draw ?

cat cater
Ranch Hand

Joined: Jun 06, 2005
Posts: 39
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

Joined: Jun 06, 2005
Posts: 39
thanked first.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
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

Joined: Sep 16, 2005
Posts: 1780
What is the purpose of isShow?


There is no emoticon for what I am feeling!
cat cater
Ranch Hand

Joined: Jun 06, 2005
Posts: 39
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;
}

}

}
}


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to draw ?