Witam,
zaczynam bawić się w Javie i taka prosta sprawa jak metoda paintComponent() mnie przeprasta. Proszę o małą pomoc :)
class etapdrugi extends JFrame implements ActionListener, MouseListener
{
Dimension roz = Toolkit.getDefaultToolkit().getScreenSize();
int w = (roz.width-1024)/2;
int h = (roz.height-768)/2;
JPanel panel = new JPanel();
JButton kl, kl1, kl2, kl3, kl4;
private String nazwa;
int x = 0, x1 = 0, x2 = 0, x3 = 0;
int y = 0, y1 = 0, y2 = 0, y3 = 0;
int current_point = 0;
boolean a = true;
public etapdrugi()
{
super("tutorial");
MatteBorder matte = new MatteBorder(2, 2, 2, 2, Color.red);
panel.setBackground(Color.BLUE);
panel.setLayout(null);
setSize(1024,768);
this.setLocation(w,h);
add(panel);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
panel.add(kl = new JButton("Hel"));
kl.setBounds(820, 384, 70, 70);
kl.setBorder(matte);
panel.add(kl1 = new JButton("Wodór"));
kl1.setBounds(890, 384, 70, 70);
kl1.setBorder(matte);
panel.add(kl2 = new JButton("Tlen"));
kl2.setBounds(820, 454, 70, 70);
kl2.setBorder(matte);
panel.add(kl3 = new JButton("Osm"));
kl3.setBounds(890, 454, 70, 70);
kl3.setBorder(matte);
panel.add(kl4 = new JButton("Połączenie"));
kl4.setBounds(820, 524, 140, 70);
kl4.setBorder(matte);
kl.addActionListener(this);
kl1.addActionListener(this);
kl2.addActionListener(this);
kl3.addActionListener(this);
kl4.addActionListener(this);
panel.addMouseListener(this);
setResizable(false);
setVisible(a);
}
@Override
public void actionPerformed(ActionEvent e){
nazwa = ((JButton) e.getSource()).getText();
}
@Override
public void mouseClicked(MouseEvent e) {
if(current_point == 0){
x = e.getXOnScreen();
y = e.getYOnScreen();
}
else if(current_point == 1){
x1 = e.getXOnScreen();
y1 = e.getYOnScreen();
}
current_point++;
System.out.println("nazwa to " +nazwa);
System.out.println("x " +x);
System.out.println("x1 " +x1);
System.out.println("cp " +current_point);
if(current_point == 2){
current_point = 0;
}
}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
protected void paintComponent(Graphics g){
super.paintComponents(g);
FontMetrics fm = g.getFontMetrics();
double textWidth = fm.getStringBounds("Hel", g).getWidth();
Graphics2D g2d = (Graphics2D) g;
switch(nazwa){
case "Hel":
g2d.setColor(Color.black);
g2d.fillOval(x - w, y - h, 80, 80);
g2d.setColor(Color.white);
g2d.drawString("Hel", (int)(x - w + textWidth/2),(int) (y - h + fm.getMaxAscent() / 2));
System.out.println(x);
System.out.println(x1);
break;
case "Wodór":
g2d.setColor(Color.black);
g2d.fillOval(x - w, y - h, 80, 80);
g2d.setColor(Color.white);
g2d.drawString("Wodór", (int)(x - w - textWidth/2),(int) (y - h + fm.getMaxAscent() / 2));
break;
case "Tlen":
g2d.setColor(Color.black);
g2d.fillOval(x - w, y - h, 80, 80);
g2d.setColor(Color.white);
g2d.drawString("Tlen", (int)(x - w - textWidth/2),(int) (y - h + fm.getMaxAscent() / 2));
break;
case "Osm":
g2d.setColor(Color.black);
g2d.fillOval(x - w, y - h, 80, 80);
g2d.setColor(Color.white);
g2d.drawString("Osm", (int)(x - w - textWidth/2),(int) (y - h + fm.getMaxAscent() / 2));
break;
//case "Połączenie":
//g2d.setColor(Color.red);
//g2d.drawLine(x - w, y - h, x1 - w, y1 - h);
//break;
}
}
}