java怎么画出一条直线呢?高手进吧。
【题目】java(application不是applet)在JPanel上画一条直线。【要求】不用extendsJPanel然后重写其paint()的这种方法。(这点很重...
【题目】java(application不是applet)在JPanel上画一条直线。
【要求】不用extends JPanel然后重写其paint()的这种方法。(这点很重要)
【!!】我懂怎么用extends JFrame或extends JPanel 然后 重写paint()的方法,但感觉那样程序的灵活性受到限制,所以不想用那种方法,但用
JPanel canvasPanel=new JPanel();
.................................
.................................
Graphics g=canvasPanel.getGraphics();
g.drawLine(......,....,.....,.....);
这种方法总是"一片空白"................................ 展开
【要求】不用extends JPanel然后重写其paint()的这种方法。(这点很重要)
【!!】我懂怎么用extends JFrame或extends JPanel 然后 重写paint()的方法,但感觉那样程序的灵活性受到限制,所以不想用那种方法,但用
JPanel canvasPanel=new JPanel();
.................................
.................................
Graphics g=canvasPanel.getGraphics();
g.drawLine(......,....,.....,.....);
这种方法总是"一片空白"................................ 展开
3个回答
展开全部
我晕,组件要重绘,就使用paintComponent方法吧,没有什么灵活不灵活的
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class FrameDemo extends JFrame {
private JPanel contentPane;
TestComponent tc1, tc2;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FrameDemo frame = new FrameDemo();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public FrameDemo() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new GridLayout(2, 1, 10, 10));
tc1 = new TestComponent();
tc1.setBorder(BorderFactory.createLineBorder(Color.BLUE));
tc2 = new TestComponent();
tc2.setBorder(BorderFactory.createLineBorder(Color.PINK));
contentPane.add(tc1);
contentPane.add(tc2);
}
}
class TestComponent extends JComponent {
public void paintComponent(Graphics g1) {
Graphics2D g = (Graphics2D) g1;
g.setColor(Color.RED);
g.drawLine(0, 0, this.getWidth(), this.getHeight());
}
}
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class FrameDemo extends JFrame {
private JPanel contentPane;
TestComponent tc1, tc2;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FrameDemo frame = new FrameDemo();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public FrameDemo() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new GridLayout(2, 1, 10, 10));
tc1 = new TestComponent();
tc1.setBorder(BorderFactory.createLineBorder(Color.BLUE));
tc2 = new TestComponent();
tc2.setBorder(BorderFactory.createLineBorder(Color.PINK));
contentPane.add(tc1);
contentPane.add(tc2);
}
}
class TestComponent extends JComponent {
public void paintComponent(Graphics g1) {
Graphics2D g = (Graphics2D) g1;
g.setColor(Color.RED);
g.drawLine(0, 0, this.getWidth(), this.getHeight());
}
}
展开全部
重写时 repaint方法调用了paint方法的,而paint方法照样调用了上面两句:
Graphics g=this.getGraphics();
paint(g);
所以上面的:
Graphics g=canvasPanel.getGraphics();
g.drawLine(......,....,.....,.....);
这个和repaint事同样的道理。
Graphics g=this.getGraphics();
paint(g);
所以上面的:
Graphics g=canvasPanel.getGraphics();
g.drawLine(......,....,.....,.....);
这个和repaint事同样的道理。
更多追问追答
追问
那应该怎么改呢?没太明白啊。
追答
起始道理是一样的,你在application中或得到g之后,可是使用g来绘图,但是有个麻烦,就是你切换屏幕时会被擦出,所以可以在定义jpanel时定义其paint方法,但是比较麻烦而且受一定限制。
比如:
JPanel jp=new JPanel(){
public void paint( Graphics g)
{
//你的代码。
}
}
所以还是使用你给出的哪个方法:
Graphics g=canvasPanel.getGraphics();
g.drawLine(......,....,.....,.....);
其中g好比就是一个指向jpanel的面板指针,在g上面绘制就是在panel上面绘制。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用js 就行了,现在不用swing 了
更多追问追答
追问
你说的js是什么?
追答
javascript 脚本语言!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询