-2

Like in a topic, I've readed the most of all solutions for repaint() but for my code their are not working. I'm a begginer so forgive me a mess in my code..

class View.java

public class View extends JFrame{
public void make(){
    setSize(640,480);
    setVisible(true);
    setTitle("Parking");
}

double x_ = MyFrame.x;
double y_ = MyFrame.y;
double odchylenie_ = MyFrame.odchylenie;
int szerokosc = 50;
int wysokosc = 30;

   public void paint(Graphics g){
       Rectangle2D rect = new Rectangle2D.Double(-szerokosc / 2., -wysokosc / 2.,                    szerokosc, wysokosc);
       AffineTransform transform = new AffineTransform();
       transform.translate(x_, y_);
       transform.rotate(Math.toRadians(odchylenie_));
       Shape rotatedRect = transform.createTransformedShape(rect);
       Graphics2D g2 = (Graphics2D)g;
       g2.draw(rotatedRect);
}

All I want to do to re-paint this rotatedRect with new values of x,y and odchylenie (sorry for polish variable names).

I'm calculating new variable values and using Jbutton to redraw it. Here is my MyFrame.class

public class MyFrame extends javax.swing.JFrame {

static double x,y,odchylenie,kat;

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        View v = new View();
        v.make();
        Fuzzy f = new Fuzzy();
        kat = f.wyjscie(x, odchylenie);
        jLabel5.setText("Kąt: " + Double.toString(kat));
        odchylenie = Fuzzy.wyliczOdchylenie(odchylenie,kat);
        jLabel8.setText("Nowe odchylenie: " + Double.toString(odchylenie));  
        x = Fuzzy.wyliczX(x, 10, kat,odchylenie);
        jLabel6.setText("Nowy x: " + Double.toString(x));
        y = Fuzzy.wyliczY(y,x, 10, kat,odchylenie);
        jLabel7.setText("Nowy y: " + Double.toString(y));
        v.repaint(); //Not works
   }

Instead of doing repaint(), the new window appears, and old one stays... I have tried to declare View v outside the button function body, but it not worked too(nothing is painting).

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
soncrash
  • 47
  • 1
  • 8
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). 2) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) 3) *"I've read many questions.."* Link to 5 of them. You are making mistakes in the current codes and I would hope at least one of those 5 covers each of the mistakes. – Andrew Thompson May 24 '14 at 02:04

2 Answers2

2

Whenever you override one of the paint() methods, you need to make a call to the super's method:

public void paint(Graphics g) {
    super.paint(g);
    // your code goes here
}

Further, you should really be using paintComponent(), not paint().

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    // your code goes here
}
martinez314
  • 12,162
  • 5
  • 36
  • 63
  • And also i've forget to say i've tried this too... the window only changes color from white to grey... – soncrash May 23 '14 at 18:55
1

Instead of using paint, use paintComponent:

public void paintComponent(Graphics g){
       super.paintComponent(g);
       Rectangle2D rect = new Rectangle2D.Double(-szerokosc / 2., -wysokosc / 2.,                    szerokosc, wysokosc);
       AffineTransform transform = new AffineTransform();
       transform.translate(x_, y_);
       transform.rotate(Math.toRadians(odchylenie_));
       Shape rotatedRect = transform.createTransformedShape(rect);
       Graphics2D g2 = (Graphics2D)g;
       g2.draw(rotatedRect);
}
Julián Chamalé
  • 607
  • 11
  • 23