Java 试图解决一个比例问题
在我的代码中,我生成了介于0和60之间的随机整数,并在此基础上画了线 我只想让我的线符合纵坐标垂直线,而不接触我的随机整数。。。我想这是一道数学题,但我真的被困在这里了 以下是我的代码: Windows.java:Java 试图解决一个比例问题,java,swing,math,draw,lines,Java,Swing,Math,Draw,Lines,在我的代码中,我生成了介于0和60之间的随机整数,并在此基础上画了线 我只想让我的线符合纵坐标垂直线,而不接触我的随机整数。。。我想这是一道数学题,但我真的被困在这里了 以下是我的代码: Windows.java: public class Window extends JFrame{ Panel pan = new Panel(); JPanel container, north,south, west; public JButton ip,print,cancel,start,ok; J
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();
}
}
公共类面板扩展JPanel实现ActionListener{
国际兰特;
int lastrand=0;
ArrayList randL=新的ArrayList();
ArrayList tL=新的ArrayList();
int lastT=0;
红色=新颜色(255,0,0);
绿色=新颜色(0200,0);
蓝色=新颜色(0,0200);
黄色=新颜色(200200,0);
int max=0;
int min=0;
int i,k,inc=0,j;
莫延德格国际总计;
公共事务委员会()
{
超级();
}
公共组件(图形g)
{
超级组件(g);
图形2d g2=(图形2d)g;
g2.setRenderingHint(RenderingHits.KEY_ANTIALIAS,RenderingHits.VALUE_ANTIALIAS_ON);
g2.设定行程(新基本行程(1.8f));
g2.抽绳(20,20,20,this.getHeight()-50);
g2.绘图线(20,this.getHeight()-50,this.getWidth()-50,this.getHeight()-50);
g2.抽绳(20,20,15,35);
g2.抽绳(20,20,25,35);
g2.绘图线(this.getWidth()-50,this.getHeight()-50,this.getWidth()-65,this.getHeight()-45);
g2.绘图线(this.getWidth()-50,this.getHeight()-50,this.getWidth()-65,this.getHeight()-55);
g、 抽绳(“10”,0,this.getHeight()-85);
g、 抽绳(“20”,0,this.getHeight()-125);
g、 抽绳(“30”,0,this.getHeight()-165);
g、 抽绳(“40”,0,this.getHeight()-205);
g、 抽绳(“50”,0,this.getHeight()-245);
g2.抽绳(“最大:”,20,this.getHeight()-20);
g2.drawString(Integer.toString(max),80,this.getHeight()-20);
g2.抽绳(“最小:”,140,this.getHeight()-20);
g2.drawString(Integer.toString(min),200,this.getHeight()-20);
g2.抽绳(“平均:”,260,this.getHeight()-20);
g2.drawString(Integer.toString(moyenne),320,this.getHeight()-20);
g2.设置颜色(红色);
g2.抽绳(500,this.getHeight()-25540,this.getHeight()-25);
g2.setColor(新颜色(0,0,0));
g2.抽绳(“:max”,560,this.getHeight()-20);
g2.设置颜色(蓝色);
g2.抽绳(640,this.getHeight()-25680,this.getHeight()-25);
g2.setColor(新颜色(0,0,0));
g2.抽绳(“:Minimum”,700,this.getHeight()-20);
g2.设置颜色(绿色);
g2.抽绳(780,this.getHeight()-25820,this.getHeight()-25);
g2.setColor(新颜色(0,0,0));
g2.拉丝(“:Average”,840,this.getHeight()-20);
如果(!randL.isEmpty()){
g2.设置颜色(红色);
g2.绘图线(15,this.getHeight()-50max,this.getWidth()-50,this.getHeight()-50max);
g2.设置颜色(蓝色);
g2.绘图线(15,this.getHeight()-50分钟,this.getWidth()-50,this.getHeight()-50分钟);
g2.设置颜色(绿色);
g2.绘制线(15,this.getHeight()-50moyenne,this.getWidth()-50,this.getHeight()-50moyenne);
}
对于(i=0;i您需要停止使用绝对值/魔法值,并开始使用组件的实际值(宽度/高度)
基本问题是一个简单的计算,即当前值除以最大值,再乘以允许区域的可用宽度
int length = (value / max) * width;
value/max
生成一个0-1的百分比值,然后使用该值计算它要使用的区域可用宽度的百分比
下面的示例在可用的可视区域上放置了一个约束(或边距
),这意味着所有线都需要在该区域内绘制,而不是使用组件的整个可视区域
导入java.awt.Dimension;
导入java.awt.EventQueue;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.UIManager;
导入javax.swing.UnsupportedLookAndFeelException;
公共级抽绳{
公共静态void main(字符串[]args){
新抽绳();
}
公共抽绳(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
例如printStackTrace();
}
JFrame=新JFrame(“测试”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(newtestpane());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共类TestPane扩展了JPanel{
公共测试窗格(){
}
@凌驾
公共维度getPreferredSize(){
返回新维度(200200);
}
受保护组件(图形g){
超级组件(g);
Graphics2D g2d=(Graphics2D)g.create();
整数裕度=20;
int width=getWidth()-(边距*2);
int height=getHeight()-(边距*2);
int x=边际;
对于(int-index=0;index<4;index++){
int y=边距+(int)((索引/3d)*高度);
int长度=(int)((索引+1)/4d)*宽度);
g2d.抽绳(x,y,x+长度,y);
}
g2d.dispose();
}
}
}
抱歉,但我不清楚:您观察到的GUI与您想要的GUI有何不同?您可以发布一张您的目标图像吗?因此,您不希望图形线使轴线变硬?那么您的图形线需要某种偏移/插入来处理它很简单,在第二个要点中,最大值是58,但直线与之相关的是o
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();
}
}
}