This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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;
}

}

}
}


 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: how to draw ?
 
Similar Threads
MouseDragg
Messagebox with Mouseevent
jpane with scrollpane is not displaying
Rotating a JWindow with multiple JPanels
Dispalying random dates