如何指定一个JPanel,并在上面用drawLine()画线

henryfeifei 2004-12-07 09:52:42
我在JFrame上贴了几个JPanel,现在想在某个JPanel上用drawLine()画线,请问如何来指定一个JPanel,并在上面画线呢,请高手帮忙.
...全文
662 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
henryfeifei 2004-12-09
  • 打赏
  • 举报
回复
后来自己搞定了,呵呵,还是谢谢您的回复了,帮了不少忙哦,分就给你了
henryfeifei 2004-12-07
  • 打赏
  • 举报
回复
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class painter extends JPanel
{
protected Graphics g;
protected int x1;
protected int x2;
protected int y1;
protected int y2;
protected int red;
protected int green;
protected int blue;

painter(int X1,int Y1,int X2,int Y2,int RED,int GREEN,int BLUE)
{
super(null);
x1=X1;
y1=Y1;
x2=X2;
y2=Y2;
red=RED;
green=GREEN;
blue=BLUE;
}
public void set(int X1,int Y1,int X2,int Y2,int RED,int GREEN,int BLUE)
{
x1=X1;
y1=Y1;
x2=X2;
y2=Y2;
red=RED;
green=GREEN;
blue=BLUE;

}
public void paint()
{
super.paint(g);
g.setColor(new Color(red,green,blue));
g.drawLine(x1,y1,x2,y2);
}
}
我自己写了这个painter类,但把这个类添加到CONTAINER的时候却系统报错说window不能添加到container里边,麻烦指点下是什么问题呢.我不是继承了JPanel么,怎么变成window了?
vgvg 2004-12-07
  • 打赏
  • 举报
回复
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
import java.awt.geom.Line2D;

public class Line {
public static void main(String[] args) {
LineFrame frame = new LineFrame();
frame.setDefaultCloseOperation(3);
frame.show();
}
}

class LineFrame extends JFrame{
public LineFrame(){
setTitle("Line");
setResizable(false);
setSize(300,200);
LinePanel panel = new LinePanel();
Container contentPane = getContentPane();
contentPane.add(panel);
}
}

class LinePanel extends JPanel {
Graphics2D g2;
public LinePanel(){

}
public void paintComponent(Graphics g){
super.paintComponent(g);
g2 = (Graphics2D)g;
g2.draw(line);
}

private double leftX = 100.0;
private double topY =500.0 ;
private double W = 50.0;
private double H = 50.0;
private double MovLen = 5.0;
private Line2D line =new Line2D.Double(leftX,topY,W,H);

}
如果享用鼠标划就要加上鼠标事件,然后得到位置坐标,重划JPanel.

62,615

社区成员

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

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