如何在Java中正确对paintComponent进行打包?

huangapple go评论79阅读模式
英文:

How to do right packing of paintComponent in java?

问题

package j; 
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.RenderingHints;
import java.awt.geom.Path2D;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Letter {

    LetterDraw letterDraw = new LetterDraw();

    public Letter() {
        JFrame frame = new JFrame();
        
        JPanel letterDrawWrapper = new JPanel(new GridBagLayout());
        
        letterDrawWrapper.add(letterDraw);
        letterDrawWrapper.setSize(1024,760);
        frame.add(createColorChooser(), BorderLayout.PAGE_END);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setSize(111, 111);
        frame.setVisible(true);
    }

    private JColorChooser createColorChooser() {
        JColorChooser colorChooser = new JColorChooser();
        colorChooser.getSelectionModel().addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                letterDraw.setColor(colorChooser.getColor());
            }
        });
        return colorChooser;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Letter();
            }
        });
    }
}

class LetterDraw extends JPanel {

    private Color color;

    public void setColor(Color color) {
        this.color = color;
        
        repaint();
    }

    @Override
    protected void paintComponent(Graphics graphics) {
        super.paintComponent(graphics);
        
        Graphics2D g = (Graphics2D) graphics;
        g.setColor(color);

        g.setRenderingHint(
                RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g.setStroke(new BasicStroke(3));
       
        g.setColor(color);
        g.drawLine(11,11,1024,1024);  
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(1024, 768);
    }
}
英文:

1. Need set 1024*768 the size of paintComponent Area.

2. Need set drawing orientation of line on (0, 0, 1366,1024) but not a center.

I try to change the size in getPreferredSize() method but It's doesn't help me or doing another effect. I can't do this.
I try to change the size in getPreferredSize() method but It's doesn't help me or doing another effect.
I try to change
This is code which I can't change for my needs!

This is code which I can't change for my needs!

package j; 
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.RenderingHints;
import java.awt.geom.Path2D;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Letter {
LetterDraw letterDraw = new LetterDraw();
public Letter() {
JFrame frame = new JFrame();
JPanel letterDrawWrapper = new JPanel(new GridBagLayout());
letterDrawWrapper.add(letterDraw);
letterDrawWrapper.setSize(1024,760);
frame.add(createColorChooser(), BorderLayout.PAGE_END);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setSize(111, 111);
frame.setVisible(true);
}
private JColorChooser createColorChooser() {
JColorChooser colorChooser = new JColorChooser();
colorChooser.getSelectionModel().addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
letterDraw.setColor(colorChooser.getColor());
}
});
return colorChooser;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Letter();
}
});
}
}
class LetterDraw extends JPanel {
private Color color;
public void setColor(Color color) {
this.color = color;
repaint();
}
@Override
protected void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
Graphics2D g = (Graphics2D) graphics;
g.setColor(color);
g.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setStroke(new BasicStroke(3));
//g.setStroke(new BasicStroke(4, BasicStroke.JOIN_BEVEL, 0));
g.setColor(color);
g.drawLine(11,11,1024,1024);  
}
@Override
public Dimension getPreferredSize() {
return new Dimension(150, 150);
}
}```
</details>
# 答案1
**得分**: 1
&gt; 需要将 paintComponent 区域的大小设置为 1024*768。
不应该硬编码尺寸值。你不知道分辨率会是多少。
而且,屏幕的分辨率并不等于可用于绘画的空间。窗口有标题栏和边框,这会减少用于自定义绘画的空间。
绘画代码应该是基于面板可用空间的动态的,所以绘画代码应该使用 `getWidth()` 和 `getHeight()` 等方法来确定绘画区域。
&gt; 我尝试在 getPreferredSize() 中更改大小。
是的,这样可以为首选大小提供建议。然后你只需要调用 `pack()` 方法来调整窗口大小,不要在窗口上使用 `setSize()`。
//frame.setSize(111, 111);
这个语句会覆盖 `pack()` 方法确定的大小。把它删掉。
g.drawLine(11,11,1024,1024);  
不要硬编码数值。面板的大小会随着窗口的调整而改变。例如,在面板上绘制对角线的代码应该是:
g.drawLine(0, 0, getWidth(), getHeight());  
尝试手动调整窗口大小,看看线条的大小如何改变。
编辑:
你没有将面板添加到窗口中:
frame.add(letterDraw);
<details>
<summary>英文:</summary>
&gt; Need set 1024*768 the size of paintComponent Area.
You should NOT be hard coding size values. You don&#39;t know what the resolution will be. 
Also, The resolution of your screen is NOT the space available for painting. The frame has a title bar and border which takes away space for custom painting. 
Painting code should be dynamic based on the space available to your panel, so the painting code would use methods like `getWidth()` and `getHeight()` to determine the painting area.
&gt; I try to change the size in getPreferredSize()
Yes that will work to give a suggestion for the preferred size. Then you just pack() the frame. Don&#39;t use setSize() on the frame.
//frame.setSize(111, 111);
That statement is overriding the size determined by the pack() method. Get rid of it.
g.drawLine(11,11,1024,1024);  
Don&#39;t hard code values. The size of the panel will change as the frame is resized. For example to draw a diagonal line on the panel the code should be:
g.drawLine(0, 0, getWidth(), getHeight());  
Try manually resizing the frame to see how the size of the line changes.
Edit:
You didn&#39;t add your panel to the frame:
frame.add(letterDraw);
</details>

huangapple
  • 本文由 发表于 2020年10月16日 04:11:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64379019.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定