本文介绍了如何在绘制直方图时更改矩形的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其实我是java gui的新手,在这里我想改变矩形的高度,只要我们在文本字段中输入内容并按下按钮,但我不知道该怎么做,实际上我检查了很多位置但我找不到一个简单的方法。实际上,我找不到如何更改MyPanel类中paintComponent内的矩形大小。



(逻辑高度变化是多少元音字母和小字母,以及字符串中的辅音字母和其他字符)

  import java.awt。*; 
import javax.swing。*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Hist {
int h1 = 0,h2 = 0,h3 = 0,h4 = 0,h5 = 0;
JFrame f = new JFrame();
JButton b =新JButton(click);
JTextField text = new JTextField(30);


public static void main(String args []){
Hist h = new Hist();


$ b Hist(){

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new FlowLayout());
f.add(新的MyPanel(h1,h2,h3,h4,h5));
f.setSize(400,300);
f.setVisible(true);

f.add(b);
f.add(text);

thehandler handler = new thehandler();
b.addActionListener(handler);


class thehandler implements ActionListener {
public void actionPerformed(ActionEvent event){
if(event.getSource()== b){
H1 = 0; H 2 = 0; H3 = 0; H4 = 0; H5 = 0;
String s;
s = text.getText();
char [] ar = s.toCharArray();
for(int i = 0; i< s.length(); i ++){
if(ar [i] =='a'|| ar [i] =='e'|| ar [i] =='i'|| ar [i] =='o'|| ar [i] =='u'){
h1 = h1 + 10;
}
else if(ar [i] =='A'|| ar [i] =='E'|| ar [i] =='I'|| ar [i] = ='O'|| ar [i] =='U'){
h2 = h2 + 10;
}
else {
h5 = h5 + 10;
}
}

}
}

}
}


类MyPanel扩展JPanel {
int x1,x2,x3,x4,x5;
public MyPanel(int a,int b,int c,int d,int e){
setBorder(BorderFactory.createLineBorder(Color.BLACK));
x1 = a; x2 = b; x3 = c; x4 = d; x5 = e;
}
public Dimension getPreferredSize(){
return new Dimension(350,200);
}
public void paintComponent(Graphics g){
super.paintComponents(g);
g.setColor(Color.red);
g.fillRect(25,25,30,x1);

g.setColor(Color.red);
g.fillRect(75,25,30,x2);

g.setColor(Color.red);
g.fillRect(125,25,30,x3);

g.setColor(Color.red);
g.fillRect(175,25,30,x4);

g.setColor(Color.red);
g.fillRect(225,25,30,x5);


$ b


解决方案

它看起来像你这样做只会改变高度,而不是 y 位置。如果它是一个颠倒的直方图,这将是很好的。



你需要做的是有一个y的起点,比方说一个高度为450的屏幕是400,所以水平线接近谷底

  int y = 400; 

所以当你画的时候会是

  g.fillRect(25,y  -  height,30,height)



现在身高会是y的起点,400.而当你想增加它的时候,你实际上需要减少400.



所以如果你想要身高为150,那么你需要-150

  g.fillRect(25,y  -  150,30,height); 

这将会产生一个从水平线上升的效果。



对于每一个 g.fillRect 你都应该计算一个新的高度,所以你可以有一个高度 varaible。和一个最后的INCREMENT 变量,它基于多少元音。所以像这样

  final int INCREMENT = 25; 
int height;

....

height = numberOfAs * INCREMENT;
g.fillRect(25,y - height,30,height);
height = numberOfEs * INCREMENT;
g.fillRect(75,y - height,30,height);
height = numberOfOs * INCREMENT;
g.fillRect(125,y - height,30,height);
height = numberOfIs * INCREMENT;
g.fillRect(175,y - height,30,height);
height = numberOfUs * INCREMENT;
g.fillRect(225,y - height,30,height);






运行此示例查看我在说什么about

  import java.awt.Dimension; 
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
导入javax.swing.SwingUtilities;

public class Histogram extends JPanel {

private static final int DIM_WIDTH = 300;
private static final int DIM_HEIGHT = 450;

int height;
private static final int INCREMENT = 50;
int numberOfAs = 4;
int numberOfEs = 2;
int numberOfIs = 1;
int numberOfOs = 5;
int numberOfUs = 6;
int y = 400;

保护void paintComponent(Graphics g){
super.paintComponent(g);
height = numberOfAs * INCREMENT;
g.fillRect(25,y - height,30,height);
height = numberOfEs * INCREMENT;
g.fillRect(75,y - height,30,height);
height = numberOfOs * INCREMENT;
g.fillRect(125,y - height,30,height);
height = numberOfIs * INCREMENT;
g.fillRect(175,y - height,30,height);
height = numberOfUs * INCREMENT;
g.fillRect(225,y - height,30,height);
}

public static void createAndShowGui(){
JFrame frame = new JFrame();
frame.add(新的直方图());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.pack();
frame.setVisible(true);


$ b public Dimension getPreferredSize(){
return new Dimension(DIM_WIDTH,DIM_HEIGHT);


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


从左至右加上 INCREMENT 每个字母50像素

  As = 4; 
Es = 2;
Os = 5;
Is = 1;
Us = 6;






程序,您希望能够使用文本字段条目来控制重绘,您可以在 MyPanel setters >类,它将设置 numberOfXs 变量的值。从你用来获取它们的任何方法得到它们后,只需调用 setNumberOfAs(int numberOfAs)和其他setter,并调用repaint。因此,每次从textField获取输入,并且已更新 MyPanel 中的变量时,绘制的直方图应该更新。






更新



另一种方法是将两个 ActionListener <$ c $> MyPanel 内部类 $ C>组织胺。这样他们可以共享 Hist 的全局变量。为 Hist 类创建 NumberOfXs 变量成员,以便所有内部类可以共享。在 actionPerformed ActionListener 类中,相应地更改这些变量,并且 repaint MyPanel 。如果你这样做了,你不需要传递一个参数给 MyPanel






更新2



我不想为你完成任务,


  • 让两个类的内部类都是 Hist

  • 具有 Hist numberOfXs 变量类成员,而不是 MyPanel

  • Hist中创建一个 MyPanel code>作为一个类成员,所以它可以被 ActionListener
  • 加上
  • 获取<$ c $在您的 actionPerformed 然后调用repaint



像这样的东西

  public class Hist {
MyPanel myPanel = new MyPanel();
int numberOfAs;
int numberOfEs;
int numberOfIs;
int numberOfOs;
int numberOfUs;

私有类MyPanel扩展JPanel {
//做你的绘画
}

私人类MyListener实现ActionListener {
public void actionPerformed ActionEvent e){
//获取numberOfXs的值,但是你这样做。确保numberOfXs = someValue
myPanel.repaint();
}
}
}


Actually I am new to java gui, and here I want to change the rectangle height whenever we put something in text field and press button, but I don't know how to do it, actually I checked in a ton of sited but I couldn't find an easy way.Actually I can't find how to change rectangle size that is inside paintComponent inside the MyPanel class.

(The logic for height change is how many vowels capital and small,and consonents and other things are there in string)

import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Hist{
    int h1=0,h2=0,h3=0,h4=0,h5=0;
    JFrame f=new JFrame();
    JButton b=new JButton("click");
    JTextField text=new JTextField(30);


public static void main(String args[]){
    Hist h=new Hist();

}

Hist(){

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLayout(new FlowLayout());
    f.add(new MyPanel(h1,h2,h3,h4,h5));
    f.setSize(400,300);
    f.setVisible(true);

    f.add(b);
    f.add(text);

    thehandler handler=new thehandler();
    b.addActionListener(handler);

}
class thehandler implements ActionListener{
    public void actionPerformed(ActionEvent event){
        if(event.getSource()==b){
            h1=0;h2=0;h3=0;h4=0;h5=0;
            String s;
            s=text.getText();
            char[] ar=s.toCharArray();
            for(int i=0;i<s.length();i++){
                if(ar[i]=='a'||ar[i]=='e'||ar[i]=='i'||ar[i]=='o'||ar[i]=='u'){
                    h1=h1+10;
                }
                else if(ar[i]=='A'||ar[i]=='E'||ar[i]=='I'||ar[i]=='O'||ar[i]=='U'){
                    h2=h2+10;
                }
                else{
                    h5=h5+10;
                }
            }

        }
    }

 }
}


class MyPanel extends JPanel{
    int x1,x2,x3,x4,x5;
    public MyPanel(int a,int b,int c,int d,int e){
        setBorder(BorderFactory.createLineBorder(Color.BLACK));
        x1=a;x2=b;x3=c;x4=d;x5=e;
    }
    public Dimension getPreferredSize(){
        return new Dimension(350,200);
    }
    public void paintComponent(Graphics g){
        super.paintComponents(g);
        g.setColor(Color.red);
        g.fillRect(25, 25, 30,x1);

        g.setColor(Color.red);
        g.fillRect(75, 25, 30, x2);

        g.setColor(Color.red);
        g.fillRect(125, 25, 30,x3);

        g.setColor(Color.red);
        g.fillRect(175, 25, 30, x4);

        g.setColor(Color.red);
        g.fillRect(225, 25, 30, x5);

    }
}
解决方案

It looks like the way you're doing it will only change the height and not the y location. Which would be fine if it were an upside down histogram.

What you need to do is have a starting point for y, say 400 for a screen with height 450, so the horizon is near the bottom

int y = 400;

So when you draw it will be

g.fillRect(25, y - height, 30, height)

Now height will be the y starting point, 400. And when you want to increase it, you actually need to decrease 400.

So if you want the height to be 150, then you need to -150

g.fillRect(25, y - 150, 30, height);

This will give the effect a bar rising from the horizon line

For every g.fillRect you should calculate a new height, so you can have a height varaible. and a final INCREMENT variable based on how many vowels. So something like this

final int INCREMENT = 25;
int height;

....

height = numberOfAs * INCREMENT;
g.fillRect(25, y - height, 30, height);
height = numberOfEs * INCREMENT;
g.fillRect(75, y - height, 30, height);
height = numberOfOs * INCREMENT;
g.fillRect(125, y - height, 30, height);
height = numberOfIs * INCREMENT;
g.fillRect(175, y - height, 30, height);
height = numberOfUs * INCREMENT;
g.fillRect(225, y - height, 30, height);


Run this example to see what I'm talking about

import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Histogram extends JPanel {

    private static final int DIM_WIDTH = 300;
    private static final int DIM_HEIGHT = 450;

    int height;
    private static final int INCREMENT = 50;
    int numberOfAs = 4;
    int numberOfEs = 2;
    int numberOfIs = 1;
    int numberOfOs = 5;
    int numberOfUs = 6;
    int y = 400;

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        height = numberOfAs * INCREMENT;
        g.fillRect(25, y - height, 30, height);
        height = numberOfEs * INCREMENT;
        g.fillRect(75, y - height, 30, height);
        height = numberOfOs * INCREMENT;
        g.fillRect(125, y - height, 30, height);
        height = numberOfIs * INCREMENT;
        g.fillRect(175, y - height, 30, height);
        height = numberOfUs * INCREMENT;
        g.fillRect(225, y - height, 30, height);
    }

    public static void createAndShowGui() {
        JFrame frame = new JFrame();
        frame.add(new Histogram());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.pack();
        frame.setVisible(true);

    }

    public Dimension getPreferredSize() {
        return new Dimension(DIM_WIDTH, DIM_HEIGHT);
    }

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

From left to right with an INCREMENT of 50 pixels per letter

As = 4;
Es = 2;
Os = 5;
Is = 1;
Us = 6;


Also in a program where you want to be able to control the the repaint with a text field entry, you can have setters in your MyPanel class that will set the values of the numberOfXs variables. After you get them from whatever method you use to get them, just call setNumberOfAs(int numberOfAs) and the rest of the setters, and call repaint. So for each time an input is taken from the textField, and you have updated the variabels in MyPanel, the painted histogram should update.


UPDATE

Another way you can do it is to make both the ActionListener and the MyPanel inner classes of Hist. That way can they can share the global variables of Hist. Make the NumberOfXs variable members of the Hist class so all inner classes can share. In the ActionListener class in your actionPerformed, change the variables accordingly and repaint the MyPanel. If you did it this way, you wouldn't need to pass an parameters to the constructor of MyPanel


UPDATE 2

I don't want to do your complete assignment for you, so I'll give you a basic outline.

  • Make both class inner classes of Hist
  • Have the numberOfXs variables class members of Hist, not in MyPanel
  • Create an instance of MyPanel in Hist as a class member so it can be accesed by the ActionListener
  • Get the values for the numberOfXs in your actionPerformed then call repaint

Something like this

public class Hist {
    MyPanel myPanel = new MyPanel();
    int numberOfAs;
    int numberOfEs;
    int numberOfIs;
    int numberOfOs;
    int numberOfUs;

    private class MyPanel extends JPanel {
        // do your painting
    }

    private class MyListener implements ActionListener {
        public void actionPerformed(ActionEvent e){
            // get the values for numberOfXs however you do it. Make sure to numberOfXs = someValue
            myPanel.repaint();
        }
    }
}

这篇关于如何在绘制直方图时更改矩形的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 15:48