简体   繁体   English

为什么这个油漆组件不起作用?

[英]why doesn't this paint component work?

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;


public class Main {
public static void main(String[]args){
    @SuppressWarnings("unused")
    Gui g = new Gui();
}
}
@SuppressWarnings("serial")
class Gui extends JFrame{
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
double width = screen.getWidth();
double height = screen.getHeight();
JPanel canvas = new JPanel();
Point mloc = new Point();

JButton br = new JButton("Red");
JButton bb = new JButton("Blue");
JButton bg = new JButton("Green");
JButton wipe = new JButton("Wipe");

JLabel brushwidth = new JLabel("Width = ",JLabel.CENTER);

public JSlider s = new JSlider();

JButton image = new JButton("Image");

Point start = null;
Point current = null;
boolean entered = false;
Color c = Color.red;
public double bwidth = 3;

Gui(){
    super("PaintPot");
    setSize((int)width/4,(int)height/2);
    setVisible(true);
    setResizable(false);
    setLayout(null);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    add(s);
    s.setSize(getWidth()-20, 20);
    s.setLocation(10, 450);
    s.setBackground(Color.gray);
    s.setForeground(Color.orange);
    s.setMajorTickSpacing(50);
    s.setMinorTickSpacing(1);
    s.setValue((int)bwidth);
    s.addChangeListener(new ChangeListener(){
        @Override
        public void stateChanged(ChangeEvent e) {
            bwidth = s.getValue();
            brushwidth.setText("Width = "+s.getValue());
        }
    });
    brushwidth.setText("Width = "+s.getValue());

    add(brushwidth);
    brushwidth.setSize(70,30);
    brushwidth.setLocation(90,410);
    brushwidth.setBackground(Color.gray);
    brushwidth.setForeground(Color.orange);
    brushwidth.setOpaque(true);
    brushwidth.setVisible(true);

    add(wipe);
    wipe.setSize(70,30);
    wipe.setLocation(10, 410);
    wipe.setBackground(Color.gray);
    wipe.setForeground(Color.orange);
    wipe.setVisible(true);

    add(br);
    br.setSize(60,30);
    br.setLocation(10, 10);
    br.setBackground(Color.red);
    br.setForeground(Color.white);
    br.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent arg0) {
            c = Color.red;
        }
    });
    br.setVisible(true);

    add(bb);
    bb.setSize(60,30);
    bb.setLocation(80, 10);
    bb.setBackground(Color.blue);
    bb.setForeground(Color.white);
    bb.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) {
            c = Color.blue;
        }
    });
    bb.setVisible(true);


    add(bg);
    bg.setSize(70,30);
    bg.setLocation(150, 10);
    bg.setBackground(Color.green);
    bg.setForeground(Color.white);
    bg.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            c = Color.green;
        }
    });
    bg.setVisible(true);

    add(image);
    image.setSize(70,30);
    image.setLocation(230, 10);
    image.setBackground(Color.gray);
    image.setForeground(Color.orange);
    wipe.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){ 

        }

    });
    image.setVisible(true);
    canvas(this,canvas);

}
public void canvas(JFrame f, JPanel p){
    p.setSize(425,350);
    p.setBorder(BorderFactory.createLineBorder(Color.black, 3));
    p.setLocation(10, 50);
    p.addMouseListener(new MouseListener(){
        @Override
        public void mouseClicked(MouseEvent arg0) {}
        @Override
        public void mouseEntered(MouseEvent arg0) {entered = true;}
        @Override
        public void mouseExited(MouseEvent arg0) {entered = false;}
        @Override
        public void mousePressed(MouseEvent arg0) {}
        @Override
        public void mouseReleased(MouseEvent arg0) {}
    });
    p.addMouseMotionListener(new MouseAdapter(){
        public void mouseDragged(MouseEvent e){
            mloc = e.getLocationOnScreen();
            if(entered = true){
                paintComponent(getGraphics());
            }
        }
    });


    f.add(p);
}
public void paintComponent(Graphics g){
    g.drawOval(mloc.x, mloc.y, (int)bwidth, (int)bwidth);
}
}

I'm trying to get this app to paint inside the JPanel but I can't seem to get it to work, I want it to draw a line whenever I move my mouse. 我试图让这个应用程序在JPanel中绘制,但我似乎无法让它工作,我希望它在我移动鼠标时画一条线。 It's just the public void paint bit I can't get to grips with nothing seem to work. 这只是公共无效的油漆位,我无法掌握似乎没有任何工作。 Thanks 谢谢

JFrame does not have a paintComponent() method. JFrame没有paintComponent()方法。

Custom painting is done by overriding the paintComponent() method of a JPanel (or JComponent) and then you add the panel to the JFrame. 通过覆盖JPanel(或JComponent)的paintComponent()方法完成自定义绘制,然后将面板添加到JFrame。

Read the section from the Swing tutorial on Custom Painting for more information and examples. 阅读自定义绘画的Swing教程中的部分以获取更多信息和示例。 You will also need to override the getPreferredSize() method. 您还需要覆盖getPreferredSize()方法。

Also, don't use a null layout. 另外,不要使用null布局。 Swing was designed to be used with layout managers. Swing旨在与布局管理器一起使用。

  1. This if(entered = true){ is an assigment operator not a conditional. 这个if(entered = true){是一个分配运算符而不是条件运算符。 Instead you want if(entered == true){ 相反,你想要if(entered == true){

  2. paintComponent is meant to be overriden and not called explicitly. paintComponent意味着被覆盖而不是显式调用。 Don't explicitly call paintComponent when what you mean to do is call repaint() 当你的意思是调用repaint()时,不要显式调用paintComponent

  3. JFrame has no paintComponent method, so you aren't actually overriding any paint functionality. JFrame 没有 paintComponent方法,所以你实际上并没有覆盖任何油漆的功能。 For JFrame you should override paint , though I'd advise against it, and paint with JPanel or JComponent 对于JFrame您应该覆盖paint ,尽管我建议不要使用它,并使用JPanelJComponent

  4. In a paintComponent or paint method you should also be calling super.paintComponent or super.paint , respectively, as to not break the paint chain. paintComponentpaint方法中,您还应分别调用super.paintComponentsuper.paint ,以免破坏绘制链。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM