用Java绘制程序。 JPanel刷新很糟糕

时间:2014-06-07 11:19:08

标签: java swing graphics jpanel paint

大家好,我对程序的问题很少: 首先,我制作了2个JPanel,并将它们添加到带有BorderLayout Center和East的主窗口。 Error 1

程序看起来像那样,但是当我开始画一些东西时,我的菜单出现在中心边框布局上,不知道为什么这么说。

Error 2

另外,当我最小化或更改窗口大小时,图形消失:/。

Error 3

第三件事我只能绘制一条折线(“Łamana”)而我又不知道如何画一条简单的线条(“Prosta”)。 Atm Prosta和Łamana一样。

另外,有时它会吸引我几个形状(“Koło”) - 例如圆圈。

我的代码:

主要

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);

}

}

绘图窗口面板

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);
        }
    }

}

}

会喜欢任何提示。

1 个答案:

答案 0 :(得分:3)

当你通过不调用super.paint()方法中的paint来打破画颜链时会发生这种情况。您所看到的是未清除油漆区域遗留下来的奇怪油漆工件(通过调用super.paint()完成)

而不是覆盖paint(),而应覆盖paintComponent

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

注意:当您执行调用super.paint[Component]时,也会清除上一行绘制的行,这可能不是您想要的功能。你有几个选择:

  • 您可以创建一个List Point2D个对象,向列表中添加点,然后通过paintComponent方法中的列表进行迭代,以便从一个点到另一个点绘制线条。 / p>

  • 您可以将每一行绘制到BufferedImage并使用paintComponent方法绘制图像。


也不要将字符串与==进行比较。使用eqauls()。 Google How do I compare Strings in Java


为了将来参考,本网站不是服务台,您不应期望用户/回答者帮助您调试一堆不同的问题。您应该就一个特定问题提出一个具体问题。如果您有多个问题,请将问题分解为不同的帖子。