[英]Paint a rect on Jpanel with paintcomponent doesn't work
I want to draw a rect on a DrawPanel. 我想在DrawPanel上绘制一个矩形。 This rect should be a object. 该矩形应该是一个对象。 When I add it the place will be grey but the paintcomponent will not paint, so I see nothing. 当我添加它时,该位置将为灰色,但paintcomponent不会绘制,因此看不到任何东西。
The class Drawpanel.java
will be add on the contentpane, the contentpane is on the JFrame. Drawpanel.java
类将添加在Drawpanel.java
上, Drawpanel.java
在JFrame上。
import java.awt.event.*;
import java.awt.*;
import javax.swing.JPanel;
public class DrawPanel extends JPanel implements MouseListener{
private static final long serialVersionUID = 6725416157621860479L;
public DrawPanel() {
addMouseListener(this);
this.setLayout(null);
setPreferredSize(new Dimension(500, 500));
}
Graphics2D g2d = null;
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
super.paintComponent(g);
g2d = (Graphics2D)g;
System.out.println("paint!!!!!!!");
for (int i = 0; i < 1500; i++) {
if (i %10 == 0) {
g2d.setColor(new Color(242, 242, 242));
g2d.drawLine(0, i, 1500, i);// _
g2d.drawLine(i, 0, i, 1500);// |
}
}
g2d.setColor(Color.BLUE);
}
@Override
public void mouseClicked(MouseEvent arg0) {
if (START.modus != 4) {
return;
}
System.out.println("X_" + arg0.getX() + " | Y_"+arg0.getY());
int x = (arg0.getX()) - (arg0.getX()%10);
int y = (arg0.getY()) - (arg0.getY()%10);
System.out.println("x_" + x + " " + "y_" + y);
Dia d = new Dia(x, y);
d.setBounds(x, y, 50, 50);
add(d);
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
The object which will not be painted is: 不会被绘制的对象是:
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Dia extends JPanel implements MouseListener {
private Point startcoord;
private int sizex = 50;
private int sizey = 20;
public Dia(int x, int y) {
super();
addMouseListener(this);
this.startcoord = new Point(x, y);
setPreferredSize(new Dimension(sizex, sizey));
}
@Override
public void paintComponents(Graphics g) {
super.paintComponents(g);
System.out.println("Diaaaaa1");
Graphics2D g2d = (Graphics2D) g;
g2d.drawLine(0, 0, 50, 50);
// g.setColor(Color.RED);
g.drawRect(0, 0, sizex, sizey);
g.setColor(Color.GREEN);
g.drawString("TEST", 3, 3);
System.out.println("Diaaaaa");
}
public Point getStartcoord() {
return startcoord;
}
public void setStartcoord(Point startcoord) {
this.startcoord = startcoord;
}
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("diac");
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
Dia c = new Dia(10, 10);
//c.textsize();
JFrame a = new JFrame();
a.setVisible(true);
a.setSize(new Dimension(100, 100));
JPanel b = new JPanel();
a.add(b);
b.add(c);
}
}
When I start the main in the dia class I see nothing. 当我在dia类中开始主课程时,我什么也没看到。 Why dia will not start the paintcomponent method? 为什么dia无法启动paintcomponent方法?
Really its simple you override another method paintComponents()
but you need paintComponent()
. 确实很简单,您可以重写另一个方法paintComponents()
但是您需要paintComponent()
。 Change that and all will be work. 改变这一点,一切都会奏效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.