我想在DrawPanel上绘制一个rect。这个rect应该是一个对象。 当我添加它时,该位置将是灰色的,但paintcomponent将不会绘制,所以我什么也看不见。
类Drawpanel.java
将添加到contentpane上,contentpane将在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
}
}
不会被绘制的对象是:
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);
}
}
当我在dia class中启动main时,我什么都没看到。为什么dia不会启动paintcomponent方法?