gpt4 book ai didi

java - 如何使用paintComponent以不同的方法进行不同的绘图?

转载 作者:行者123 更新时间:2023-12-02 07:03:44 25 4
gpt4 key购买 nike

我有 2 个类,一个扩展面板,另一个扩展框架。

第一类,面板(用于绘图):

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class GraficPne extends JPanel{


public int dim1 = 0, dim2 = 0;
public JLabel lblSem1GraficPne, lblSem2GraficPne;

public GraficPne(){
super();
this.setLayout(null);
this.setupComponents();

}


public void setupComponents(){

lblSem1GraficPne = new JLabel("Sem I");
lblSem2GraficPne = new JLabel("Sem II");

lblSem1GraficPne.setFont(new Font("Serif", Font.PLAIN, 12));
lblSem2GraficPne.setFont(new Font("Serif", Font.PLAIN, 12));

this.add(lblSem1GraficPne);
this.add(lblSem2GraficPne);
}

public void layComponents(int firstConv, int secondConv){

lblSem1GraficPne.setBounds(20, getHeight() - firstConv - 20, 100, 20);
lblSem2GraficPne.setBounds(100, getHeight() - secondConv - 20, 100, 20);

}



public void paint(Graphics g){

super.paint(g);

g.setColor(Color.BLACK);
g.drawRect(0, 0, getWidth()-1, getHeight()-1);

this.setBackground(Color.GREEN);

g.setColor(Color.RED);
g.fillRect(20, getHeight()-dim1, 40, dim1);

g.setColor(Color.BLUE);
g.fillRect(100, getHeight()-dim2, 40, dim2);

}



public void setData(int dim1, int dim2){

this.dim1 = dim1;
this.dim2 = dim2;

}

}

第二类,框架,用于在其上添加面板:

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


public class GraficFrm extends JFrame implements ActionListener{

private JButton btnApply;
private JLabel lblSem1, lblSem2;
private JTextField txtSem1, txtSem2;
private GraficPne ob; //the object from first class
private int firstConv = 0, secondConv = 0;


public GraficFrm() {

super("Grafic soft");
initSettings();
setupComponents();
}



private void initSettings() {

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
this.setSize(400, 450);
this.setLocation(100, 100);
this.setResizable(false);

}


private void setupComponents() {


ob = new GraficPne();
lblSem1 = new JLabel("Sem I");
txtSem1 = new JTextField();
lblSem2 = new JLabel("Sem II");
txtSem2 = new JTextField();
btnApply = new JButton("Apply");

ob.setBounds(30, 50, 340, 200);
lblSem1.setBounds(30, 280, 100, 20);
txtSem1.setBounds(150, 280, 220, 20);
lblSem2.setBounds(30, 330, 100, 20);
txtSem2.setBounds(150, 330, 220, 20);
btnApply.setBounds(170, 380, 80, 30);

this.add(lblSem1);
this.add(ob);
this.add(txtSem1);
this.add(lblSem2);
this.add(txtSem2);
this.add(btnApply);

btnApply.addActionListener(this);

}


public void getConvertedValues(){

String txtField1 = txtSem1.getText();
firstConv = Integer.parseInt(txtField1);
String txtField2 = txtSem2.getText();
secondConv = Integer.parseInt(txtField2);

}


public void getHighValues(){

if((firstConv > 100) || (secondConv > 100)) {

JOptionPane.showMessageDialog(this, "Only values < 100 are allowed");

firstConv = 0;
secondConv = 0;
txtSem1.setText("");
txtSem2.setText("");
ob.removeAll();
ob.repaint();
}

}



public void actionPerformed(ActionEvent e) {


if(e.getSource().equals(btnApply)){

try{

new GraficFrm();
getConvertedValues();
getHighValues();
ob.setData(firstConv, secondConv);
ob.layComponents(firstConv,secondConv);
ob.repaint();

}catch(NumberFormatException ex1){
JOptionPane.showMessageDialog(this, "Only natural numbers are allowed");
}

}

}

public static void main(String[] args) {

GraficFrm frm = new GraficFrm();
frm.setVisible(true);
}

}

我想要做的就是创建一个名为 getNegativeValues() 的新方法,按下“Apply”按钮即可调用该方法,并且当firstConv或secondConv < 0时执行该方法。我的方法的 block ,getNegativeValues(),我想只保留面板的绿色背景,就像在paint(Graphics g)中一样,但要绘制一条中心水平线(g.drawLine(0, getHeight()/2, getWidth()) , getHeight()/2);)。然后,当我执行程序并输入时,假设第一个文本字段的值为 55,第二个文本字段的值为 -40,在中心水平线上绘制 55,在中心水平线下绘制 -40,0 点将位于中心水平线上。

