0

Hello guys I have few questions regarding program: First of all I ve made 2 JPanels and added them to main Window with BorderLayout Center and East. Error 1

Program looks like that but when I start to draw something my Menu appears on Center Border Layout dont know to be honest why.

Error 2

Additionaly when I minimalize or change the Window size the graphic dissapears: /.

Error 3

Third thing I can only draw a polygonal line ("Łamana") and again I dont know how to make it draw a simple line ("Prosta"). Atm Prosta is same as Łamana.

Additionaly sometimes it draws me few shapes ("Koło") - circle for example.

My code:

Main

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class edytor extends JFrame {

/**
 * 
 */
private static final long serialVersionUID = 1L;

public static String jaki, co = null;
public static int jak, jak1;

final static float dash1[] = { 10.0f };
final static BasicStroke dashed = new BasicStroke(1.0f,
        BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f);

public edytor() throws HeadlessException {
    super("PAINT");
    setLayout(new BorderLayout());
    setSize(840, 702);
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    jak = 10;
    co = "Łamana";
    jaki = "Czarny";

    JButton zapis = new JButton("Zapis");

    zapis.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

        }
    });

    JPanel prawyPanel = new JPanel(new GridLayout(5, 0));
    JPanel lewyPanel = new rysowanie();
    lewyPanel.setVisible(true);
    prawyPanel.setVisible(true);

    final JComboBox<String> coRysujemy = new JComboBox<String>();
    coRysujemy.setEditable(true);
    coRysujemy.addItem("Łamana");
    coRysujemy.addItem("Kwadrat");
    coRysujemy.addItem("Koło");
    coRysujemy.addItem("Prosta");

    prawyPanel.add(coRysujemy);

    coRysujemy.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            co = (String) coRysujemy.getSelectedItem();
        }
    });

    final JComboBox<String> jakiKolor = new JComboBox<String>();
    jakiKolor.setEditable(true);
    jakiKolor.addItem("Czarny");
    jakiKolor.addItem("Niebieski");
    jakiKolor.addItem("Zielony");
    prawyPanel.add(jakiKolor);

    jakiKolor.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            jaki = (String) jakiKolor.getSelectedItem();
        }
    });

    final JComboBox<String> jakGrube = new JComboBox<String>();
    jakGrube.setEditable(true);
    jakGrube.addItem("10");
    jakGrube.addItem("20");
    jakGrube.addItem("30");
    prawyPanel.add(jakGrube);

    jakGrube.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            jak = jakGrube.getSelectedIndex();
        }
    });

    final JComboBox<String> jakRysowane = new JComboBox<String>();
    jakRysowane.setEditable(true);
    jakRysowane.addItem("Nie Przerywane");
    jakRysowane.addItem("Przerywane");
    prawyPanel.add(jakRysowane);
    prawyPanel.add(zapis);
    lewyPanel.setBackground(Color.WHITE);
    add(lewyPanel, BorderLayout.CENTER);
    add(prawyPanel, BorderLayout.EAST);

    jakRysowane.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            jak1 = jakRysowane.getSelectedIndex();
        }
    });

}

public static void main(String[] args) {
    edytor ramka = new edytor();
    ramka.setVisible(true);

}

}

Drawing Window Panel

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;

public class rysowanie extends JPanel {

/**
 * 
 */
private static final long serialVersionUID = 636480153152364773L;
private int squareX;
private int squareY;
private int squareW;
private int squareH;
private int xstart, ystart, xend, yend;

public rysowanie() {
    // TODO Auto-generated constructor stub

}

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    if (edytor.jak == 1) {
        edytor.jak = 10;
    }
    if (edytor.jak == 2) {
        edytor.jak = 20;
    }

    if (edytor.jak == 3) {
        edytor.jak = 30;
    }

    addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            squareX = e.getX();
            squareY = e.getY();
            repaint();

        }

        public void mouseReleased(MouseEvent e) {
            if (e.getX() > squareX)
                squareW = e.getX() - squareX;
            else {
                squareW = squareX - e.getX();
                squareX = e.getX();
            }
            if (e.getY() > squareY)
                squareH = e.getY() - squareY;
            else {
                squareH = squareY - e.getY();
                squareY = e.getY();
            }
            repaint();
        }

    });

    addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            xstart = e.getX();
            ystart = e.getY();
            repaint();

        }

        public void mouseReleased(MouseEvent e) {
            xend = e.getX();
            yend = e.getY();
            repaint();

        }

    });

    if (edytor.co == "Kwadrat") {
        if (edytor.jaki == "Niebieski") {
            g.setColor(Color.BLUE);
            g2.setStroke(new BasicStroke(edytor.jak));
            if (edytor.jak1 == 1) {
                g2.setStroke(edytor.dashed);
            }
            g.drawRect(squareX, squareY, squareW, squareH);
        }
        if (edytor.co == "Czarny") {
            g.setColor(Color.BLACK);
            g2.setStroke(new BasicStroke(edytor.jak));
            if (edytor.jak1 == 1) {
                g2.setStroke(edytor.dashed);
            }
            g.drawRect(squareX, squareY, squareW, squareH);
        }
        if (edytor.jaki == "Zielony") {
            g.setColor(Color.GREEN);
            g2.setStroke(new BasicStroke(edytor.jak));
            if (edytor.jak1 == 1) {
                g2.setStroke(edytor.dashed);
            }
            g.drawRect(squareX, squareY, squareW, squareH);
        }
    }

    if (edytor.co == "Łamana") {
        if (edytor.jaki == "Niebieski") {
            g.setColor(Color.BLUE);
            g2.setStroke(new BasicStroke(edytor.jak));
            if (edytor.jak1 == 1) {
                g2.setStroke(edytor.dashed);
            }
            g2.drawLine(xstart, ystart, xend, yend);
        }
        if (edytor.jaki == "Czarny") {
            g.setColor(Color.BLACK);
            g2.setStroke(new BasicStroke(edytor.jak));
            if (edytor.jak1 == 1) {
                g2.setStroke(edytor.dashed);
            }
            g.drawLine(xstart, ystart, xend, yend);
        }
        if (edytor.jaki == "Zielony") {
            g.setColor(Color.GREEN);
            g2.setStroke(new BasicStroke(edytor.jak));
            if (edytor.jak1 == 1) {
                g2.setStroke(edytor.dashed);
            }
            g.drawLine(xstart, ystart, xend, yend);
        }
    }

    if (edytor.co == "Koło") {
        if (edytor.jaki == "Niebieski") {
            g.setColor(Color.BLUE);
            g2.setStroke(new BasicStroke(edytor.jak));
            if (edytor.jak1 == 1) {
                g2.setStroke(edytor.dashed);
            }
            g.drawOval(squareX, squareY, squareW, squareH);
        }
        if (edytor.jaki == "Czarny") {
            g.setColor(Color.BLACK);
            g2.setStroke(new BasicStroke(edytor.jak));
            if (edytor.jak1 == 1) {
                g2.setStroke(edytor.dashed);
            }
            g.drawOval(squareX, squareY, squareW, squareH);
        }
        if (edytor.jaki == "Zielony") {
            g2.setColor(Color.GREEN);
            g2.setStroke(new BasicStroke(edytor.jak));
            if (edytor.jak1 == 1) {
                g2.setStroke(edytor.dashed);
            }
            g2.drawOval(squareX, squareY, squareW, squareH);
        }
    }

    if (edytor.co == "Prosta") {

        if (edytor.jaki == "Niebieski") {
            g.setColor(Color.BLUE);
            g2.setStroke(new BasicStroke(edytor.jak));
            if (edytor.jak1 == 1) {
                g2.setStroke(edytor.dashed);
            }
            g2.drawLine(xstart, ystart, xend, yend);
        }
        if (edytor.jaki == "Czarny") {
            g.setColor(Color.BLACK);
            g2.setStroke(new BasicStroke(edytor.jak));
            if (edytor.jak1 == 1) {
                g2.setStroke(edytor.dashed);
            }
            g.drawLine(xstart, ystart, xend, yend);
        }
        if (edytor.jaki == "Zielony") {
            g.setColor(Color.GREEN);
            g2.setStroke(new BasicStroke(edytor.jak));
            if (edytor.jak1 == 1) {
                g2.setStroke(edytor.dashed);
            }
            g.drawLine(xstart, ystart, xend, yend);
        }
    }

}

}

Would love any hints.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
FilOle
  • 53
  • 1
  • 7
  • possible duplicate of [JPanel repaint issue](http://stackoverflow.com/questions/7213178/jpanel-repaint-issue) – trashgod Jun 07 '14 at 12:25

1 Answers1

3

This happens when you break the paint chain by not calling super.paint() in the paint method. What you are seeing are the weird paint artifacts left over from not clearing the paint area (done by calling super.paint())

Rather than overriding paint(), you should instead be override paintComponent.

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
}

Note: When you do call super.paint[Component], that will also clear the previous line drawn, which is probably not the functionality you want. You have a few options around this:

  • You could create a List of Point2D object, add points to the list and iterate though the list in the paintComponent method to draw lines from point to point.

  • You could draw each line to a BufferedImage and draw the image in the paintComponent method.


Also don't compare strings with ==. Use eqauls(). Google How do I compare Strings in Java


For future reference, this site is not a help desk and you should not expect users/answerers to help you debug a bunch of different problems. You should ask one specific question regarding one specific problem. If you have multiple problems, then break the question in to different posts.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720