rework Jpanel

This commit is contained in:
Макс Бондаренко 2022-11-28 23:18:52 +04:00
parent c36ffe6688
commit 60e488021c
3 changed files with 64 additions and 29 deletions

View File

@ -57,16 +57,35 @@ public class DrawingShip extends JPanel {
}
//Отрисовка транспорта
public void DrawTransport()
public void DrawTransport(Graphics g)
{
if (GetWarmlyShip() == null) return;
if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null)
{
return;
}
repaint();
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(warmlyShip.GetBodyColor());
int[] xPol = new int[] {(int)(_startPosX), (int)(_startPosX + _warmlyShipWidth), (int)(_startPosX + _warmlyShipWidth - 25), (int)(_startPosX + 25)};
int[] yPol = new int[] {(int)(_startPosY + 20), (int)(_startPosY + 20), (int)(_startPosY + _warmlyShipHeight), (int)(_startPosY + _warmlyShipHeight)};
g2d.fillPolygon(new Polygon(xPol, yPol, xPol.length));
g2d.fillRect((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY, _warmlyShipWidth * 3 / 5, 20);
g2d.setColor(Color.CYAN);
g2d.fillOval((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY + 25, 20, 20);
g2d.fillOval((int)(_startPosX + _warmlyShipWidth * 3 / 5 + 5), (int)_startPosY + 25, 20, 20);
g2d.fillOval((int)(_startPosX + _warmlyShipWidth * 2 / 5 + 2.5f), (int)_startPosY + 25, 20, 20);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(new Polygon(xPol, yPol, xPol.length));
g2d.drawRect((int)(_startPosX + _warmlyShipWidth / 5), (int)(_startPosY), _warmlyShipWidth * 3 / 5, 20);
g2d.setColor(Color.BLUE);
g2d.drawOval((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY + 25, 20, 20);
g2d.drawOval((int)(_startPosX + _warmlyShipWidth * 3 / 5 + 5), (int)_startPosY + 25, 20, 20);
g2d.drawOval((int)(_startPosX + _warmlyShipWidth * 2 / 5 + 2.5f), (int)_startPosY + 25, 20, 20);
dd.DrawningDeck(_startPosX, _startPosY, _warmlyShipWidth, g2d, warmlyShip.GetBodyColor());
}
@Override
/*@Override
public void paintComponent(Graphics g){
if (GetWarmlyShip() == null) return;
@ -93,7 +112,7 @@ public class DrawingShip extends JPanel {
g2d.drawOval((int)(_startPosX + _warmlyShipWidth * 3 / 5 + 5), (int)_startPosY + 25, 20, 20);
g2d.drawOval((int)(_startPosX + _warmlyShipWidth * 2 / 5 + 2.5f), (int)_startPosY + 25, 20, 20);
dd.DrawningDeck(_startPosX, _startPosY, _warmlyShipWidth, g2d, warmlyShip.GetBodyColor());
}
}*/
//Изменение границ отрисовки
public void ChangeBorders(int width, int height)

View File

@ -36,14 +36,6 @@
<text value=""/>
</properties>
</component>
<component id="9f55" class="DrawingShip" binding="pictureShip">
<constraints>
<grid row="0" column="0" row-span="1" col-span="5" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="10" height="286"/>
</grid>
</constraints>
<properties/>
</component>
<component id="fb937" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
@ -83,7 +75,7 @@
<component id="9e2ce" class="javax.swing.JButton" binding="buttonCreate">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="118" height="30"/>
<preferred-size width="122" height="30"/>
</grid>
</constraints>
<properties>
@ -102,6 +94,14 @@
<text value=""/>
</properties>
</component>
<grid id="8b0ad" binding="GraphicsOutput" layout-manager="FlowLayout" hgap="5" vgap="5" flow-align="1">
<constraints>
<grid row="0" column="0" row-span="1" col-span="5" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
</children>
</grid>
</form>

View File

@ -5,43 +5,56 @@ import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
public class FormShip {
public class FormShip extends JFrame{
private JToolBar statusStrip;
public JPanel Mainpanel;
private DrawingShip pictureShip;
private DrawingShip ship;
private JButton buttonRight;
private JButton buttonCreate;
private JButton buttonLeft;
private JButton buttonUp;
private JButton buttonDown;
private JPanel GraphicsOutput;
private JLabel JLabelSpeed = new JLabel();
private JLabel JLabelWeight = new JLabel();
private JLabel JLabelColor = new JLabel();
private void Draw()
{
if (pictureShip.GetWarmlyShip() == null) return;
pictureShip.DrawTransport();
if (ship == null || ship.GetWarmlyShip() == null) return;
GraphicsOutput.removeAll();
BufferedImage bmp = new BufferedImage(GraphicsOutput.getWidth(), GraphicsOutput.getHeight(),BufferedImage.TYPE_INT_RGB);
Graphics g = bmp.getGraphics();
g.setColor(new Color(238, 238, 238));
g.fillRect(0, 0, GraphicsOutput.getWidth(), GraphicsOutput.getHeight());
ship.DrawTransport(g);
JLabel imageOfShip = new JLabel();
imageOfShip.setPreferredSize(GraphicsOutput.getSize());
imageOfShip.setMinimumSize(new Dimension(1, 1));
imageOfShip.setIcon(new ImageIcon(bmp));
GraphicsOutput.add(imageOfShip,BorderLayout.CENTER);
validate();
}
private void ButtonMove_Click(String name)
{
if (pictureShip == null) return;
if (ship == null) return;
switch (name)
{
case "buttonLeft":
pictureShip.MoveTransport(Direction.Left);
ship.MoveTransport(Direction.Left);
break;
case "buttonUp":
pictureShip.MoveTransport(Direction.Up);
ship.MoveTransport(Direction.Up);
break;
case "buttonRight":
pictureShip.MoveTransport(Direction.Right);
ship.MoveTransport(Direction.Right);
break;
case "buttonDown":
pictureShip.MoveTransport(Direction.Down);
ship.MoveTransport(Direction.Down);
break;
}
GraphicsOutput.revalidate();
Draw();
}
@ -70,11 +83,12 @@ public class FormShip {
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
pictureShip.Init(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)));
pictureShip.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), pictureShip.getWidth(), pictureShip.getHeight());
JLabelSpeed.setText("орость: " + pictureShip.GetWarmlyShip().GetSpeed() + " ");
JLabelWeight.setText("Вес: " + pictureShip.GetWarmlyShip().GetWeight() + " ");
JLabelColor.setText(("Цвет: " + pictureShip.GetWarmlyShip().GetBodyColor() + " "));
ship = new DrawingShip();
ship.Init(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)));
ship.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), GraphicsOutput.getWidth(), GraphicsOutput.getHeight());
JLabelSpeed.setText("орость: " + ship.GetWarmlyShip().GetSpeed() + " ");
JLabelWeight.setText("Вес: " + ship.GetWarmlyShip().GetWeight() + " ");
JLabelColor.setText(("Цвет: " + ship.GetWarmlyShip().GetBodyColor() + " "));
Draw();
}
});
@ -102,11 +116,13 @@ public class FormShip {
ButtonMove_Click("buttonRight");
}
});
pictureShip.addComponentListener(new ComponentAdapter() {
GraphicsOutput.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
super.componentResized(e);
pictureShip.ChangeBorders(pictureShip.getWidth(), pictureShip.getHeight());
if (ship == null || ship.GetWarmlyShip() == null) return;
ship.ChangeBorders(GraphicsOutput.getWidth(), GraphicsOutput.getHeight());
GraphicsOutput.revalidate();
Draw();
}
});