LE:@nIcE cOw 我将做一个简短的回顾:所以,首先,当我执行我的应用程序时,我只想显示我的绿色面板和那两个有 2 个文本字段的标签,如我的示例中所示(请尝试遵循我的示例)(很好,我已经这样做了,所以这部分已经完成)。之后,当我完成这两个文本字段时,假设有两个正值,然后当我按下“应用”按钮时,就会发生与我的示例中完全相同的情况(没有任何中心垂直线)(所以也已经这样做了)。一切都很好,但我的问题是:如果用负值填写这两个文本字段之一,用正值填写另一个文本字段,并且在我按下“应用”按钮后,我希望我的应用程序能够识别出其中一个输入了负值我的文本字段,因此在我的绿色面板上绘制一条中心水平线(在我按下“应用”按钮后)并完全按照您在上传的第一张图片中所做的那样绘制这两个矩形。请帮助我,正如我所说,我会帮助你理解我想做什么,而且我想说我真的很感谢你的努力,如果我(再次)感到困惑,我很抱歉(再次)。

最佳答案

我想,您正在寻找的是如下所示的方法,它可以计算您尝试在 JPanel 上绘制的矩形的起点。这里 pos 是正值,neg 是负值:

public void setValues(int above, int below)
{
midPoint = getHeight() / 2;
heightAbove = Math.abs(above);
heightBelow = Math.abs(below);
/*
* If both are negative values,
* we need a Mid-Point in this case.
*/
if (above < 0 && below < 0)
{
rectAbove = midPoint;
diff = 120;
rectBelow = midPoint;
flag = true;
}
else if (above >= 0 && below >= 0)
{
rectAbove = getHeight() - heightAbove;
diff = 120;
rectBelow = getHeight() - heightBelow;
flag = false;
}
else if (above < 0 && below >= 0)
{
rectAbove = midPoint;
diff = 40;
rectBelow = midPoint - heightBelow;
flag = true;
}
else if (above >= 0 && below < 0)
{
rectAbove = midPoint - heightAbove;
diff = 40;
rectBelow = midPoint;
flag = true;
}

repaint();
}

这是代码:

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

public class PaintingRectangles
{
private JTextField aboveField;
private JTextField belowField;
private JButton applyButton;
private MyCanvas canvas;

private void displayGUI()
{
JFrame frame = new JFrame("Painting Rectangles Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout(5, 5));

JPanel rightPanel = new JPanel();
rightPanel.setLayout(new GridLayout(3, 1, 5, 5));
aboveField = new JTextField(10);
belowField = new JTextField(10);
applyButton = new JButton("Apply");
applyButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent ae)
{
int above = 0;
int below = 0;
try
{
above = Integer.parseInt(aboveField.getText());
below = Integer.parseInt(belowField.getText());
}
catch(NumberFormatException nfe)
{
aboveField.setText("0");
belowField.setText("0");
above = 0;
below = 0;
nfe.printStackTrace();
}

canvas.setValues(above, below);
}
});

rightPanel.add(aboveField);
rightPanel.add(belowField);
rightPanel.add(applyButton);

canvas = new MyCanvas();
contentPane.add(canvas, BorderLayout.CENTER);
contentPane.add(rightPanel, BorderLayout.LINE_END);

frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
new PaintingRectangles().displayGUI();
}
});
}
}

class MyCanvas extends JPanel
{
private int midPoint = getHeight() / 2;
private int rectAbove = getHeight() / 2;
private int rectBelow = getHeight() / 2;
private int width = 40;
private int heightAbove = 0;
private int heightBelow = 0;
private int diff = 0;
private boolean flag = false;

public MyCanvas()
{
setBackground(Color.GREEN);
}

public void setValues(int above, int below)
{
midPoint = getHeight() / 2;
heightAbove = Math.abs(above);
heightBelow = Math.abs(below);
/*
* If both are negative values,
* we need a Mid-Point in this case.
*/
if (above < 0 && below < 0)
{
rectAbove = midPoint;
diff = 120;
rectBelow = midPoint;
flag = true;
}
else if (above >= 0 && below >= 0)
{
rectAbove = getHeight() - heightAbove;
diff = 120;
rectBelow = getHeight() - heightBelow;
flag = false;
}
else if (above < 0 && below >= 0)
{
rectAbove = midPoint;
diff = 40;
rectBelow = midPoint - heightBelow;
flag = true;
}
else if (above >= 0 && below < 0)
{
rectAbove = midPoint - heightAbove;
diff = 40;
rectBelow = midPoint;
flag = true;
}

repaint();
}

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

@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.BLACK);
g.drawRect(0, 0, getWidth(), getHeight());
if (flag)
g.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2);
makeRectangles(g);
}

private void makeRectangles(Graphics g)
{
g.setColor(Color.RED);
g.fillRect(40, rectAbove, width, heightAbove);

g.setColor(Color.BLUE);
g.fillRect(diff, rectBelow, width, heightBelow);

g.dispose();
}
}

这是输出:

RECTANGLES RECTANGLES! RECTANGLES2 RECTANGLES3

关于java - 如何使用paintComponent以不同的方法进行不同的绘图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16340398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com