不调用repaint(),如何画图?

johnpanq 2004-06-17 10:23:01
JFrame中加入一个JTabbedPane
JTabbedPane中加入多个JPanel
这些JPanel用来画直线,多边形等。
当JPanel响应mouse事件后,我调用repaint()来画图,这时JTabbedPane会出现重影,按最大化,最小化按钮后则正常,请问如休解决repaint()后重影的问题。
还有调用repaint会清除先前画的图,而我想一直画下去,该如何解决?多谢!
...全文
166 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
johnpanq 2004-06-18
  • 打赏
  • 举报
回复
To:icy_csdn
你的这个程序,是在程序运行后自动画图,如果我想动态画图呢?还是调用paintComponent吗?比如说通过按某个按钮来决定画什么图形。
shuneng 2004-06-18
  • 打赏
  • 举报
回复
mark
johnpanq 2004-06-18
  • 打赏
  • 举报
回复
多谢楼上几位。
还有一个问题,就是调用repaint会重绘,而我不想重绘,即保留原来的图形,在它的基础上继续画。该如何操作?
jellen 2004-06-18
  • 打赏
  • 举报
回复
同意楼上的,当你决定画什么图形了,然后在移动鼠标的时候就调用鼠标监听函数,用JFrame(或JPanel)的repaint()方法重绘。
ShellC 2004-06-18
  • 打赏
  • 举报
回复
调用JFrame的repaint()方法重绘就可以了。不要调用JTabbedPane的repain()方法。
icy_csdn 2004-06-17
  • 打赏
  • 举报
回复
用paintComponent

Example:DrawTest.java


import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

public class DrawTest {
public static void main(String[] args) {
DrawFrame frame = new DrawFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.show();
}

}
/**
* A frame that contians a panel with drawings
*/
class DrawFrame extends JFrame{
public static final int DEFAULT_WIDTH = 400;
public static final int DEFAULT_HEIGHT = 400;

public DrawFrame(){
setTitle( "DrawTest" );
setSize( DEFAULT_WIDTH, DEFAULT_HEIGHT );

// add panel to frame
DrawPanel panel = new DrawPanel();
Container contentPane = getContentPane();
contentPane.add( panel );
}
}

/**
* A panel that display rectangle and ellipses.
*/
class DrawPanel extends JPanel{
public void paintComponent( Graphics g ){
super.paintComponent( g );
Graphics2D g2 = (Graphics2D)g;

// draw a rectangle
double leftX = 100;
double topY = 100;
double width = 200;
double height = 200;

Rectangle2D rect = new Rectangle2D.Double( leftX, topY, width, height );
g2.draw( rect );

// draw a enclosed ellipse
Ellipse2D ellipse = new Ellipse2D.Double();
ellipse.setFrame( rect );
g2.draw( ellipse );

// draw a diagonal line
g2.draw( new Line2D.Double( leftX, topY, leftX + width, topY + height ) );

// draw a circle with the same center
double centerX = rect.getCenterX();
double centerY = rect.getCenterY();
double radius = 150;

Ellipse2D circle = new Ellipse2D.Double();
circle.setFrameFromCenter( centerX, centerY, centerX + radius, centerY + radius );
g2.draw( circle );
}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