试图在这里解决比例问题

时间:2015-12-08 22:58:17

标签: java swing math draw lines

在我的代码中,我生成0到60之间的randoms整数,我根据这些绘制线条。

我只是希望我的线条适合纵坐标垂直线而不触及我的随机整数......我猜这是一个数学问题,但我真的卡在这里!

这是我的代码:

Windows.java:

public class Window extends JFrame{


Panel pan = new Panel();
JPanel container, north,south, west;
public JButton ip,print,cancel,start,ok;
JTextArea timeStep;
JLabel legend;
double time=0;
double temperature=0.0;
Timer chrono;


public static void main(String[] args) {
    new Window();
}

public Window()
{
    System.out.println("je suis là");
    this.setSize(1000,400);
    this.setLocationRelativeTo(null);
    this.setResizable(false);
    this.setTitle("Assignment2 - CPU temperature");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    container = new JPanel(new BorderLayout());


    north = new JPanel();
    north.setLayout(new BorderLayout());
    ip = new JButton ("New");
    north.add(ip, BorderLayout.WEST);
    print = new JButton ("Print");
    north.add(print,BorderLayout.EAST);

    JPanel centerPanel = new JPanel();

    centerPanel.add(new JLabel("Time Step (in s): "));
    timeStep = new JTextArea("0.1",1,5);
    centerPanel.add(timeStep);
    start = new JButton("OK");
    ListenForButton lForButton = new ListenForButton();
    start.addActionListener(lForButton);
    ip.addActionListener(lForButton);
    print.addActionListener(lForButton);
    centerPanel.add(start);

    north.add(centerPanel, BorderLayout.CENTER);



    west = new JPanel();
    JLabel temp = new JLabel("°C");
    west.add(temp);

    container.add(north, BorderLayout.NORTH);
    container.add(west,BorderLayout.WEST);
    container.add(pan, BorderLayout.CENTER);

    this.setContentPane(container);
    this.setVisible(true);
}



private class ListenForButton implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource()==start)
    {

        time=Double.parseDouble(timeStep.getText());
        System.out.println(time);
        chrono = new Timer((int)(1000*time),pan);
        chrono.start();

    }
    if(e.getSource()==ip)
    {
        JPanel options = new JPanel();
        JLabel address = new JLabel("IP Address:");
        JTextField address_t = new JTextField(15);
        JLabel port = new JLabel("Port:");
        JTextField port_t = new JTextField(5);
        options.add(address);
        options.add(address_t);
        options.add(port);
        options.add(port_t);
         int result = JOptionPane.showConfirmDialog(null, options, "Please Enter an IP Address and the port wanted", JOptionPane.OK_CANCEL_OPTION);
         if(result==JOptionPane.OK_OPTION)
         {
             System.out.println(address_t.getText());
             System.out.println(port_t.getText());
         }
    }
    if(e.getSource()==print)
    {
        chrono.stop();
    }

}
}
}

Panel.java:

public class Panel extends JPanel implements ActionListener {


int rand;
int lastrand=0;
ArrayList<Integer> randL = new ArrayList<>();
ArrayList<Integer> tL = new ArrayList<>();
int lastT = 0;
Color red = new Color(255,0,0);
Color green = new Color(0,200,0);
Color blue = new Color (0,0,200);
Color yellow = new Color (200,200,0);
int max=0;
int min=0;
int i,k,inc = 0,j;
int total,degr,moyenne;

public Panel()
{
    super();
}

public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

    g2.setStroke(new BasicStroke(1.8f));


