主要思想是-当滑块的值在增长时,更多的线在分割组件的等份,而不是横过多边形的线(就像它在图片的左上角)。我想对所有的角落都这样做,但现在我只做了其中之一。
有人能告诉我,我需要改变什么,使我的线的1/3宽吗?
我的值对1/2很好,但对1/3不是,n是滑块的一个变量。

我的代码:
import java.awt.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Mariusz extends JFrame {
private int n = 5;
private Color kolor = Color.RED;
MyComponent komponent = null;
private class MyComponent extends JComponent
{
protected void paintComponent (Graphics grafika)
{
grafika.setColor(kolor);
grafika.drawLine(getWidth() * 1/3, 0, 0, getHeight() * 1/3);
grafika.drawLine(0, getHeight() * 1/3, getWidth() * 1/3, getHeight());
grafika.drawLine(getWidth() * 1/3, getHeight(),getWidth(), getHeight() * 1/3);
grafika.drawLine(getWidth(), getHeight() * 1/3, getWidth() * 1/3, 0);
for (int i = 0; i < n ; i++)
{
if (i <= n / 3)
{
grafika.drawLine(getWidth() * i /n, 0, getWidth() * i /n, (getHeight() - getHeight() * 2/3 ) - getHeight() * i / n); //lewy gorny
grafika.drawLine( getWidth() * i / n,(getHeight() - getHeight() * 2/3 ) + getHeight() * i / n + getHeight() *1/3, getWidth() * i / n, getHeight() );
}
if (i > n / 3)
{
grafika.drawLine(getWidth() * i / n , 0, getWidth() * i /n, getHeight() * 2 * i /n / 3 - getHeight() * 1 /3 );
}
}
}
}
public Mariusz(String string)
{
super(string);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension d = kit.getScreenSize();
setBounds( d.width / 4, d.height / 4, d.width / 2, d.height / 2);
add (komponent = new MyComponent());
JPanel panel = new JPanel(new BorderLayout());
add(panel,BorderLayout.SOUTH);
final JSlider slider = new JSlider(3,40,n);
slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
// TODO Auto-generated method stub
n = slider.getValue();
komponent.repaint();
}
});
panel.add(slider);
setVisible(true);
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
EventQueue.invokeLater(new Runnable() {
public void run() {
new Mariusz("triangles");
}
});
}
}发布于 2017-01-05 02:48:19
你的方法是临时性的,我不能相信它将在其他措施中取得成功(4/5等)。
下面是一种基于类似三角形的方法:
从右上角开始向后工作:
h1/h2=(2*w/3)/(2*w/3-(1/n)*(2*w/3))因此
h2=h1/(.....)其中h1=h/3。
将其转换为代码
double f=2./3, f2=1-f;
g2.setColor(Color.blue);
for (int i=n-1; i>-1; i--)
{
grafika.drawLine(getWidth() * (n-i) / n, 0, getWidth() * (n-i)/n,
(int)((getHeight()/ 3)/(f/(f-1.*i/n))) );
}对于左上角,我们的工作方式是相同的,但使用因子f2:
g2.setColor(Color.green);
for (int i=0; i<n; i++)
{
grafika.drawLine(getWidth() * i / n , 0, getWidth() * i /n, (int)((getHeight()/ 3)/(f2/(f2-1.*i/n))) );
}左下角也是如此:
g2.setColor(Color.magenta);
for (int i=0; i<n; i++)
{
grafika.drawLine(getWidth() * i / n , getHeight()-(int)((2*getHeight()/ 3)/(f2/(f2-1.*i/n))), getWidth() * i /n, getHeight() );
}右下角
g2.setColor(Color.black);
for (int i=n-1; i>-1; i--)
{
grafika.drawLine(getWidth() * (n-i) / n , getHeight()-(int)((2*getHeight()/ 3)/(f/(f-1.*i/n))), getWidth() * (n-i) /n, getHeight() );
}例如,如果需要4/5度量,则必须将f=4./5和所有2*getHL.8()/3更改为4*getHL.8()/5(getHeight()/3 to getHeight()/5等)。
发布于 2017-01-05 04:26:35
让我提出另一种办法。而不是挣扎的数学画线从边缘,直到满足多边形,画出规则,完整,垂直的线,然后覆盖在他们之上的多边形。
首先,有三件事要做:
protected void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
Graphics2D grafika = (Graphics2D) graphics;super方法是您在paintComponent中做的第一件事,以避免工件(除非您知道自己在做什么)。Graphics对象转换为更高级的Graphics2D对象总是安全的。paintComponent()中的调用数。现在,使用这个非常简单的循环绘制线条:
for (int i = 0; i < n; i++)
grafika.drawLine(w * i / n, 0, w * i / n, h);这只是减少了你的循环。您可能需要根据您的需求修改它,但是请注意,y坐标扩展了整个组件的高度。

然后根据Polygon方法中的点创建一个drawLine:
Polygon poly = new Polygon();
poly.addPoint(w * 1 / 3, 0);
poly.addPoint(0, h * 1 / 3);
poly.addPoint(w * 1 / 3, h);
poly.addPoint(w, h * 1 / 3);最后,设置填充此形状的颜色,绘制该形状,设置用于概述该形状的颜色,然后再次绘制该形状:
grafika.setColor(getBackground());
grafika.fill(poly); // draws the inside
grafika.setColor(kolor);
grafika.draw(poly); // draws the outline

希望这不算作弊..。
另外,这条线
add(komponent = new MyComponent());在我看来是个麻烦的源头。把它分成2组。
https://stackoverflow.com/questions/41472070
复制相似问题