Swing - problem z rysowaniem

0

witam mam problem z rysowaniem, mam dwie klasy: pierwsza tworzy okno, druga natomiast będzie rysować kwadraty. problem polega na tym że metoda paint nie chce się wywoływać. proszę o pomoc, proszę w tej chwili nie czepiać się części logicznej kodu, program tak jak teraz jest według mnie już powinien coś rysować, chcę wiedzieć dlaczego tego nie robi, podwójne buforowanie zaimplementowałem.
o to kod:

 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.HeadlessException;
import javax.swing.JFrame;

/**
 *
 * @author marcin
 */
public class JavaApplication1 extends JFrame{

    /**
     * @param args the command line arguments
     * program rysuje kwadrat z 9 malymi kwadratami w srodku w roznych kolorach
     * kwadrat moze zmieniac pozycje
     */
    private int widthWindow, heightWindow;
    
    public JavaApplication1() {
        widthWindow = 480;
        heightWindow = 320;
        setTitle("kolorowy kwadrat");
        setSize(new Dimension(widthWindow, heightWindow));
        setVisible(true);
        //setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }
    public static void main(String[] args) {
        // TODO code application logic here
        JavaApplication1 javaApplication1 = new JavaApplication1();
        KlasaKwadrat klasaKwadrat = new KlasaKwadrat();
        klasaKwadrat.repaint();
    }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JPanel;

/**
 *
 * @author marcin
 */
public class KlasaKwadrat extends JPanel{
    private Image dbImage;
    private Graphics dbGraphics; 
    private int widthSquare = 20;
    private int heightSquare = 20;//ustawienie wielkosci kwadratu
    private int startDrawPositionX = 30;
    private int startDrawPositionY = 30;
    static int [][]plansza = {{0, 1, 2},//rysowanie kwadratow
                              {3, 4, 5},
                              {6, 7, 8}
    };
    
    public void paint(Graphics graphics){
        dbImage = createImage(getWidth(), getHeight());
        dbGraphics = dbImage.getGraphics();
        paintComponent(dbGraphics);
        graphics.drawImage(dbImage, 0, 0, this);
    }
    
    public void paintComponent(Graphics graphics){
        for(int i = 0; i < plansza.length; ++i){
            for(int j = 0; j < plansza[0].length; ++j){
                if(plansza[i][j] == 0){
                    graphics.setColor(Color.red);
                    graphics.fillRect((widthSquare * j) + startDrawPositionX, 
                            (heightSquare * i) + startDrawPositionY, 
                            widthSquare, heightSquare);
                } else if(plansza[i][j] == 1){
                } else if(plansza[i][j] == 2){
                } else if(plansza[i][j] == 3){
                } else if(plansza[i][j] == 4){
                } else if(plansza[i][j] == 5){
                } else if(plansza[i][j] == 6){
                } else if(plansza[i][j] == 7){
                } else if(plansza[i][j] == 8){
                }
            }
        }
        repaint();
    }
    
}
0
  1. repaint() wywołane z paintComponent() prowadzi do nieskończonej pętli.
  2. Może byś tak wyświetlił utworzony panel?
        JavaApplication1 javaApplication1 = new JavaApplication1();
        KlasaKwadrat klasaKwadrat = new KlasaKwadrat();
	javaApplication1.add(klasaKwadrat);
  1. Nazwy klas masz genialne.
0

dziękuje za pomoc, nie pomyślałem, myślałem że metoda paint po utworzeniu nowego obiektu automatycznie się uruchomi

0

program rysuje 9 małych kwadratów które tworzą jeden wielki kwadrat i jeden z kwadratów ustawiony jako domyślny może zamieniać się miejscami z innymi kwadratami, każdy kwadrat jest w innym kolorze, program działa dobrze ale gdy przekroczymy tablice wyrzuca wyjątek, rozwiązaniem byłoby pewnie stworzyć jeszcze wokół istniejących kwadratów puste pola tak aby program wiedział że ma nie przekraczać poza tablice, wstawiam kod i proszę o sugestie jak może inaczej to rozwiązać jak i uwagi dotyczące kodu, pozdrawiam:

 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.HeadlessException;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author marcin
 */
public class JavaApplication1 extends JFrame{

    /**
     * @param args the command line arguments
     * program rysuje kwadrat z 9 malymi kwadratami w srodku w roznych kolorach
     * kwadrat moze zmieniac pozycje
     */
    private int widthWindow, heightWindow;
    JPanel jPanel = new JPanel();
   
    public JavaApplication1() {
        widthWindow = 480;
        heightWindow = 320;
        setTitle("kolorowy kwadrat");
        setSize(new Dimension(widthWindow, heightWindow));
        setVisible(true);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }
    public static void main(String[] args) {
        // TODO code application logic here
        JavaApplication1 javaApplication1 = new JavaApplication1();
        KlasaKwadrat klasaKwadrat = new KlasaKwadrat();
        javaApplication1.add(klasaKwadrat);
    }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JPanel;

/**
 *
 * @author marcin
 */
public class KlasaKwadrat extends JPanel{
    private KlasaRuch klasaRuch = new KlasaRuch(this);
    private Image dbImage;
    private Graphics dbGraphics;
    private int widthSquare = 80;//ustawienie wielkosci kwadratu
    private int heightSquare = 80;//ustawienie wielkosci kwadratu
    private int startDrawPositionX = 120;//poczatek rysowania
    private int startDrawPositionY = 30;//poczatek rysowania
    static int [][]plansza = {{0, 1, 2},//rysowanie kwadratow
                              {3, 4, 5},
                              {6, 7, 8}
    };

    public KlasaKwadrat() {
        this.setFocusable(true);
        addKeyListener(klasaRuch);
    }
   
    public void paint(Graphics graphics){
        dbImage = createImage(getWidth(), getHeight());
        dbGraphics = dbImage.getGraphics();
        paintComponent(dbGraphics);
        graphics.drawImage(dbImage, 0, 0, this);
    }
   
    public void paintComponent(Graphics graphics){
        for(int i = 0; i < plansza.length; ++i){
            for(int j = 0; j < plansza[0].length; ++j){
                if(plansza[i][j] == 0){
                    graphics.setColor(Color.red);
                    graphics.fillRect((widthSquare * j) + startDrawPositionX,
                            (heightSquare * i) + startDrawPositionY,
                            widthSquare, heightSquare);
                } else if(plansza[i][j] == 1){
                    graphics.setColor(Color.BLUE);
                    graphics.fillRect((widthSquare * j) + startDrawPositionX,
                            (heightSquare * i) + startDrawPositionY,
                            widthSquare, heightSquare);
                } else if(plansza[i][j] == 2){
                    graphics.setColor(Color.CYAN);
                    graphics.fillRect((widthSquare * j) + startDrawPositionX,
                            (heightSquare * i) + startDrawPositionY,
                            widthSquare, heightSquare);
                } else if(plansza[i][j] == 3){
                    graphics.setColor(Color.DARK_GRAY);
                    graphics.fillRect((widthSquare * j) + startDrawPositionX,
                            (heightSquare * i) + startDrawPositionY,
                            widthSquare, heightSquare);
                } else if(plansza[i][j] == 4){
                    graphics.setColor(Color.GRAY);
                    graphics.fillRect((widthSquare * j) + startDrawPositionX,
                            (heightSquare * i) + startDrawPositionY,
                            widthSquare, heightSquare);
                } else if(plansza[i][j] == 5){
                    graphics.setColor(Color.GREEN);
                    graphics.fillRect((widthSquare * j) + startDrawPositionX,
                            (heightSquare * i) + startDrawPositionY,
                            widthSquare, heightSquare);
                } else if(plansza[i][j] == 6){
                    graphics.setColor(Color.LIGHT_GRAY);
                    graphics.fillRect((widthSquare * j) + startDrawPositionX,
                            (heightSquare * i) + startDrawPositionY,
                            widthSquare, heightSquare);
                } else if(plansza[i][j] == 7){
                    graphics.setColor(Color.MAGENTA);
                    graphics.fillRect((widthSquare * j) + startDrawPositionX,
                            (heightSquare * i) + startDrawPositionY,
                            widthSquare, heightSquare);
                } else if(plansza[i][j] == 8){
                    graphics.setColor(Color.ORANGE);
                    graphics.fillRect((widthSquare * j) + startDrawPositionX,
                            (heightSquare * i) + startDrawPositionY,
                            widthSquare, heightSquare);
                }
            }
        }
    }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
 *
 * @author marcin
 */
public class KlasaRuch implements KeyListener{
   
    private KlasaKwadrat klasaKwadrat;
    private int x = 0;
    private int y = 0;
    private int temp;

    public KlasaRuch(KlasaKwadrat klasaKwadrat) {
        this.klasaKwadrat = klasaKwadrat;
    }

    @Override
    public void keyTyped(KeyEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void keyPressed(KeyEvent e) {
        switch(e.getKeyCode()){
            case KeyEvent.VK_UP:
                temp = KlasaKwadrat.plansza[x][y];
                KlasaKwadrat.plansza[x][y] = KlasaKwadrat.plansza[x - 1][y];
                KlasaKwadrat.plansza[x - 1][y] = temp;
                x--;
                break;
            case KeyEvent.VK_DOWN:
                temp = KlasaKwadrat.plansza[x][y];
                KlasaKwadrat.plansza[x][y] = KlasaKwadrat.plansza[x + 1][y];
                KlasaKwadrat.plansza[x + 1][y] = temp;
                x++;
                break;
            case KeyEvent.VK_LEFT:
                temp = KlasaKwadrat.plansza[x][y];
                KlasaKwadrat.plansza[x][y] = KlasaKwadrat.plansza[x][y - 1];
                KlasaKwadrat.plansza[x][y - 1] = temp;
                y--;
                break;
            case KeyEvent.VK_RIGHT:
                temp = KlasaKwadrat.plansza[x][y];
                KlasaKwadrat.plansza[x][y] = KlasaKwadrat.plansza[x][y + 1];
                KlasaKwadrat.plansza[x][y + 1] = temp;
                y++;
                break;
        }
        klasaKwadrat.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {
    }
   
}
0

wstawiam poprawiony kod, program już nie wyrzuca wyjątku, może się komuś przyda do ćwiczeń ten program

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author marcin
 */
public class JavaApplication1 extends JFrame{

    /**
     * @param args the command line arguments
     * program rysuje kwadrat z 9 malymi kwadratami w srodku w roznych kolorach
     * kwadrat moze zmieniac pozycje
     */
    private int widthWindow, heightWindow;
    JPanel jPanel = new JPanel();
    
    public JavaApplication1() {
        widthWindow = 640;
        heightWindow = 480;
        setTitle("kolorowy kwadrat");
        setSize(new Dimension(widthWindow, heightWindow));
        setVisible(true);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }
    public static void main(String[] args) {
        // TODO code application logic here
        JavaApplication1 javaApplication1 = new JavaApplication1();
        KlasaKwadrat klasaKwadrat = new KlasaKwadrat();
        javaApplication1.add(klasaKwadrat);
    }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JPanel;

/**
 *
 * @author marcin
 */
public class KlasaKwadrat extends JPanel{
    private KlasaRuch klasaRuch = new KlasaRuch(this);
    private Image dbImage;
    private Graphics dbGraphics; 
    private int widthSquare = 80;//ustawienie wielkosci kwadratu
    private int heightSquare = 80;//ustawienie wielkosci kwadratu
    private int startDrawPositionX = 120;//poczatek rysowania
    private int startDrawPositionY = 30;//poczatek rysowania
    static int [][]plansza = {{9, 9, 9, 9, 9},
                              {9, 0, 1, 2, 9},//rysowanie kwadratow
                              {9, 3, 4, 5, 9},
                              {9, 6, 7, 8, 9},
                              {9, 9, 9, 9, 9}
    };

    public KlasaKwadrat() {
        this.setFocusable(true);
        addKeyListener(klasaRuch);
    }
    
    public void paint(Graphics graphics){
        dbImage = createImage(getWidth(), getHeight());
        dbGraphics = dbImage.getGraphics();
        paintComponent(dbGraphics);
        graphics.drawImage(dbImage, 0, 0, this);
    }
    
    public void paintComponent(Graphics graphics){
        for(int i = 0; i < plansza.length; ++i){
            for(int j = 0; j < plansza[0].length; ++j){
                if(plansza[i][j] == 0){
                    graphics.setColor(Color.red);
                    graphics.fillRect((widthSquare * j) + startDrawPositionX, 
                            (heightSquare * i) + startDrawPositionY, 
                            widthSquare, heightSquare);
                } else if(plansza[i][j] == 1){
                    graphics.setColor(Color.BLUE);
                    graphics.fillRect((widthSquare * j) + startDrawPositionX, 
                            (heightSquare * i) + startDrawPositionY, 
                            widthSquare, heightSquare);
                } else if(plansza[i][j] == 2){
                    graphics.setColor(Color.CYAN);
                    graphics.fillRect((widthSquare * j) + startDrawPositionX, 
                            (heightSquare * i) + startDrawPositionY, 
                            widthSquare, heightSquare);
                } else if(plansza[i][j] == 3){
                    graphics.setColor(Color.DARK_GRAY);
                    graphics.fillRect((widthSquare * j) + startDrawPositionX, 
                            (heightSquare * i) + startDrawPositionY, 
                            widthSquare, heightSquare);
                } else if(plansza[i][j] == 4){
                    graphics.setColor(Color.GRAY);
                    graphics.fillRect((widthSquare * j) + startDrawPositionX, 
                            (heightSquare * i) + startDrawPositionY, 
                            widthSquare, heightSquare);
                } else if(plansza[i][j] == 5){
                    graphics.setColor(Color.GREEN);
                    graphics.fillRect((widthSquare * j) + startDrawPositionX, 
                            (heightSquare * i) + startDrawPositionY, 
                            widthSquare, heightSquare);
                } else if(plansza[i][j] == 6){
                    graphics.setColor(Color.LIGHT_GRAY);
                    graphics.fillRect((widthSquare * j) + startDrawPositionX, 
                            (heightSquare * i) + startDrawPositionY, 
                            widthSquare, heightSquare);
                } else if(plansza[i][j] == 7){
                    graphics.setColor(Color.MAGENTA);
                    graphics.fillRect((widthSquare * j) + startDrawPositionX, 
                            (heightSquare * i) + startDrawPositionY, 
                            widthSquare, heightSquare);
                } else if(plansza[i][j] == 8){
                    graphics.setColor(Color.ORANGE);
                    graphics.fillRect((widthSquare * j) + startDrawPositionX, 
                            (heightSquare * i) + startDrawPositionY, 
                            widthSquare, heightSquare);
                }
            }
        }
    }
}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
 *
 * @author marcin
 */
public class KlasaRuch implements KeyListener{
    
    private KlasaKwadrat klasaKwadrat;
    private int x = 1;
    private int y = 1;
    private int temp;

    public KlasaRuch(KlasaKwadrat klasaKwadrat) {
        this.klasaKwadrat = klasaKwadrat;
    }

    @Override
    public void keyTyped(KeyEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void keyPressed(KeyEvent e) {
        switch(e.getKeyCode()){
            case KeyEvent.VK_UP:
                if(KlasaKwadrat.plansza[x - 1][y] != 9){
                    temp = KlasaKwadrat.plansza[x][y];
                    KlasaKwadrat.plansza[x][y] = KlasaKwadrat.plansza[x - 1][y];
                    KlasaKwadrat.plansza[x - 1][y] = temp;
                    x--;
                }
                break;
            case KeyEvent.VK_DOWN:
                if(KlasaKwadrat.plansza[x + 1][y] != 9){
                    temp = KlasaKwadrat.plansza[x][y];
                    KlasaKwadrat.plansza[x][y] = KlasaKwadrat.plansza[x + 1][y];
                    KlasaKwadrat.plansza[x + 1][y] = temp;
                    x++;
                }
                break;
            case KeyEvent.VK_LEFT:
                if(KlasaKwadrat.plansza[x][y - 1] != 9){
                    temp = KlasaKwadrat.plansza[x][y];
                    KlasaKwadrat.plansza[x][y] = KlasaKwadrat.plansza[x][y - 1];
                    KlasaKwadrat.plansza[x][y - 1] = temp;
                    y--;
                }
                break;
            case KeyEvent.VK_RIGHT:
                if(KlasaKwadrat.plansza[x][y + 1] != 9){
                    temp = KlasaKwadrat.plansza[x][y];
                    KlasaKwadrat.plansza[x][y] = KlasaKwadrat.plansza[x][y + 1];
                    KlasaKwadrat.plansza[x][y + 1] = temp;
                    y++;
                }
                break;
        }
        klasaKwadrat.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {
    }
    
} 

1 użytkowników online, w tym zalogowanych: 0, gości: 1