    g2.drawLine(20, 20, 20, this.getHeight()-50);
    g2.drawLine(20, this.getHeight()-50, this.getWidth()-50, this.getHeight()-50);
    g2.drawLine(20, 20, 15, 35);
    g2.drawLine(20, 20, 25, 35);
    g2.drawLine(this.getWidth()-50, this.getHeight()-50, this.getWidth()-65, this.getHeight()-45);
    g2.drawLine(this.getWidth()-50, this.getHeight()-50, this.getWidth()-65, this.getHeight()-55);
    g.drawString("10", 0, this.getHeight()-85);
    g.drawString("20", 0, this.getHeight()-125);
    g.drawString("30", 0, this.getHeight()-165);
    g.drawString("40", 0, this.getHeight()-205);
    g.drawString("50", 0, this.getHeight()-245);
    g2.drawString("Maximum: ", 20, this.getHeight()-20);
    g2.drawString(Integer.toString(max), 80, this.getHeight()-20);
    g2.drawString("Minimum: ", 140, this.getHeight()-20);
    g2.drawString(Integer.toString(min), 200, this.getHeight()-20);
    g2.drawString("Average: ", 260, this.getHeight()-20);
    g2.drawString(Integer.toString(moyenne), 320, this.getHeight()-20);
    g2.setColor(red);
    g2.drawLine(500, this.getHeight()-25, 540, this.getHeight()-25);
    g2.setColor(new Color(0,0,0));
    g2.drawString(":  Maximum", 560, this.getHeight()-20);
    g2.setColor(blue);
    g2.drawLine(640, this.getHeight()-25, 680, this.getHeight()-25);
    g2.setColor(new Color(0,0,0));
    g2.drawString(":  Minimum", 700, this.getHeight()-20);
    g2.setColor(green);
    g2.drawLine(780, this.getHeight()-25, 820, this.getHeight()-25);
    g2.setColor(new Color(0,0,0));
    g2.drawString(":  Average", 840, this.getHeight()-20);


    if(!randL.isEmpty()){
        g2.setColor(red);
        g2.drawLine(15, this.getHeight()-50-max, this.getWidth()-50,this.getHeight()-50-max);

        g2.setColor(blue);
        g2.drawLine(15, this.getHeight()-50-min, this.getWidth()-50,this.getHeight()-50-min);

        g2.setColor(green);
        g2.drawLine(15, this.getHeight()-50-moyenne, this.getWidth()-50,this.getHeight()-50-moyenne);

    }
    for(i = 0; i<tL.size(); i++){
        int temp = randL.get(i);
        int t = tL.get(i);
        g2.setColor(new Color(0,0,0));
        g2.drawLine(20+t, this.getHeight()-50-temp, 20+t, this.getHeight()-50);
       // Ellipse2D circle = new Ellipse2D.Double();
        //circle.setFrameFromCenter(20+t, this.getHeight()-50, 20+t+2, this.getHeight()-52);

    }
    for(j=0;j<5;j++)
    {
        inc=inc+40;
        g2.setColor(new Color(0,0,0));
        g2.drawLine(18, this.getHeight()-50-inc, 22, this.getHeight()-50-inc);
    }
    inc=0;
    }


@Override
public void actionPerformed(ActionEvent e) {
    rand = (int)(Math.random() * (60));
    lastT += 80;
    randL.add(rand);
    tL.add(lastT);
    Object obj = Collections.max(randL);
    max = (int) obj;
    Object obj2 = Collections.min(randL);
    min = (int) obj2;
   if(!randL.isEmpty()) {
    degr = randL.get(k);
    total += degr;
    moyenne=total/randL.size();
     }
    k++;
    if(randL.size()>=12)
    {
        randL.removeAll(randL);
        tL.removeAll(tL);
        lastT = 0;
        k=0;
        degr=0;
        total=0;
        moyenne=0;
    }

    repaint();
}

}

这就是我给我的东西:

Starting window

Window

对不起,这真是一团糟!

有什么想法? 感谢。

1 个答案:

答案 0 :(得分:2)

您需要停止使用绝对值/魔法值,并开始使用组件的实际值(宽度/高度)。

基本问题是一个简单的计算,它将当前值除以最大值并将其乘以允许区域的可用宽度

int length = (value / max) * width;

value / max生成0-1的百分比值,然后您可以使用它来计算它想要使用的区域的可用宽度的百分比。

以下示例在可用的可视区域上放置约束(或margin),这意味着需要在该区域内绘制所有线条,而不使用组件的整个可视区域

Example

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class DrawLine {

    public static void main(String[] args) {
        new DrawLine();
    }

    public DrawLine() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            int margin = 20;

            int width = getWidth() - (margin * 2);
            int height = getHeight() - (margin * 2);

            int x = margin;
            for (int index = 0; index < 4; index++) {

                int y = margin + (int)(((index / 3d) * height));
                int length = (int)(((index + 1) / 4d) * width);

                g2d.drawLine(x, y, x + length, y);

            }

            g2d.dispose();
        }

    }

}